So I am new to Greenfoot and I am having a problem with a actor. Basically, all I need is for the program to quit when the value of the fuel hits zero.
import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot)
public class shuttle extends Actor
{
//variables
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;
public Explosion E1;
private int direction;
GoingToMoon world = (GoingToMoon)getWorld(); //tells the program that space is the same as the word world
public Counter FCounter;
public void setDirection(int direction) //constructor
{
this.direction = direction;
switch(direction)
{
case SOUTH:
setRotation(180); // points down
break;
case EAST:
setRotation(90); // points right
break;
case NORTH:
setRotation(0); // points up
break;
case WEST:
setRotation(270); // points left
break;
default: // execution continues without change
break;
}
}
public void FuelDisplay()
{
int Fuel = 5;
if (FCounter == null)
{
FCounter = new Counter ("Fuel: ", Fuel);
getWorld().addObject(FCounter, 720, 280);
}
FCounter.Decrement();
if (Fuel == 0)
{
GoingToMoon world = (GoingToMoon)getWorld();
Greenfoot.stop();
}
}
public shuttle()
{
setDirection(NORTH); //sets to the default direction
}
public void move()
{
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 act()
{
if(FoundMoon())
{
setDirection(SOUTH); //points ship down
move(); //asks ship to move
} else if (FoundEarth())
{
setDirection(NORTH); //points ship up
Greenfoot.stop(); //ends game
} else if (FoundRockC())
{
explode();
Greenfoot.stop();
} else if (FoundRockR())
{
explode();
Greenfoot.stop();
} else if (FoundRockL())
{
explode();
Greenfoot.stop();
}
move();
processKeys();
FuelDisplay();
}
public boolean FoundMoon()
{
Actor MoonL = getOneObjectAtOffset(0, -15, MoonLand.class); //detects moon
if (MoonL != null)
{
F1 = new Flag(); //creates new flag
getWorld().addObject(F1, 450, 50); //changes location of F1
return true;
} else {
return false;
}
}
public boolean FoundEarth()
{
Actor EarthL = getOneObjectAtOffset(0, 5, Earth.class); //detects moon
if (EarthL != null)
{
H1 = new Home(); //creates new landing message
getWorld().addObject(H1, 550, 750); //changes location of H1
return true;
} else {
return false;
}
}
private void processKeys()
{
if(Greenfoot.isKeyDown("down"))
{
setDirection(SOUTH);
move();
}
if(Greenfoot.isKeyDown("up"))
{
setDirection(NORTH);
move();
}
if(Greenfoot.isKeyDown("right"))
{
setDirection(EAST);
move();
}
if(Greenfoot.isKeyDown("left"))
{
setDirection(WEST);
move();
}
}
private boolean FoundRockC()
{
Actor RockC = getOneObjectAtOffset(0, -15, RockCentre.class); //detects rock centre
Actor RockL = getOneObjectAtOffset(0, -15, RockLeft.class); //detects rock left
Actor RockR = getOneObjectAtOffset(0, -15, RockRight.class); //detects rock right
if (RockC != null)
{
return true;
} else {
return false;
}
}
private boolean FoundRockR()
{
Actor RockR = getOneObjectAtOffset(0, -15, RockRight.class); //detects rock right
if (RockR != null)
{
return true;
} else {
return false;
}
}
private boolean FoundRockL()
{
Actor RockL = getOneObjectAtOffset(0, -15, RockLeft.class); //detects rock left
if (RockL != null)
{
return true;
} else {
return false;
}
}
public void explode()
{
E1 = new Explosion(); //creates new flag
getWorld().addObject(E1, getX(), getY()); //changes location of F1
}
public boolean FoundSpaceStation()
{
Actor SpaceS = getOneObjectAtOffset(+15, 0, spacestation.class);
if (SpaceS != null)
{
return true;
}
else
{
return false;
}
}
}