Im having trouble with my jumping game
my actor that jumps, everytime i hold the space key it just skyrockets up instead of only jumping once.
my other problem is that my actor falls through the ground, and does not stay on the ground
here is the code im using:
im new so go easy on me
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* A small demo of a jumping movement.
*
* @author M K�lling
* @version 1.0
*/
public class Hobbes extends Actor
{
private int speed = 7; // running speed (sideways)
private int vSpeed = 0;
private int acceleration = 2;
private int jumpStrength = 12;
private boolean jumping = false;
/**
* Check keyboard input and act accordingly
*/
public void act()
{
checkKeys();
checkFall();
if (Greenfoot.isKeyDown("space")&& jumping==false )
{
jump();
}
}
private void checkKeys()
{
if (Greenfoot.isKeyDown("left") )
{
moveLeft();
}
if (Greenfoot.isKeyDown("right") )
{
moveRight();
}
if (Greenfoot.isKeyDown("space") )
{
jump();
}
}
public void jump()
{
jumping = true;
vSpeed = -jumpStrength;
fall();
}
public void checkFall()
{
if(onGround()) {
vSpeed = 0;
jumping = false;
}
else {
fall();
}
}
public boolean onGround()
{
Ground under = null;
int counter = 1;
int max = vSpeed;
//int variance;
while (counter <= max && under == null)
{
under = (Ground)getOneObjectAtOffset ( 0, getImage().getHeight() / 2 + counter, Ground.class);
counter++;
}
// If there is a platform, correct Pengu's height so that he always lands right on the platform
// This avoids a wierd floating effect that was present otherwise
if (under != null)
{
int newY;
newY = under.getY() - (under.getImage().getHeight()/2) - ((getImage().getHeight() / 2))-1;
setLocation(getX(), newY);
}
return under != null;
}
public void fall()
{
setLocation ( getX(), getY() + vSpeed);
vSpeed = vSpeed + acceleration;
}
public void moveRight()
{
setLocation ( getX() + speed, getY() );
}
public void moveLeft()
{
setLocation ( getX() - speed, getY() );
}

