flash actionscript:
Hi all- Maybe I'm missing something obvious, but I can't quite figure this one out. Is there any way to check and see if a specific key has been relased? I'm working on a game that currently works great with a mouse, but I'm trying to convert it so that it works with keyboard as well. Right now I have a function that is called by an onMouseDown event, and then a separate function called on the onMouseUp event. I'd like to do the same thing with the keyboard, and I've come very close through a few different ways, but nothing quite gets me there. Make sense? Let me know if you need more info... The "ideal" code would go something like this: onKeyDown(Key.SPACE) { do something ONCE; } onKeyUP(Key.SPACE) { do something else ONCE; }
: lo = new Object(); lo.onKeyDown = function() { if (Key.isDown(Key.SPACE)&&!repeat) { downBool = true; repeat=1; } else { downBool = false; } }; lo.onKeyUp = function() { repeat=0; if (downBool) { trace("UP"); } }; Key.addListener(lo);
Thanks kglad. That seems close. I'll have to try and plug in something like that. At a glance, though, I'm not seeing how that would translate to multiple keys. Would you recommend a separate object for each key to be checked? Otherwise, every time any key was released the repeat var would be set to 0. Or does each key to be checked require its own two variables, such as bolSpaceRepeat and bolSpaceDown? You follow? Thanks, SyddyS
Hmmm... just ran a test on that code, and while it works if you quickly press and release the Spacebar, if you hold it down for a moment the Up function isn't triggered. Is this related to the Flash 8 bug/feature I've read about regarding the onKeyUp function?
what's wrong with: lo = new Object(); lo.onKeyDown = function() { keyAscii = Key.getAscii(); }; lo.onKeyUp = function() { trace(keyAscii); };
Thanks for working with me on this! Running your code yields the following problem, which is actually similar to where I got stuck in my own code and came here. Press and release A key, traces 97 Press and release B key, traces 98 Press and hold A key, then press and hold B key, then release A key and you get... TADA! Nothing! Until you release the B key which then traces 98. Grrr... Maybe it would be helpful to know what I'm trying to do. I have a guy that moves back and forth controlled by the left and right arrow keys. He throws a ball- but pressing the spacebar increments a power variable, and releasing it throws the ball with the distance determined by the power variable. So... using the code above if you start to "power up" with the spacebar, then start moving left, then release the spacebar- nothing happens. This process works great with the onMouseDown and onMouseUP, but unfortunately I need to convert this to keyboard for this particular application. Thanks again for any insight you or anyone else might have! -SyddyS
you aren't going to be able to use key listeners for this because the onKeyDown only detects the most recent key pressed and the onKeyUp only executes when all keys are up (and so can be used to detect the last key released). you'll need to use a loop that repeatedly checks which keys are pressed. you'll need to track all key presses and use that information to determine when a key is released: ie, it was detected by your loop and then it is not detected.
yes, it is going to require storing (say the ascii) value of the keys pressed on each loop and then checking if any of the previously pressed keys are not being pressed. i think i would use an associative array to track the keys pressed on any given loop. that would make it easy to check on the next loop if those keys are still being pressed and it would make it easy to adjust the array when the key presses change.
Don't see what you're looking for? Try a search.
|