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

No problem to show you ! This line is called within planetSpawnHere() the issue is the random() as everytime the method is entered it will go and get a different image. That is why we shouldn’t calculate a random number at every iteration but just ‘sometimes’ for example when you press a key ( that up to you how often , but remember to much and it will flicker ) but if you leave getting the random number being calculated in draw() it will keep causing the flickering!

image(PlanetLand[int(random(PlanetLand.length))], x, y);

Here’s code which accommodates what I meant earlier.

JSONArray values;
ArrayList<Moon> manen = new ArrayList<Moon>();
ArrayList<Clouds> wolken = new ArrayList<Clouds>();

boolean planetSpawn = false;

PImage matte;

PImage temp; //screenshot
int saveP = 1;

PImage[] PlanetLand = new PImage[4];

float x = 500;
float y = 110;
float diameter = 600;

Planet planeet;

void setup() {
  size(1600, 800);
  planeet = new Planet();
  matte = loadImage("matte.png"); 

  for (int i = 0; i < PlanetLand.length; i++) {
    PlanetLand[i] = loadImage("land" + i + ".png");
  }
}

void draw() {
  background(0);

  for (Clouds b : wolken) {
    b.cloudPuff();
  }

  image(matte, 0, 0); //obscure layer for clouds

  for (Moon a : manen) {
    a.orbitLines();
    a.whiteCirkel();
  }

  planeet.planetSpawnHere();
}

void keyPressed() {
  planeet.getRandom();
  if (key == '1') {
    manen.add(new Moon(random(200, 380), random(20, 100), random(5, 45), random(0.002, 0.05), color(random(255), random(255), random(255))));
  }

  if (key == '2') {
    wolken.add(new Clouds(random(30, 75), random(15, 30), random(150, 600), random(250, 350), random(0, 10)));
  }

  if (key == 'a') {
    planetSpawn = true;
  }
}

void keyTyped() {
  if (key == '0') {
    PImage temp = get(415, 0, 770, 800); 
    temp.save("MyPlanet_" + saveP + ".png"); 
    saveP++;
  }
}




class Clouds {
  float widthClouds, heightClouds;
  float x, y;
  float speed;
  float start;

  Clouds(float tempWidth, float tempHeight, float tempVertical, float tempStart, float tempSpeed) {
    widthClouds = tempWidth;
    heightClouds = tempHeight;
    x = 500;
    y = tempVertical;
    start = tempStart;
    speed = tempSpeed;
  }

  void cloudPuff() {
    for (int i = 0; i < speed; i++) {
      if (x > 1050) {
        x = start;
      }
      x = x + 1;
    }

    noStroke();
    fill(240);
    rect(x, y, widthClouds, heightClouds, 250);
  }
}




class Moon {
  float x, y;
  float radius;
  int centX;
  int centY;
  float ang;
  float angspeed;
  float sizeMoon;
  color kleur;

  Moon(float tempRadius, float tempAng, float tempMoonSize, float tempAngSpeed, color tempKleur) {
    radius = tempRadius;
    centX = width/2;
    centY = height/2;
    ang = tempAng;
    angspeed = tempAngSpeed;
    sizeMoon = tempMoonSize;
    kleur = tempKleur;
  }

  void orbitLines() {
    stroke(kleur);
    strokeWeight(2);
    noFill();
    ellipse(centX, centY, radius * 2, radius * 2);
  }

  void whiteCirkel() {
    stroke(kleur);
    strokeWeight(1);
    fill(kleur);
    ellipse(x, y, sizeMoon, sizeMoon);

    x = centX + (radius * cos(ang));
    y = centY + (radius * sin(ang));

    ang = ang + angspeed;
  }
}




class Planet {
  PImage[] planetDiffuse = new PImage[4];
  int rand;
  Planet() {
    for (int i = 0; i < planetDiffuse.length; i++) {
      planetDiffuse[i] = loadImage("land" + i + ".png");
    }
	rand = 0;
  }

  void planetSpawnHere() {
    if (planetSpawn) {
      image(PlanetLand[rand], x, y);
      //planetSpawn = false;
    }
  }
  void getRandom(){
	rand = int(random(PlanetLand.length));
  }
}
2 Likes