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

2014/10/26

Walking and jumping

JeppDev JeppDev

2014/10/26

#
Hello I have the problem where my walk and my jump metods is conflicting so that only one of them can work at a time and i can't seem to figure out where the problem is. This is my code
import greenfoot.*;


public class Jonas extends Actor
{
    int Imagecycle = 1;
    int lastKey =0;
    private int ySpeed;
    /**
     * Act - do whatever the Jonas wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        Jump();
        Walk();
        Image();
    }    

    public void Walk()
    {
        String key = Greenfoot.getKey();
        if("right".equals(key) && lastKey <= 0)
        {
            Background background = (Background) getWorld();
            background.drawBackgroundImage();
            Imagecycle ++;
            lastKey = 1;
        }
        if("left".equals(key) && lastKey >= 1)
        {
            Background background = (Background) getWorld();
            background.drawBackgroundImage();
            Imagecycle ++;
            lastKey = 0;
        }
    }
    
    public void Jump()  
    {  
        int groundLevel = getWorld().getHeight() - getImage().getHeight()/2;  
        boolean onGround = (getY() == groundLevel);  
        if (!onGround) // in middle of jump  
        {  
            ySpeed++; // adds gravity effect  
            setLocation(getX(), getY()+ySpeed); // fall (rising slower or falling faster)  
            if (getY()>=groundLevel) // has landed (reached ground level)  
            {  
                setLocation(getX(), groundLevel); // set on ground  
                Greenfoot.getKey(); // clears any key pressed during jump  
           }  
        }  
        else // on ground  
        {  
            if ("space".equals(Greenfoot.getKey())) // jump key detected  
            {  
                ySpeed = -15; // add jump speed  
                setLocation(getX(), getY()+ySpeed); // leave ground  
            }  
        }  
    }  
Please if someone know the problem I would really appreciate the help
BenMcBen BenMcBen

2014/10/26

#
Hi there, JepDev! Your problem is probably that you're using Greenfoot.getKey(). Your friend Jonas can't Jump() while Walk()ing, because Greenfoot.getKey() can only return one key per frame. So, if you press left, and Greenfoot detects that you pressed left, and you ask it, "Which key did I press?" It'll say you pressed left. And then, later, in the same call to the act() method, if you ask it again, "Which key did I press?" It'll still say you pressed left, because it can't process key presses while it's busy running your code. If you pressed space in-between calling Walk() and calling Jump(), it's not going to catch it, because it hasn't had time to process any key strokes between those two method calls. It process key strokes *between* calls to act(), but not *during* them. The solution: Greenfoot.isKeyDown(). Instead of asking Greenfoot, "Which key did I press?" You can ask it, "Did I press this key?" That way, if you're pressing multiple keys, you can ask it, "Did I press left?" And it will say, yes, you did. And then you can *also* ask it, "Did I press space?" And it will say, yes, you *also* pressed space. Here's an example of how you could use Greenfoot.isKeyDown() in your scenario:
public void Walk() {
  if (Greenfoot.isKeyDown("left")) {
    // do the walk-lefty thing
  }
  if (Greenfoot.isKeyDown("right")) {
    // do the walk-righty thing
  }
}
public void Jump() {
  // do things...
  if (Greenfoot.isKeyDown("space")) {
    // do the jumpy thing
  }
  // more things...
}
Cheers!
JeppDev JeppDev

2014/10/26

#
Thank you very much, I got it to work now, but I only use the isKeyDown in the jump because in the left and right I have made it so that you press left and then right and then left to walk and if I use the isKeyDown, then I can just hold them down
danpost danpost

2014/10/26

#
@BenMcBen, that is not quite how it would work out when calling 'getKey' multiple times. Keystrokes are temporarily saved in a buffer. Think of it as a list where each keystroke value is added to the end of. If the list is empty, 'null' would be returned by 'getKey'. If not, the first item in the list is returned AND also removed from the list. So, any successive call to 'getKey' would return 'null', unless there has been enough time between calls that more than one keystroke could be made -- in which case, the second keystroke would be returned on the second call, etc.
BenMcBen BenMcBen

2014/10/26

#
danpost wrote...
@BenMcBen, that is not quite how it would work out when calling 'getKey' multiple times. Keystrokes are temporarily saved in a buffer. Think of it as a list where each keystroke value is added to the end of. If the list is empty, 'null' would be returned by 'getKey'. If not, the first item in the list is returned AND also removed from the list. So, any successive call to 'getKey' would return 'null', unless there has been enough time between calls that more than one keystroke could be made -- in which case, the second keystroke would be returned on the second call, etc.
Oh I see o: Well color me informed! Thanks for the correction.
You need to login to post a reply.