Hi, I am new to Greenfoot and I am creating a platform game. I want the level to restart when my player falls down onto the ground. I tried to do this by adding in the code private boolean atBottom() and private void gameRestart(). When the player touches the ground, nothing happens except when the player touches a specific point, then it restarts. I think the problem is in the private boolean atBottom() but I don't know what I am doing wrong.
This is my code for my player and please note, onGround refers to the platform.
public class Player extends Actor
{
private int vSpeed = 0; //How fast the player falls down. Vertical speed.
private int acceleration = 2; //Acceleration when falling
private boolean jumping; //Keeps track of whether the player is jumping or not
private int jumpStrength = 20; //How high the player jumps
private int speed = 4; //How fast the player will move around
/**
* Act - do whatever the Player wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
checkFall(); //Plays a loop. Checks for ground...
checkKey(); //Ables basic movement
platformAbove(); // Stops the player from jumping through a platform
eat(); //"Eats" the points
}
public void checkKey() //Check the keyboard
{
if(Greenfoot.isKeyDown("up") && jumping == false) // When the up key is pressed the player will jump
{
jump();
}
if(Greenfoot.isKeyDown("right")) //Press the right key to go right
{
moveRight();
}
if(Greenfoot.isKeyDown("left")) // Press the left key to go left
{
moveLeft();
}
}
public void moveRight() //make the player move right
{
setLocation(getX()+speed, getY()); //Where he is on the screen with the y co-ordinate
}
public void moveLeft()
{
setLocation(getX()-speed, getY()); // Same as above except goes in different direction
}
public void fall()
{
setLocation(getX(), getY() +vSpeed); //Checks if player is on the ground
if(vSpeed <=9)
{
vSpeed = vSpeed + acceleration; // falls as fast as the speed combined with the acceleration
if (atBottom())
gameRestart();
}
jumping = true; // Will stop the player from jumping while falling
}
private boolean atBottom() //This method will decide if Player has fallen
{
return getY() >= getWorld().getHeight()-2;
}
private void gameRestart() //Restarts the world
{
Greenfoot.setWorld(new JurrasicPark());
}
public boolean onGround()
{
int spriteHeight = getImage(). getHeight(); //Finds out how big the player is
int lookForGround = (int)(spriteHeight/2) + 5; //Looks for ground 5 by 5 pixels under the player
Actor ground = getOneObjectAtOffset(0, lookForGround, Platform.class);
if(ground == null) // If there is no ground...
{
jumping = true;
return false; //Keeps on falling
}
else
{
moveToGround(ground); //Moves to the top of the platform
return true;
}
}
public boolean platformAbove()
{
int spriteHeight = getImage(). getHeight(); //Finds out how big the player is
int yDistance = (int)(spriteHeight/ -2); //Divide by a negative number so it looks above not below the player
Actor ceiling = getOneObjectAtOffset(0, yDistance, Platform.class); //Looks for y distance above
if(ceiling != null) // If it is not null...
{
vSpeed =1; //Make the player go down
bumpHead(ceiling);
return false; //Keeps on falling
}
else
{
return true;
}
}
public void bumpHead(Actor ceiling) //Stops the player from jumping through a platform
{
int ceilingHeight = ceiling.getImage().getHeight();
int newY = ceiling.getY() + (ceilingHeight + getImage().getHeight())/2;
setLocation(getX(), newY);
}
public void moveToGround(Actor ground) //Makes the player touch the platform on the surface
{
int groundHeight = ground.getImage().getHeight();
int newY = ground.getY() - (groundHeight + getImage().getHeight())/2;
setLocation(getX(), newY);
jumping = false; // Not jumping and touching ground so that he is ready to jump
}
public void checkFall() // This codes checks whether the player is on the ground or not
// If it is then it makes the vertical speed 0 which stops the player from falling. If not then continues to fall
{
if(onGround())
{
vSpeed = 0;
}
else
{
fall();
}
}
public void jump()
{
vSpeed = vSpeed - jumpStrength; //Puts the player in the air
jumping = true; //Checks whether the player is jumping or not
fall(); // Makes the player fall after it has jumped
}
public void eat()
{
Actor point;
point = getOneObjectAtOffset(0, 0, Point.class);
if (point != null)
{
World world;
world = getWorld();
world.removeObject(point);
}
}
}

