|
-
Trainee coder
[RESOLVED] [HELP][AS2] Detecting Keys
Alrighty.. So I'm working on this very small project, but I've run into some trouble surprisingly very early in.
The game is a basic platformer. To detect the key strokes I'm using the good'ol (or so I thought) Key.addListener method.
MY PROBLEM: if I hold the right key down and then tap the up key, the listener forgets about the right key still being held down, (and upon the release of the up key while the right key IS STILL DOWN) the onKeyUp function activates. Because of this, the listener ignores the right key which IS STILL being held down, and thinks that since it recorded the up key being released that no other keys are being held down.
I realise that this is the nature of using a listener, but for reasons I'm not going to go into detail about I'm not going to ditch this method and use Key.isDown instead. So yeah, it'd sweet if someone could suggest a fix for this, even if it is a lil’ hacky.
Here's the relevant code:
PHP Code:
var keyListener:Object = new Object(); keyListener.onKeyDown = function() { keyDown = getKey(); moveChar(keyDown); }; keyListener.onKeyUp = function() { if(getKey()!="UP") { keyDown = " "; } }; //add the mouse listener Key.addListener(keyListener);
function getKey ():String { var theKey:String; switch (Key.getCode()) { case Key.LEFT : theKey = "LEFT"; break; case Key.RIGHT : theKey = "RIGHT"; break; case Key.UP : theKey = "UP"; jumpChar(); break; case Key.DOWN : theKey = "DOWN"; break; default : theKey = chr(Key.getAscii()); } trace(theKey); return theKey; };
Thanks in advance for any helpful suggestions,
Viza.
-
Trainee coder
-
Pumpkin Carving 2008
getCode returns the last key pressed which in your case is the up. Restructure your code by not using a Switch. Stick with the "good-ol" if.
The 'Boose':
ASUS Sabertooth P67 TUF
Intel Core i7-2600K Quad-Core Sandy Bridge 3.4GHz Overclocked to 4.2GHz
8GB G.Skill Ripjaws 1600 DDR3
ASUS ENGTX550 TI DC/DI/1GD5 GeForce GTX 550 Ti (Fermi) 1GB 1GDDR5 (Overclocked to 1.1GHz)
New addition: OCZ Vertex 240GB SATA III SSD
WEI Score: 7.6
-
Heli Attack!
Look how people have implemented Key.isDown is AS3, essentially you need to do that. (create an array of booleans, and turn it on when the keyDown is detected, and turn it off when the keyUp is detected)
-
Trainee coder
Hey mate, thanks once again to help me out.
I'm not too sure what you meant by using if statements instead of a switch.. It's practically the same, and I don't see how that could possibly be a solution. I tried it anyway:
PHP Code:
function getKey ():String { var theKey:String; if (Key.getCode()==Key.LEFT) { theKey = "LEFT"; } if (Key.getCode()==Key.RIGHT) { theKey = "RIGHT"; } if (Key.getCode()==Key.UP) { //theKey = "UP"; jumpChar(); } if (Key.getCode()==Key.DOWN) { theKey = "DOWN"; } trace(theKey); return theKey; };
..Is that what you meant? 'Cause it didn't work. 
I can understand why it would be doing this. As you said, geCode() returns the last key pressed. I'm just wanting to find a solution. Is it possible to track two keys at once??
Any further help on this would be greatly appreciated. 
Viza.
EDIT: Just saw your post then Iopred. Ok, that sounds good. Thanks a lot, when I get some free time I'll do as you suggested (lesli Felix posted up some AS3.0 key detection, right?).
Cheers.
Last edited by Viza; 09-27-2008 at 06:16 AM.
-
Pumpkin Carving 2008
Yeah, don't really know why I suggested that. Using arrays might be another open and you'd have to cycle through it, but I still feel it would be easier to write listeners for each key instead of trying to encompass all the keys with a single method, but maybe it's me be bull-headed or just loving to do it with brute force.
The 'Boose':
ASUS Sabertooth P67 TUF
Intel Core i7-2600K Quad-Core Sandy Bridge 3.4GHz Overclocked to 4.2GHz
8GB G.Skill Ripjaws 1600 DDR3
ASUS ENGTX550 TI DC/DI/1GD5 GeForce GTX 550 Ti (Fermi) 1GB 1GDDR5 (Overclocked to 1.1GHz)
New addition: OCZ Vertex 240GB SATA III SSD
WEI Score: 7.6
-
Hype over content...
The actual logic is broken mate.
If you press right then theKey = "RIGHT"; but if you press down at the same time then theKey = "DOWN"; so it wipes out the fact that you're still pressing the right arrow.
Either have theKeyHorizontal and the theKeyVertical vars or store all the key presses in an array and check that when the event is fired.
Squize.
-
instead of using listeners, the only robust way I found is to check Key.isDown() "on enterFrame":
Code:
pressedKeys = new Array();
this.onEnterFrame = function()
{
checkKey(Key.LEFT);
checkKey(Key.RIGHT);
// etc..
}
// this function will fire onKeyPressed(code) or onKeyUp(code) whenever <code> is pressed or released.
function checkKey(keyCode)
if (Key.isDown(keyCode))
{
if (!pressedKeys[keyCode]) onKeyPressed(keyCode);
pressedKeys[keyCode] = true;
} else
{
if (pressedKeys[keyCode]) onKeyUp(keyCode);
pressedKeys[keyCode] = false;
}
}
(note: getCode() only records the latest pressed key, then it's not recommended to use getCode() to know which key was released)
-
Trainee coder
Hey, thanks to everyone who put in their advice to help me out. Just before I left for a small trip (with no pc acess - which is why I haven't marked this as resolved yet) I fixed it with a pretty simple solution actually (pretty similiar to what Squize suggested).
Squize, you are completely right. I realised the code was doing exactly what it was supposed to be doing, I just wanted a way to change what I already had to do something it wasn't quite meant to do (in a hacky sense, if you get what i mean?).. Anyway, everything's working fine now. Thanks again to everyone who put their suggestions forward. Hopefully I'll get a very small demo of this mini-game up in a week or so.
Viza.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|