-
KEY_DOWN Problems
Hey everyone! I have a virtual joystick that I am making for one of my games, and I'm having a problem with the KEY_DOWN command. Here's my code so far.
Code:
stage.addEventListener(KeyboardEvent.KEY_DOWN, joyKeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, joyKeyUp);
function joyKeyDown(joyEventGo:KeyboardEvent):void {
if (joyEventGo.keyCode==Keyboard.LEFT) {
joystick.gotoAndPlay(2);
}
}
function joyKeyUp(joyEventStop:KeyboardEvent):void {
if (joyEventStop.keyCode==Keyboard.LEFT) {
joystick.gotoAndPlay(1);
}
}
My problem is that I cant seem to get it to stop looping. I have a joystick in normal position in frame 1 of the joystick MC, and other positions in the other 4 frames.
Here's what my code makes the joystick do.
\ / \ / \ / \ / \ / \ / \ / \
it flashes back and forth. I cant get it to stop pinging the function for gotoAndPlay, so it wont stop going to the right position.
Can someone please fix my code?
Thanks, Xen Element
-
Have you tried gotoAndStop()?
-
Wow. It didn't work before, but I just tried it now and it worked! Thanks so much!
-
The answer was, gotoAndStop(), because the stop part does not override the stop already in the MC frame like the gotoAndPlay() did.
Thank you very much, I can't believe I didn't see this before!
-
You probably want gotoAndStop instead of gotoAndPlay.
Also, if you are holding down your key, then the operating system will start sending repeated keypresses after a short delay. You could remove the listener until the KeyUp is detected.
Edit:
I'm slow on the draw today.