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

2014/3/28

move just one time

fabio-mateus fabio-mateus

2014/3/28

#
hello guys, i need some help... do you know any way to do a move() function that only move one time per click and not one time per act?
danpost danpost

2014/3/28

#
Put the condition (using an 'if' statement) on the call to 'move'.
1
if (Greenfoot.mouseClicked(this)) move();
fabio-mateus fabio-mateus

2014/3/29

#
yh i had alreay do that, but with the keyboard... "left" to move left and "right" to move right but the problem is that i want that the object move 50 cells per move, but with the method: isKeyDown always that i press the keyboard the objets move 1 time per act and not 1 time per click
erdelf erdelf

2014/3/29

#
with isKeyDown it would be difficult try with getKey
fabio-mateus fabio-mateus

2014/3/30

#
yh, erdelf, i had done that already too, but dont work becouse i'm using the method isKeyDown to fire...
danpost danpost

2014/3/30

#
You can use 'isKeyDown' with a boolean to track the state of the keys:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// instance field
private boolean dirKeyDown; // direction keys state
// code
if (!dirKeyDown)
{
    if (Greenfoot.isKeyDown("right"))
    {
        move(1);
        dirKeyDown = true;
    }
    if (Greenfoot.isKeyDown("left"))
    {
        move(-1);
        dirKeyDown = true;
    }
}
else if (!Greenfoot.isKeyDown("right") && !Greenfoot.isKeyDown("left")) dirKeyDown = false;
The else part waits for both direction keys to be not in a pressed state before resetting the field.
fabio-mateus fabio-mateus

2014/3/31

#
works great thank you man:)
You need to login to post a reply.