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

2013/3/7

Make an Actor Jump Higher or Lower

Hey, I'm trying to figure out how to make my actor jump higher or lower, depending on how long you hold the jump button, like in the Mario games. I have jump code, but it only jumps a specific height. Thanks for anyone who can help.
MatheMagician MatheMagician

2013/3/7

#
Could you share the code you currently have so we can change it? A guess is that you are adding a variable to the character's height and then reducing that variable until it starts falling again. You should alter this starting variable in order to change the height.
Sorry, forgot, here's my jumping/moving code: I don't know how I'd change the jump or fall code to jump higher, depending on how long I press the jump button.
    /**
     * Tells the Player what to do
     */
    public void act()
    {
        proccessKeys();
        checkFall();
    }

    /**
     * Sees which keys are pressed, and does an according action.
     * myStats is a GameStats object that contains the keys for moving left,
     * right, and for jumping. These can be changed later. Default is "a", "w", "d"
     */

    public void proccessKeys()
    {
        if (Greenfoot.isKeyDown(myStats.left))
            moveLeft();
        if (Greenfoot.isKeyDown(myStats.right))
            moveRight();
        if (Greenfoot.isKeyDown(myStats.up) && onGround())
            jump();
    }

    /**
     * Makes the Player jump
     */
    public void jump()
    {
        gravity = -15;
    }

    /**
     * Checks if the player is in the air, if so, the player must fall.
     */
    public void checkFall()
    {
        if (!onGround() || gravity < 0)
            fall();
    }

    /**
     * Moves the Player down
     */
    public void fall()
    {
        
        if (gravity < -15)
            gravity = -15;
        
        //MAX_VELOCITY = 5. So it doesn't fall through ground
        if (gravity < MAX_VELOCITY)
            gravity++;

        setLocation(getX(), getY() + gravity);
    }

    /**
     * Checks if the player is on the ground.
     */
    public boolean onGround()
    {
        int height = getImage().getHeight();
        Actor ground = getOneObjectAtOffset(0, height/2, Ground.class);
        if(ground != null)
            return true;   
        return false;  
    }
Nevermind, I figured it out myself!
You need to login to post a reply.