This site requires JavaScript, please enable it in your browser!
Greenfoot back
sinlessclown
sinlessclown wrote ...

2017/4/14

How to stop my player from being able to jump continuously- platformer

sinlessclown sinlessclown

2017/4/14

#
I'm pretty new to greenfoot and as a assignment, we had to create a game. I wanted to make a platfomer. This is my code, and its pretty generic.
public class Player extends Actor
{
    private int speed = 4;
    private int vSpeed = 1;
    private int acceleration = 1;

    public void act() 
    {
       move();
       checkFall();
    }

    public void move()
    {
       if(Greenfoot.isKeyDown("left"))
        {
            setLocation ( getX() - speed, getY() );
            setImage("run1l.png");
        }
       if(Greenfoot.isKeyDown("right"))
        {
            setLocation ( getX() + speed, getY() );
            setImage("run1r.png");
        }
       if(Greenfoot.isKeyDown("space"))
        {
            jump();
        }
    }

    public void jump()
    {
        vSpeed = -6;
        fall();
    }

    public void fall()
    {
       setLocation ( getX(), getY() + vSpeed ); 
       vSpeed = vSpeed + acceleration;
    }

    public void checkFall()
    {
       if(onGround()) {
        vSpeed = 0;
       }
        else {
              fall();
       }
    }

    public boolean onGround()
    {
       Actor under = getOneObjectAtOffset(0,getImage().getHeight()/2,Platform.class);
       return under != null;
    }
}
I wanted to stop my charachter from being able to jump continously. Please can you explain how this can be done.
danpost danpost

2017/4/14

#
You only want to jump when the player is on the ground. Place that condition on the ability to jump.
You need to login to post a reply.