Noticed that the Key.isDown()-thingy doesn't work anymore, so I thought we had to use events for this. However, that doesn't seem to work aswell for me?
Yeah, I've had the same problem. I didn't look into it, but maybe it only works after you added the DisplayObject to the displaylist (just a wild guess).
But I ended up using stage.addEventListener which works perfectly. However, if anyone gets to the bottom of this, keep us posted! (I might experiment with it a bit when I find the time).
Well, by adding an object to the displaylist I mean that it has a parent.
For instance : root.addChild(someObject) - this would add someObject to root's displaylist.
But still I'd like to have this work in the class itself
What I do is I pass a reference to the stage to my objects (unless they have direct access to it, like a displayobject that has been parented), and then do stage.addEventListener() from within my object.
But the reason I haven't looked into this any further is that I made a Key-class myself with an old-school static isDown() function, and I use this approach in it. All the rest of my code just uses this Key-class, and I don't have to worry about it anymore
shouldn't the listeners work on any object?
Well, no, they shouldn't work on any object, but they should on objects that extend displayobject. However, I have a feeling that internally, these keyevent's also use the stage's events, and if you haven't parented your displayobject, it's stage property is null, so it doesn't work. However, that's just an un-educated guess, lol.
Edit:
What I do now is the folowing:
Create an empty array.
Create an keyDown event, that'll set the index of the keyCode on true.
Create an keyUp event, that'll set the index of the keyCode on false.
Create an onEnterFrame event, that'll check if an certain index is true or false.
However, this only works while pressing only 1 key..
Didn't notice any problems with multiple keypresses. Don't see why it should be a problem - haven't tried it in a fast action game though, so it's possible that there is a problem. But I'm pretty sure there isn't
Event listeners will only be triggered if the object is in focus. Movieclips/Sprites can not be easily focused. You would have to do something like stage.focus = myMovieClip; but the result doesn't look too good.
Try adding a key listener to a text field. The listener will only be triggered if the text field is in focus. If you click outside the text field, it is no longer in focus and the listener will not be triggered.
Most of the time, you will want to globally listen for events using stage.addEventListener like Fall_X said. This way all key presses will trigger the event.
Last edited by jason merchant; 08-02-2006 at 01:29 PM.
I just noticed a problem with this method : if you click another window, and release the key there, flash doesn't dispatch the keyUp event.
My solution is to use the Stage's deactivate event to reset my arrays.
Using stage.addEventListener i found a little delay,i wonder if its just my computer
I mean, when you press > and keep pressing, it seems like the player waits a little to do the thing
Its not such a big wait, but in a game its important