Is it possible to make an actor jump but instead of going up and falling, you jump down and fall up?
import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot)
/**
* The class Mover provides some basic movement methods. Use this as a superclass
* for other actors that should be able to move left and right, jump up and fall
* down.
*/
public class Mover extends Actor
{
private static final int acceleration = 2; // down (gravity)
private static final int speed = 7; // running speed (sideways)
private int vSpeed = 1; // current vertical speed
public void moveRight()
{
setLocation ( getX() + speed, getY() );
}
public void moveLeft()
{
setLocation ( getX() - speed, getY() );
}
public boolean onGround()
{
Object under = getOneObjectAtOffset(0, getImage().getHeight()/2-8 , null);
return under != null;
}
public void setVSpeed(int speed)
{
vSpeed = speed;
}
public void fall()
{
setLocation ( getX(), getY() + vSpeed);
vSpeed = vSpeed - acceleration;
if ( atBottom() )
gameEnd();
}
private boolean atBottom()
{
return getY() >= getWorld().getHeight() + 2;
}
private void gameEnd()
{
Greenfoot.stop();
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot)
/**
* A little penguin that wants to get to the other side.
*/
public class Pengu extends Mover
{
private static final int jumpStrength = 16;
public void act()
{
checkKeys();
checkFall();
}
private void checkKeys()
{
if (Greenfoot.isKeyDown("left") )
{
setImage("pengu-left.png");
moveLeft();
}
if (Greenfoot.isKeyDown("right") )
{
setImage("pengu-right.png");
moveRight();
}
if (Greenfoot.isKeyDown("space") )
{
if (onGround())
jump();
}
}
private void jump()
{
setVSpeed(-jumpStrength);
fall();
}
private void checkFall()
{
if (onGround())
{
setVSpeed(0);
}
else
{
fall();
}
}
}