[SOLVED] PImage[] array inside class shows all images at once

Hey it me again !

So I believe what your problem is that you hit the key and they only appear for a second right ? Then the boolean ‘stop key’ isn’t correct. But the answer I gave previously should have solved it. But here’s a more sophisticated example.


Planets planetTex;
float x = 500;
float y = 110;
void setup() {
  size (800, 640);
  planetTex = new Planets();
}

void draw() {
  background(0);
  planetTex.pickedPlanet();
}
void keyPressed() {
  if(keyCode == 'A' && !planetTex.getPressed()) planetTex.pressed(true);
  else if(keyCode == 'A' && planetTex.getPressed() ) planetTex.pressed(false);
}
class Planets {
  PImage[] planetDiffuse = new PImage[2];
  boolean chosen;
  Planets() {
    planetDiffuse[0] = loadImage("earth.png");
    planetDiffuse[1] = loadImage("earth2.png");
    chosen = false;
  }
  void pickedPlanet() {
    if ( chosen ) {
      for (int i = 0; i < planetDiffuse.length; i++) {
        image(planetDiffuse[i], i%width, i%height);
      }
    }
  }
  boolean pressed(boolean hitKey) {
    return chosen = hitKey;
  }
  boolean getPressed(){
    return chosen;  
  }
}

Does this fix your issue ?

Also a note. Your images will be flickering as when you haveshowit = true; A random number is picked in the draw method every single time the draw() is called. This should only be done per press not inside the draw method as it executes 60 FPS. That what was causing your issue as your picking a new image 60 FPS.

if (showit) {
    image(planetDiffuse[int(random(planetDiffuse.length))], x, y);
  }
3 Likes