View Full Version : A better wat of capturing keystrokes
nih
February 18th, 2005, 06:15 PM
Hello!
I'm a bit of a flash noob, and I'm having a few issues with keypresses being over-captured. In this case if the movie runs slowly for any reason, keypresses are being queued up and then executed for quite some time after the key was released. This introduces a really nasty laggy feeling. You can try it here:
http://darken.spark.lithotech.co.nz/pier_island_02.zip
It's a zipped projector file. You need to have an athlon 2600 or less, or just run another SWF in the background to see the effect.
This is how I currently capture keypresses:
if (Key.isDown(Key.RIGHT)) {
if (_root.world_rotate_velocity > (_root.max_rotate_velocity * -1)) {
_root.world_rotate_velocity -= _root.rotate_acceleration;
}
}
Does anyone have a better technique for capturing keypresses?
nih
February 18th, 2005, 06:19 PM
Second question split off and moved to: http://www.kirupa.com/forum/showthread.php?t=86237
Marz
February 18th, 2005, 06:29 PM
You could use a KeyListener to capture keystrokes. I've never really dabbled with it that much, but then again I haven't bothered working with it as much as other methods. Here is a possible piece of code that might work [Note : This code has not been tested yet]
myKeys = new Object();
myKeys.onKeyDown = function ()
{
// the key is down
}
myKeys.onKeyUp = function ()
{
// the key was released
}
Key.addListener(myKeys);
Allright, see if this works for you man.
[Note : You should only have to place this in one frame.. whether it's an onLoad frame or what. It shouldn't be needed in an OnEnterFrame]
nih
February 18th, 2005, 06:39 PM
Cheers dude, I'll try this now :D
nih
February 18th, 2005, 06:50 PM
Worked beautifully! Thanks Marz!
I had a good look around the net but never managed to impress upon myself the sheer usefulness of a key listener over just capturing the key per frame. Perhaps this could be enforced in a tutorial on Kirupa.
Thanks again!
nih
February 18th, 2005, 06:58 PM
Here's the whole thing, in case someone needs a working example:
// Key listeners
myKeys = new Object();
myKeys.onKeyDown = function ()
{
// Keycodes, for if you want to do it like: Key.getCode() == 38
//up 38
//down 40
//left 37
//right 39
if (Key.getCode() == Key.RIGHT) {
if (_root.world_rotate_velocity > (_root.max_rotate_velocity * -1)) {
_root.world_rotate_velocity -= _root.rotate_acceleration;
_root.rotation_change = 1;
}
}
if (Key.getCode() == Key.LEFT) {
if (_root.world_rotate_velocity < _root.max_rotate_velocity) {
_root.world_rotate_velocity += _root.rotate_acceleration;
_root.rotation_change = 1;
}
}
if (Key.getCode() == Key.UP) {
if (_root.world_velocity < _root.max_velocity) {
_root.world_velocity += _root.acceleration;
_root.velocity_change = 1;
}
}
if (Key.getCode() == Key.DOWN) {
if (_root.world_velocity > (_root.max_velocity * -1)) {
_root.world_velocity -= _root.acceleration;
_root.velocity_change = 1;
}
}
}
myKeys.onKeyUp = function () {
if (Key.getCode() == Key.LEFT) _root.rotation_change = 0;
if (Key.getCode() == Key.RIGHT) _root.rotation_change = 0;
if (Key.getCode() == Key.UP) _root.velocity_change = 0;
if (Key.getCode() == Key.DOWN) _root.velocity_change = 0;
}
Key.addListener(myKeys);
Copes
February 18th, 2005, 07:33 PM
hmm im having problems when it comes to the key up section of my code, if i have say a "use" action on the ALT key and press it while im moving with my direction keys, the on key UP code doesnt seem to work properly anymore for stopping the walking animation, and he walks on the spot so to speak.
here is examples of the two bits of code im using.
if (Key.isDown(Key.RIGHT) and this._x<535) {
this._x += _root.moveSpeed;
this.gotoAndStop(6);
onClipEvent (keyUp) {
if (Key.getCode() == Key.RIGHT) {
gotoAndStop(2);
How could i get around this problem, and would the method above be a better one than this current one to use?
Copes
nih
February 18th, 2005, 08:57 PM
From my search I suspect there's no keyUp method. (not including the one mars mentions)
Use the example Marz gave above - it works well, and more like you'd expect keystrokes to be responded to.
nih
February 18th, 2005, 10:58 PM
I've sobered up enough to notice that there's a significant delay when you hold a key down.
The character makes one move forward, pauses, then continues moving. Same for rotation. I can't find any reason for this in the script unless it's the actual mechanics of the flash movie in action, such as update order (again).
To me it seems more like the delay you get when holding down a key before it repeats. Try it now in a reply dialogue.
I was testing for keyup before applying any movement decay originally but now I'm a bit less drunk I'll try that again as a way to smooth out the delay.
Marz
February 19th, 2005, 03:05 AM
I'm glad that worked out for you bro. Hopefully you can get the drag bull**** working better for you.
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.