We are about to switch to a new forum software. Until then we have removed the registration on this forum.
So I am trying to make a menu for a game I am building, but when I use the keyPressed to switch turns and Init the Game I get this error message. Any Idea why? Thanks In Advance ;)
space [] Space = new space[800];
int turn=1; //1=menu,2=initgame,3=gameover
void setup()
{
size(800,800);
//
if (turn==2)
{
initstarField();
}
}
void draw()
{
background(0);
if (turn==1)
{
menu();
}
else if (turn==2)
{
starField();
}
}
void keyTyped()
{
if (turn==1 && key=='a')
turn=2;
}
void menu()
{
if (turn==1)
{
background(0,0,255);
}
}
void initGame()
{
}
void spaceShip()
{
}
void asteroids()
{
}
void distanceTracker()
{
}
void initstarField()
{
for (int i = 0; i < Space.length; i++)
{
Space[i]=new space();
}
}
void starField()
{
translate(width/2, height/2);
for (int i = 0; i < Space.length; i++)
{
Space[i].lightSpeed();
Space[i].create();
}
}
// class
class space
{
float x;
float y;
float z;
float pz;
space()
{
x =random(-width, width);
y =random(-height, height);
z =random (width);
pz=z;
}
void lightSpeed()
{
z=z-25;
if (z<1)
{
z=width;
x =random(-width, width);
y =random(-height, height);
pz=z;
}
}
void create()
{
//Stars
float sx = map (x / z, 0, 1, 0, width);
float sy = map (y / z, 0, 1, 0, height);
float r= map(z, 0, width, 16, 0);
fill(255);
ellipse(sx, sy, r, r);
//LightSpeed Illusion
float px = map(x/pz, 0, 1, 0, width);
float py = map(y/pz, 0, 1, 0, width);
pz=z;
stroke(255);
line(px, py, sx, sy);
}
}
Answers
When I run the code you posted I just see a blue window without any errors.
You need to click on it and press 'a' to make it switch turns then you'll get a "NullPointerException"
Okay, so have you tried debugging your code to figure out exactly why that's happening? Which line is it happening on? Which variable is null?
line 77 and void lightSpeed the problem when the turn switches to 2 it gives me that but when I set to turn to 2 from the very beginning at line 3 everything works fine I'm new to programming so I have no idea what to do now
Did you click the link I gave you? You need to debug your code to narrow the problem down.