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

2011/5/21

Jumping

roccoricciardi roccoricciardi

2011/5/21

#
how might one put a limit on how high an object can jump? aka - if i hold down the "up" key i don't want my actor to soar to the sky
davmac davmac

2011/5/22

#
One simple way is to use a counter to count how long the actor has been jumping for, and if it gets too high, stop moving up. Declare the counter inside your class (not inside the method):
int jumpCounter = 0;
Then, something like:
    if (Greenfoot.isKeyDown("up")) {
        jumpCounter++;
        if (jumpCounter < 20) {
            // move upwards
            ....
        }
    }
This isn't a complete solution, but it should get you started.
roccoricciardi roccoricciardi

2011/5/22

#
thanks! only issue with that is that jumpCounter remains at 20 even after the key is released, so a second keyDown check such as the following is needed to reset the count so that the actor could jump later if(!(Greenfoot.isKeyDown("w"))) jumpCount=0;
You need to login to post a reply.