Hi!
I am working on a Jump and Run game now.And I want my actor to jump and then fall down.
I tied this for jump but it does not work like it should.
With this Code I fly when I am pressing space, but it is not like a want, I want it to jump.
Can you help me?
Thanks,
Fifilein
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Spieler here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Spieler extends Actor
{
int acceleration = 2;
int vSpeed = 0;
int speed = 5;
int jumpStrenght = 2;
/**
* Act - do whatever the Spieler wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
// Add your action code here.
//if ( getObjects(GameOver.class).isEmpty() && ! getObjects(Grape.class).isEmpty())
}
public boolean onGround()
{
Actor under = getOneObjectAtOffset(0, getImage().getHeight()/2 + 2, Ground.class);
return under != null;
}
public void Fall()
{
setLocation (getX(), getY() + vSpeed);
vSpeed = vSpeed += acceleration;
}
public void checkFall()
{
if (onGround())
{
vSpeed = 0;}
else {
Fall();
}
}
public void checkKeys()
{
if (Greenfoot.isKeyDown("left"))
{
moveLeft();
}
if(Greenfoot.isKeyDown("right"))
{
moveRight();
}
if(Greenfoot.isKeyDown("space"))
{
jump();
}
}
public void jump()
{
vSpeed = -jumpStrenght;
Fall();
}
public void moveLeft()
{
setLocation(getX() - speed, getY());
}
public void moveRight()
{
setLocation(getX() + speed, getY());
}
}