|
-
Zombie Coder
Key Listener
Been a while, greetings,
AS3 the key listener is odd . It fires off one event,waits an interval, then proceeds to fire off events every few milliseconds instead of instantly. Why Adobe did it this way I can't imagine but anyway- it's making it damn hard to code games that require tight arcade controls.
Does anyone know a hack for this?
I know you could do say set a variable when the listeners are first fired :
Key_downListener-
key_up=true;
Key_upListener-
key_down=false;
This will work fine for say, moving a character in 8 directions.
But what I need is to time between the same key being pressed twice:
keys[ev.keyCode].now = getTimer() - keys[ev.keyCode].then;
keys[ev.keyCode].then = getTimer();
Imagine a combo like situation in a fighting game. tap tap tap, if the key registers a 0.05 second pause it combo's- if between it's 1second, don't combo and only play the first attack in the combo link array.
Impossible with the AS3 Listener, it registers unevenly if you hold the key down, therefore the timer won't work properly. the code in the EVENT_FRAME has gone through 12 or so passes after the first pause.
Anyway, not sure if anyone grasps what I'm on about, it's hard to explain and I'm sure I'm not making sense here.
Thanks if you can help.
Kris
-
That's not flash or as3's fault, that's an operating system level behavior. Try it in notepad or the reply window here. Hold down a key and watch the letter show up, pause, then show up a lot in rapid succession. The OS is actually sending the key events as they are detected.
-
Then you should be needing somehing like this:
1. Create and object with all you keys as Boolean, so you can check if item is down or not:
Actionscript Code:
class KeyboarObject
public var A : Boolean = false; public var B : Boolean = false; public var C : Boolean = false;
etc...
2. then add the eventListener for key handling.
Actionscript Code:
stage.addEventListener ( KeyboardEvent.KEY_DOWN, this.onKeyDownHandler ); stage.addEventListener ( KeyboardEvent.KEY_UP, this.onKeyDownHandler );
onKeyDownHandler ( e : KeyboardEvent ) : void { switch ( e.keyCode ) { case 65 : if ( keyBoardObject.A == false ) { keyBoardObject.A = true; this.keyWasClicked( 'A' ); } break; } }
onKeyDownHandler ( e : onKeyDownHandler ) : void { switch ( e.keyCode ) { case 65 : keyBoardObject.A = false; break; } }
keyWasClicked ( value : String ) { // do what ever you need with the key. ;) }
In this case you will be able to track all the keys which were clicked, but not pressed.
p.s: you can do some better logics handling. beacause this one is tno the fastes one
--
there is a place for those who dare to dream...
Flash Developer
VISTAPARK GMBH
BÄRENSTRASSE 11-13
D-42117 WUPPERTAL
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
|