answered question
0
Votes
Votes
2
Answers
Answers
M$1.00
FLASH AS3: Help me determine why this code isn't accepting keystrokes
My code accepts keystrokes as long as I am in debugging mode, but if do a straight test, it stops accepting keystrokes and I never get a "listening!" message. Any ideas why?
Output is:
[object Stage]
listening?
listening! (ONLY IN DEBUGGING MODE)
65 (ONLY IN DEBUGGING MODE)
...
import flash.events.KeyboardEvent
import flash.events.Event
import flash.ui.Keyboard;
import flash.display.Stage
...
public function Start()
{
trace(stage);
this.stage.addEventListener(KeyboardEvent.KEY_DOWN, KeyPressed);
trace("listening?");
this.addEventListener(Event.ENTER_FRAME, DoFrame);
}
private function KeyPressed(eventx:KeyboardEvent):void
{
trace("listening!");
LastGuess=eventx.keyCode;
trace(LastGuess);
}
Output is:
[object Stage]
listening?
listening! (ONLY IN DEBUGGING MODE)
65 (ONLY IN DEBUGGING MODE)
...
import flash.events.KeyboardEvent
import flash.events.Event
import flash.ui.Keyboard;
import flash.display.Stage
...
public function Start()
{
trace(stage);
this.stage.addEventListener(KeyboardEvent.KEY_DOWN, KeyPressed);
trace("listening?");
this.addEventListener(Event.ENTER_FRAME, DoFrame);
}
private function KeyPressed(eventx:KeyboardEvent):void
{
trace("listening!");
LastGuess=eventx.keyCode;
trace(LastGuess);
}
answers (2)
There might be some security or other exception that is preventing it from executing some of your code. When you say running in debug mode, you are probably in CS3 right? Maybe when you test externally you could use the debug version of Flash player which is available on the downloads page. Its different from the normal Flash player version.
Also try creating a sprite or other object, adding that to the stage, specifying position and dimensions of the object, and listening to events on that object.
Also try creating a sprite or other object, adding that to the stage, specifying position and dimensions of the object, and listening to events on that object.
| Asker's rating: |
You were kind of right. I needed to turn off shortcut processing from the control menu of the flash player when running the movie. That's just SOO obvious. I still don't know why debug mode is different. Perhaps it's a player bug. Anyway, problem solved. Thanks.
I believe that
this.stage.addEventListener(KeyboardEvent.KEY_DOWN, KeyPressed);
creates a listener on this to stage for Event.KEY_DOWN. but stage should never dispatch Event.KEY_DOWN.
Another words... the eventListener declaration has to happen within the object you want to listen for the event, the above code tells this to listen for an event from stage, it doesn't tell stage to listen for an event.
this.stage.addEventListener(KeyboardEvent.KEY_DOWN, KeyPressed);
creates a listener on this to stage for Event.KEY_DOWN. but stage should never dispatch Event.KEY_DOWN.
Another words... the eventListener declaration has to happen within the object you want to listen for the event, the above code tells this to listen for an event from stage, it doesn't tell stage to listen for an event.
140 characters left












