Hey you guys,
I hope you can help me with this. I am creating a little game, for which I need an array of Snowflakes (an object I created), which I create and instantiate. Despite that I am still getting a NPE. Here’s my code:
Main Class:
//Global vars
Panel panel;
Snowflake[] flakes;
void setup() {
fullScreen(P2D);
panel = new Panel();
flakes = new Snowflake[5];
for(Snowflake sf:flakes){
sf = new Snowflake(int(random(20, width - 20)), int(random(20, height - 20)), 20, 3);
sf.drawSnowflake();
}
}
void draw() {
background(0);
panel.drawPanel();
for(Snowflake sf:flakes){
sf.drawSnowflake();
}
}
Snowflake Class:
class Snowflake{
int posX;
int posY;
int offset;
int speed;
boolean firstDraw = true;
public Snowflake(int posX, int posY, int offset, int speed){
this.posX = posX;
this.posY = posY;
this.offset = offset;
this.speed = speed;
}
public void drawSnowflake(){
if(firstDraw == true){
createSFShape(posX,posY,offset);
firstDraw = false;
}
else{
int rPosY;
if(posY >= height){
rPosY = 0;
posY = rPosY;
}
else{
rPosY = posY + speed;
posY = rPosY;
}
createSFShape(posX,rPosY,offset);
}
}
public void createSFShape(int x, int y, int offset){
stroke(255);
line(x, y - offset, x, y + offset);
line(x - offset, y, x + offset, y);
line(x - offset, y - offset, x + offset, y + offset);
line(x + offset, y - offset, x - offset, y + offset);
}
}
I hope you can help me with this and thanks for reading!
PS: If I create a single Snowflake, the code works.