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

2021/5/20

Movement issues

Georgegeorge Georgegeorge

2021/5/20

#
I'm wondering if there is any way to make sure when you press down a key to make the action it creates only happens once every time you press it instead of it going on repeat?.
danpost danpost

2021/5/20

#
Track the state of the key with a boolean field:
// field
private boolean keyDown;
private String key = "space"; // or whatever key you like

// in action code
if (keyDown != Greenfoot.isKeyDown(key))
{
    keyDown = ! keyDown;
    if (keyDown) ; // do non-repeatable something
}
Georgegeorge Georgegeorge

2021/5/20

#
thanks
Georgegeorge Georgegeorge

2021/5/20

#
what do you mean by field?
Georgegeorge Georgegeorge

2021/5/20

#
should have specified, I mean where should I put the boolean field in my code because I have it with my ints up the top, I don't feel I have implemented it right as it is not working
Georgegeorge Georgegeorge

2021/5/20

#
ok I've got the code to work now but there is a slight problem it only executes the action once I was wondering if it could be executed once everytime I press the button
HELP HELP

2021/5/20

#
yeah wait a second
HELP HELP

2021/5/20

#
use this if (Greenfoot.isKeyDown("space")) { blablabla }
danpost danpost

2021/5/20

#
Georgegeorge wrote...
ok I've got the code to work now but there is a slight problem it only executes the action once I was wondering if it could be executed once everytime I press the button
That is what it should do. If not working properly, show your class codes.
You need to login to post a reply.