Processing slow down with images

edited December 2014 in How To...

Hi everyone, I've just finished a simple bouncing ball/arkanoid game for University. I did it only with rect(),ellipse() and line(). Now it's time to replace them with images. When I put the images (over the blocks) the game slows down a lot. The images' size is only 3,07kb, PNG, 64x32 pixels.

Here I upload the game without images: link

And here with this first try of images (in class Bloques): link

I think the problem is that I'm using a for-loop to load them.

Thank you.

Answers

  • Well, I was using an array for loading each image. I delete the array and the for-loop, and the sketch works perfectly. I'm gonna keep trying with another images. If another error occurs I will comment.

  • Answer ✓

    you need to load all images in setup() to make sure it's done only once. don't do it in the class or in draw().

    I can't open the code, can you post the code here?

    ;-)

  • after you've loaded them into an array, just work with index - should be fast.

  • edited December 2014 Answer ✓

    Chrisir guessed right:

      void dibujar() {
        if(estado.equals("intacto")){
           noStroke();
           fill(255);
         } else{     
         fill(150,0,0);
       }
        rect(x, y, tamx, tamy);
        for (int j=0; j<filas; j++) {
           for (int i=0; i<cant; i++) {
           block[j][i] = loadImage("block.jpg");
           image(block[j][i],x,y);
           }
           }
      }
    

    If dibujar() is called in draw(), it means you load images in a loop 60 times per second. No wonder the sketch is slowed down!

    See also What are setup() and draw()? and the other articles of the Common Questions category.

  • thank you, philho!

  • Yes, I've noticed that and solved it. Now I'm gonna load it in setup as Chrisir said.

    P.S: Chrisir I owe you my mark in the University haha

  • glad to hear that!

    ;-)

Sign In or Register to comment.