cwm9's Avatar
cwm9 4
8 Asked
13 Answered
3 Best
0
No one has voted on this question yet :(
3 years, 4 months ago

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);
}
Tip for best answer: M$1.00
Separate topics with commas, or by pressing return. Use the delete or backspace key to edit or remove existing topics.

You can leave an optional "tip" with Mahalo's virtual currency, Mahalo Dollars. If you are asking a difficult question that might require some research, or if you'd like a wide variety of feedback, a higher tip often leads to more answers to your question.

M$

What is Your Answer?

0
0
0

2 Answers

0
ilaksh's Avatar
ilaksh | 3 years, 4 months ago
4
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.

You can leave an optional "tip" with Mahalo's virtual currency, Mahalo Dollars. If you are asking a difficult question that might require some research, or if you'd like a wide variety of feedback, a higher tip often leads to more answers to your question.

M$

Report Abuse

Post Reply Cancel
0
cyberangel's Avatar
cyberangel | 3 years, 4 months ago
4
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.

You can leave an optional "tip" with Mahalo's virtual currency, Mahalo Dollars. If you are asking a difficult question that might require some research, or if you'd like a wide variety of feedback, a higher tip often leads to more answers to your question.

M$

Report Abuse

Post Reply Cancel

Learn something new with our FREE educational apps!

Private lessons in the comfort of your own home. Get back in shape or finally pick up a guitar with our great experts guiding you the whole way!
Learn Guitar
Learn Hip Hop
Learn Pilates