I want that when I hold the buttum that I only jump once and not always. So that I must press "up" always when I want to jump.
That is the "Mans.class" code.
import greenfoot.*;
public class Mans extends Actor
{
public int ySpeed;
public int groundLevel=489;
private boolean pressed;
public void act()
{
boolean onGround = false;
ySpeed++;
setLocation(getX(), getY()+ySpeed);
// at ground level
if (getY() > groundLevel)
{
ySpeed = 0;
setLocation(getX(), groundLevel);
onGround = true;
}
// hitting box
Actor object = getOneIntersectingObject(Objects.class);
if (object != null)
{
int yDir = (int)Math.signum(ySpeed);
setLocation(getX(), object.getY()-yDir*((object.getImage().getHeight()+getImage().getHeight())/2+1));
ySpeed = 0;
if (yDir > 0) onGround = true;
}
// jumping
if (onGround && Greenfoot.isKeyDown("up")) ySpeed = -15;
// firing weapon
if (pressed == Greenfoot.isKeyDown("space"))
{
pressed = !pressed;
if (pressed) getWorld().addObject(new Weapons(), getX()+80, getY()-25);
}
//die
if (this.isAtEdge())
{ this.removeTouching(Objects.class);
this.setImage("Mans3.png");
this.setLocation(400, 400);
Greenfoot.playSound("game-over.wav");
Greenfoot.stop();
MyWorld m = (MyWorld) getWorld();
m.GameOver();
}
}
}
