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

2013/3/7

My Horrible Jump

al_griff al_griff

2013/3/7

#
Hey Guys, So I'm currently programming a platformer type of game for a major work at school, and I've been able to program a mediocre jump method, but it's absolutely terrible. Basically what happens is, you press the spacebar and then the players Y location should decrease by a set value, and then the player should drop, however, in my code it's telling the player to stay at that increased Y position until the spacebar has been released. I don't really know how to fix this. My code for my jump method is:
public void jump()
    {
        if (Greenfoot.isKeyDown("space")){
            setLocation(getX(), getY() - speed*12);
            Greenfoot.delay(1);
            fall();
// the hSpeed was just to make the player jump forward or backward when necessary 
            if (Greenfoot.isKeyDown("d")) {
                setLocation(getX() + hSpeed, getY() - speed*12);
                Greenfoot.delay(1);
                fall();
            }
            if (Greenfoot.isKeyDown("a")) {
                setLocation(getX() - hSpeed, getY() - speed*12);
                Greenfoot.delay(1);
                fall();
            }
            if (Greenfoot.isKeyDown("shift")) {
                if (Greenfoot.isKeyDown("d")) {
                    setLocation(getX() + hSpeed*4, getY() - speed*16);
                    Greenfoot.delay(1);
                    fall();
                }
                if (Greenfoot.isKeyDown("a")) {
                    setLocation(getX() - hSpeed*4, getY() - speed*16);
                    Greenfoot.delay(1);
                    fall();
                }
            }
        }
danpost danpost

2013/3/7

#
One big problem is the use of the 'delay' statement in the code provided. All code can be created without the use of the 'delay' statement and that statement is mainly used to give the CPU time to process background operations during long foreground processes. The steps of an action, like the 'jump' (or animation), should be created by the use of the built-in cycling of act methods (the repeating of executing all the 'act' method of all active objects -- the active world and all objects within that world). Please refer to the Wombat class of my Scrolling SuperWorld scenario for an example of how to program a jump.
You need to login to post a reply.