G'day. I was making a simple game where monkey had to jump on platforms to reach the final destination. however, when i jump , and my monkey touches the platforms above, it directly goes to the above platforms, rather than hitting the platform and falling down again. Can anyone help me with this?
public class Monkey extends Animal
{
private int speed = 2; //movement speed
private int vSpeed = 0; //vertical speed
private int acceleration = 2; //gravity effect while falling
private int jumpStrength = -20;
public void act()
{
checkFall();
jump();
checkKeys();
}
private void checkKeys()
{
if(Greenfoot. isKeyDown("space"))
{
jump();
}
}
public void jump()
{
if (Greenfoot.isKeyDown("space") && onPlatform())
{
vSpeed = jumpStrength;
fall();
}
}
public void fall()
{
setLocation(getX(), getY()+vSpeed);
vSpeed = vSpeed + acceleration;
}
public boolean onPlatform()
{
Actor under = getOneIntersectingObject(Platform.class) ;
return under !=null;
}
public void checkFall()
{
if (onPlatform())
{
vSpeed = 0;
}
else
{
fall();
}
}
}
