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

2015/5/26

Need to only call the act method once

someone1 someone1

2015/5/26

#
public final String  UP = "up";    
public final String  DOWN = "down";    
public final String  LEFT = "left";    
public final String  RIGHT = "right";        

private boolean keyPressed = false;
    /**
     * Act method
     * /
      public void act()
    {
       if(!keyPressed && Greenfoot.isKeyDown("right")) {
            keyPressed = true;
            move(1);
       }    
I want my character too only move continuously when the right key is pressed and not move at the start of the game, but this code does not do anything when i press the right key
Super_Hippo Super_Hippo

2015/5/26

#
If this is all code, then the object will move one cell when the right key is pressed. I don't know why you only want the act method to be called once. If you want it to move when is the right key is down, do this:
if (Greenfoot.isKeyDown("right")) move(1);
If you want it to start moving when the right key was pressed, do this:
if (keyPressed) move(1);
else
{
    if (Greenfoot.isKeyDown("right"))
    {
        keyPressed = true;
        move(1);
    }
}
someone1 someone1

2015/5/28

#
What i mean is that i want too press the right key once and have my character continously move
someone1 someone1

2015/5/28

#
I just tried it out thnx soo much
You need to login to post a reply.