I have been working on a new scenario and I can't seem to get the game to end. I want the game to end when the shuttle runs out of fuel or if it explodes, however, I can't get it to work. I think the problem is that a variable isn't linked between the two classes (in this case, fuel). Here is the code for the two classes.
Counter:
Shuttle:
I also get an error when the shuttle hits a rock. The error highlights line 40 of the shuttle class, saying there is a NullPointerException Error.
public class Counter extends Actor
{
//Variables used later
private int value;
private String text;
private int stringLength;
public Counter()
{
text = "";
stringLength = (text.length() + 2) * 10;
updateImage();
}
public Counter(String prefix, int fuel)
{
value = fuel;
text = prefix;
stringLength = (text.length() + 2) * 10;
setImage(new GreenfootImage(stringLength, 16)); // sets up an image using stringLength which is 16 pixels above the image
updateImage();
}
public void Reset(int valuenew)
{
value = valuenew;
updateImage();
}
public void Decrement()
{
value--; //decreases the value
updateImage();
}
/**
* Create and update the image
*/
public void updateImage()
{
GreenfootImage image = getImage();
image.clear();
image.setColor(Color.WHITE); //sets the text colour to white
image.drawString(text + value, 1, 12);
}
}public class shuttle extends Actor
{
//Variables used later
private static final int east = 0;
private static final int west = 1;
private static final int north = 2;
private static final int south = 3;
public Flag F1;
public Home H1;
private int direction;
private static int fuel = 1500;
private Counter FCounter;
public shuttle()
{
setDirection(north);
}
public void act()
{
fuelDisplay();
checkKeys();
move();
if(foundMoon())
{
setDirection(south);
move();
}
else if(foundEarth())
{
setDirection(north);
Greenfoot.stop();
}
else if(foundRock())
{
explode();
getWorld().addObject(new GameOver(), 400, 400);
Greenfoot.stop();
}
else if(foundStation())
{
fuel = 1500;
FCounter.Reset(fuel);
setDirection(west);
move();
}
if(fuel <= 0)
{
getWorld().addObject(new GameOver(), 400, 400);
Greenfoot.stop();
}
}
private void checkKeys()
{
//If the down arrow key is pressed, change the shuttle's direction to south
if(Greenfoot.isKeyDown("down"))
{
setDirection(south);
move();
}
//If the up arrow key is pressed, change the shuttle's direction to north
if(Greenfoot.isKeyDown("up"))
{
setDirection(north);
move();
}
//If the right arrow key is pressed, change the shuttle's direction to east
if(Greenfoot.isKeyDown("right"))
{
setDirection(east);
move();
}
//If the left arrow key is pressed, change the shuttle's direction to west
if(Greenfoot.isKeyDown("left"))
{
setDirection(west);
move();
}
}
public void move()
{
//Depending on the direction, move in that direction
switch(direction)
{
case south:
setLocation(getX(), getY() + 1);
break;
case east:
setLocation(getX() + 1, getY());
break;
case north:
setLocation(getX(), getY() - 1);
break;
case west:
setLocation(getX() - 1, getY());
break;
}
}
public void setDirection(int direction)
{
this.direction = direction;
//Depending on the direction, set a rotation according to that direction
switch(direction)
{
case south:
setRotation(180);
break;
case east:
setRotation(90);
break;
case north:
setRotation(0);
break;
case west:
setRotation(270);
break;
default:
break;
}
}
public boolean foundMoon()
{
Actor moonl = getOneObjectAtOffset(0, 15, MoonLand.class);
//If the shuttle contacts the moonlanding, spawn in a flag
if(moonl != null)
{
F1 = new Flag();
getWorld().addObject(F1, 450, 50);
return true;
}
else
{
return false;
}
}
public boolean foundEarth()
{
Actor earthl = getOneObjectAtOffset(0, 5, Earth.class);
//If the shuttle contacts the earth, spawn in a welcome home sign
if(earthl != null)
{
H1 = new Home();
getWorld().addObject(H1, 550, 750);
return true;
}
else
{
return false;
}
}
public boolean foundRock()
{
Actor rock1 = getOneObjectAtOffset(0, 10, RockCentre.class);
Actor rock2 = getOneObjectAtOffset(0, 10, RockLeft.class);
Actor rock3 = getOneObjectAtOffset(0, 10, RockRight.class);
//If the shuttle contacts any rock, cause an explosion
if(rock1 != null || rock2 != null || rock3 != null)
{
return true;
}
else
{
return false;
}
}
public boolean foundStation()
{
Actor station = getOneObjectAtOffset(+15, 0, spacestation.class);
//If the shuttle comes into contact with the space station, return true
if(station != null)
{
return true;
}
else
{
return false;
}
}
public void explode()
{
//Spawns in an explosion and removes the rocket
getWorld().addObject(new Explosion(), getX(), getY());
getWorld().removeObject(this);
}
public void fuelDisplay()
{
if(FCounter == null)
{
FCounter = new Counter("Fuel: ", fuel);
getWorld().addObject(FCounter, 720, 280);
}
FCounter.Decrement();
}
}
