I am trying to remove a GameOver object but I can't get it to work. Also my fell() method isn't working. What am I doing wrong?
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Bob here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Bob extends Actor
{
private int speed = 6; //declares the speed variable
private int vspeed = 0; // declares the vspeed variable
private int acceleration = 1; // declares the acceleration variable
private static final int jumpStrength = 12;
private GameOver GameOver;
/**
* Act - do whatever the Bob wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
checkKeys(); // runs the check key method
fall(); // runs the fall method
CheckFall();// runs the CheckFall method
dead();//runs the dead method
fell();//runs the fell method
win(); //runs the win method
}
private void checkKeys()// declares the checkKeys method
{
if (Greenfoot.isKeyDown("d"))// checks for the d key to be pressed
{
setImage("polar-bear.png");
moveRight(); // moves right according ot the public void
}
if (Greenfoot.isKeyDown("a"))// checks for the a key to be pressed
{
setImage("polar-bearCopy.png");
moveLeft(); // moves left according to the public void
}
if (Greenfoot.isKeyDown("space"))//checks for the space key to be pressed
{
if (onGround()|| onMovingGround())
jump();
}
if (Greenfoot.isKeyDown("w"))//checks for the space key to be pressed
{
if (onGround()|| onMovingGround())
jump();
}
}
public void moveLeft()// declares moveLeft method
{
setLocation(getX()- speed,getY());// moves Bob left at the rate of the speed integer earlier declared
}
public void moveRight()// declares the moveRight method
{
setLocation(getX() + speed,getY()); //moves Bob right at the rate of the speed integer earlier declared
}
public void fall()// declares the fall method
{
setLocation(getX(),getY()+vspeed); // creates the falling action
vspeed = vspeed + acceleration; // makes the fall look more natural
}
public boolean onGround()// declares the on ground boolean
{
Object under = getOneObjectAtOffset ( 0, getImage().getHeight()/2, Ground.class);// checks for ground underneath the bear
return under!= null;
}
public void CheckFall()// declares the check fall method
{
{
if (onGround() || onMovingGround())
{
vspeed=(0); // sets the vspeed to 0 if the onGround boolean returns true
}
else
{
fall(); // falls if he is not on ground
}
Actor under = getOneObjectAtOffset ( 0, getImage().getHeight()/2, Ground.class);
if((under != null)) setLocation(getX(),under.getY() - under.getImage().getHeight()-5);
}
}
private void jump()// declares jump method
{
vspeed= (-jumpStrength);
Greenfoot.playSound("jump1.wav");
fall();
}
public boolean onMovingGround()
{
Object under = getOneObjectAtOffset ( 0, getImage().getHeight()/2, MovingGround.class);// checks for ground underneath the bear
return under!= null;
}
public void dead()
{
if (getOneIntersectingObject(Spear.class) !=null)// says what to do if the Spear hits Bob
{
getWorld().addObject(new Death(),getX(),getY()); // adds the death picture effect
Actor Spear = getOneIntersectingObject(Spear.class); // tells when the bear and spear intersect
getWorld().removeObject(Spear); // removes Spear from the world
Actor Death = getOneIntersectingObject(Death.class);
Greenfoot.playSound("death1.wav");// plays the death sound
Greenfoot.delay(30); // delays whats happening so that the death shows
getWorld().addObject(new GameOver(),400,300); //adds GameOver
Greenfoot.delay(100); //Delays Greenfoot
Actor GameOver = getOneIntersectingObject(GameOver.class);
getWorld().removeObject(GameOver);//removes game over
getWorld().removeObject(Death); // removes the death symbol
setLocation(44,546); // puts the bear back at the beggining
Greenfoot.start(); //automaticly starts the game
}
}
public void fell()
{
if(getY()>=getWorld().getHeight()+1)
{
dead();
}
}
public void win()
{
if (getOneIntersectingObject(SealHole.class) !=null)
{
getWorld().addObject(new YouWin(),400,300); //adds the YouWin to the center of the screen
//play sound
getWorld().addObject(new Winner(),400,300);
//greenfoot stop
}
}
}