Summer
April 27, 2019, 6:07pm
1
I made a program that animate sprites but it use a huge amount of memory (~ 3000 MB). I increased the memorry alowed in preference and the program run but it still laggy. Can someone help me to resolve this problem. Thanks for answering
PImage[] walk1 = new PImage[43];
PImage[] attack1 = new PImage[40];
PImage[] idle1 = new PImage[50];
PImage[] walk2 = new PImage[40];
PImage[] attack2 = new PImage[45];
PImage[] idle2 = new PImage[48];
void setup(){
frameRate(60);
size(1000,600);
for(int i=0;i<=42;i++){
walk1[i] = loadImage("Préhistoire/Niv 1/Marche/"+(i+1)+".png");
}
for(int i=0;i<=39;i++){
attack1[i] = loadImage("Préhistoire/Niv 1/Attaque/"+(i+1)+".png");
}
for(int i=0;i<=49;i++){
idle1[i] = loadImage("Préhistoire/Niv 1/Immobile/"+(i+1)+".png");
}
for(int i=0;i<=39;i++){
walk2[i] = loadImage("Préhistoire/Niv 2/Marche/"+(i+1)+".png");
}
for(int i=0;i<=44;i++){
attack2[i] = loadImage("Préhistoire/Niv 2/Attaque/"+(i+1)+".png");
}
for(int i=0;i<=47;i++){
idle2[i] = loadImage("Préhistoire/Niv 2/Immobile/"+(i+1)+".png");
}
}
void draw(){
background(0);
if(mousePressed){
image(attack1[frameCount%40], 200,200,114,117);
image(attack2[frameCount%45], 500,200,218,134);
}else if(keyPressed){
image(walk1[frameCount%43],211,226,69,90);
image(walk2[frameCount%40],502,200,180,133);
}else{
image(idle1[frameCount%50],228,228,49,88);
image(idle2[frameCount%48],502,201,179,132);
}
}
jb4x
April 27, 2019, 6:13pm
2
Hi, can you also link the images that your are using ?
Summer
April 27, 2019, 7:29pm
3
Yes of course but how do I do it ? I’m new in this forum so I don’t know how.
Summer
April 27, 2019, 8:42pm
4
dan850
April 27, 2019, 8:52pm
5
@Summer Hi you can also copy and paste your code in the message box with The code formatting button
Here are some other tips Guidelines—Tips on Asking Questions
Summer
April 27, 2019, 9:06pm
6
Ok thanks for the tip
Like this the code will be easier for people to understand it :
PImage[] walk1 = new PImage[43];
PImage[] attack1 = new PImage[40];
PImage[] idle1 = new PImage[50];
PImage[] walk2 = new PImage[40];
PImage[] attack2 = new PImage[45];
PImage[] idle2 = new PImage[48];
void setup() {
frameRate(60);
size(1000, 600);
for (int i=0; i<=42; i++) {
walk1[i] = loadImage("Préhistoire/Niv 1/Marche/"+(i+1)+".png");
}
for (int i=0; i<=39; i++) {
attack1[i] = loadImage("Préhistoire/Niv 1/Attaque/"+(i+1)+".png");
}
for (int i=0; i<=49; i++) {
idle1[i] = loadImage("Préhistoire/Niv 1/Immobile/"+(i+1)+".png");
}
for (int i=0; i<=39; i++) {
walk2[i] = loadImage("Préhistoire/Niv 2/Marche/"+(i+1)+".png");
}
for (int i=0; i<=44; i++) {
attack2[i] = loadImage("Préhistoire/Niv 2/Attaque/"+(i+1)+".png");
}
for (int i=0; i<=47; i++) {
idle2[i] = loadImage("Préhistoire/Niv 2/Immobile/"+(i+1)+".png");
}
}
void draw() {
background(0);
if (mousePressed) {
image(attack1[frameCount%40], 200, 200, 114, 117);
image(attack2[frameCount%45], 500, 200, 218, 134);
} else if (keyPressed) {
image(walk1[frameCount%43], 211, 226, 69, 90);
image(walk2[frameCount%40], 502, 200, 180, 133);
} else {
image(idle1[frameCount%50], 228, 228, 49, 88);
image(idle2[frameCount%48], 502, 201, 179, 132);
}
}
Summer
April 27, 2019, 9:09pm
7
jb4x
April 28, 2019, 3:52pm
8
Hi,
I downloaded your files and it is almost 700Mo ! Plus your are loading everything at the same time so it’s really heavy on the computer and the ram.
You need to reduce the size of your files. Here are some ideas:
Lower the number of frame per animation (I can see that sometime you have around a hundred frame for just 1 move. I think you can get a pretty decent look with only 12.
Lower the size of your images. Some of them are more than 1600x1600 for just a character. I doubt that it will be it’s final size on screen. So no need to make it that big.
Save as jpg can also help to save some space but sometimes png is need to get nice results.
In addition of that you can build a resources manager. I presume that you won’t need all the sprites all the time so you can load only the one you will be using.
1 Like
Summer
April 28, 2019, 3:58pm
9
Ok thanks for the answere I’ll try these solutions
In addition, there is also a Sprites library for Processing.