Sprites animation that use a lot of memory

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 :slight_smile:

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);
 }
}

Hi, can you also link the images that your are using ?

Yes of course but how do I do it ? I’m new in this forum so I don’t know how.

Ok i find a way to give a link of the data folder, there it is :
https://drive.google.com/open?id=1epCjqgq2cZ06WntQpM4qlA-ZtJWEO5eH

@Summer Hi you can also copy and paste your code in the message box with The code formatting button

Here are some other tips :slight_smile: Guidelines—Tips on Asking Questions

Ok thanks for the tip :slight_smile:
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);
  }
}

And there is the data folder with the images if needed :
https://drive.google.com/open?id=1epCjqgq2cZ06WntQpM4qlA-ZtJWEO5eH

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

Ok thanks for the answere I’ll try these solutions :wink:

In addition, there is also a Sprites library for Processing.