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

2016/8/25

My character flying instead of jumping

Boboy18 Boboy18

2016/8/25

#
public class Player extends Charcters
{
    private int speed = 7;
    private int vSpeed = 0;
    private int acceleration = 1;
    private int jumpStrength = 7;

    public void act() 
    {
        controls();
        checkFall();
    }    
    
    public void controls()
    {
        if(Greenfoot.isKeyDown("left"))
        {
            moveLeft();
        }
        if(Greenfoot.isKeyDown("right"))
        {
            moveRight();
        }
        if(Greenfoot.isKeyDown("space"))
        {
            jump();
        }
        if(Greenfoot.isKeyDown("up"))
        {
            jump();
        }
    }
    
    public void moveLeft()
    {
        setLocation(getX() - speed, getY());
    }
    
    public void moveRight()
    {
        setLocation(getX() + speed, getY());
    }
    
    public void jump()
    {
        vSpeed = -jumpStrength;
        fall();
    }
    
    public void checkFall()
    {
        if(onGround()) {
            vSpeed = 0; 
        }
        else {
            fall();
        }
    }
    
    public boolean onGround()
    {
        Actor under = getOneObjectAtOffset (0, getImage().getHeight() / 2, Ground.class);
        return under != null;
    }
    
    public void fall()
    {
        setLocation ( getX(), getY() + vSpeed);
        vSpeed = vSpeed + acceleration;
    }
}
Got the jumping code from videos (Joy of Code) and tried other methods. I still don't understand why it doesn't jump like a regular person?
fejfo fejfo

2016/8/25

#
After a quick look trough it seems like the problem is that you don't check if the player is on the ground before increasing his vSpeed
Boboy18 Boboy18

2016/8/27

#
I thought my "checkFall" would do that. Anyways how can I fix this?
danpost danpost

2016/8/27

#
Boboy18 wrote...
I thought my "checkFall" would do that. Anyways how can I fix this?
Use the combination (1) if on ground AND (2) if jump key down in the controls method.
Boboy18 Boboy18

2016/8/30

#
Sorry danpost I don't understand what your saying...poor english.
danpost danpost

2016/8/30

#
Boboy18 wrote...
Sorry danpost I don't understand what your saying...poor english.
Replace lines 24 through 31 with:
if (onGround() && (Greenfoot.isKeyDown("space") || Greenfoot.isKeyDown("up")))
{
    jump();
}
Boboy18 Boboy18

2016/9/3

#
It worked thank you. I have an idea for my game but don't know how to do it. My idea is that I want a menu screen that you can choose the level you can play.
Super_Hippo Super_Hippo

2016/9/3

#
You can create an Actor subclass called 'Button', add these buttons to the menu world and pass a variable to them. Then you check if they were clicked and set the world based on the variable then. Or you let the user press a number and set the world based on that.
You need to login to post a reply.