View Full Version : EventListener not working correctly
WhoCares357
12-07-2008, 11:53 PM
I made a little script to wait until the user presses the SPACE key. The script would then go to the main loop of the game (a different frame). However, my little script isn't working at all :(. Am I doing something wrong?
import flash.events.*
this.stop();
this.addEventListener(KeyboardEvent.KEY_DOWN, check_keys);
function check_keys(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.SPACE)
{
this.gotoAndPlay("main_loop");
}
}
Thanks for any help.
Tilpo
12-08-2008, 06:22 AM
could you maybe say what doesn't work? And otherwise you might want to change.
this.stop();
to
stop();
and
this.addEventListener(KeyboardEvent.KEY_DOWN, check_keys)
to
stage.addEventListener(KeyboardEvent.KEY_DOWN, check_keys)
and maybe
this.gotoAndPlay("main_loop")
to
gotoAndPlay("main_loop")
together this would make
import flash.events.*
stop();
stage.addEventListener(KeyboardEvent.KEY_DOWN, check_keys);
function check_keys(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.SPACE)
{
gotoAndPlay("main_loop");
}
}
but the best thing is to just add a few trace()'s to your code to first find out where the problem lies.
WhoCares357
12-09-2008, 11:00 AM
Thank you very much. I actually figured out the stage.addEventListener by studying some tutorials, but I didn't know that I couldn't add "this." when referencing to the movie as a whole.
Thank you very much for your help.
creynders
12-09-2008, 12:07 PM
Just to set some things straight:
this line:
this.gotoAndPlay("main_loop")
is exactly the same as
gotoAndPlay("main_loop")
And the keyword "this" doesn't refer to the movie as a whole.
"this" is context-dependent and refers to the scope where it's written in.
So if you use "this" on the main time line, it refers to the main timeline and if you use it inside a movieclip timeline it refers to the movieclip.
wvxvw
12-09-2008, 01:14 PM
You need to add listener for keyboard events to the visual object on the display list, but, the object should have focus on it in order to receive those events. Stage has this focus by default, but if you move focus from it it will stop receiving keyboard events (eg. after MouseLeave event is trigged).
WhoCares357
12-09-2008, 08:34 PM
You need to add listener for keyboard events to the visual object on the display list, but, the object should have focus on it in order to receive those events. Stage has this focus by default, but if you move focus from it it will stop receiving keyboard events (eg. after MouseLeave event is trigged).
So is there any way to have an event listener when the focus is not on the flash movie? Or would you have to do that with javascript?
wvxvw
12-09-2008, 08:46 PM
The listener exists independently from focus, but if the object isn't focused the listener won't be called. You can focus flash movie from JavaScript, but the user may draw focus away from the movie by clicking anywhere on the page outside of it.
Rossman
12-09-2008, 08:53 PM
Also, don't forget that you can't trap certain keys inside the flash authoring environment by default.
If you press CTRL-ENTER to test your movie, go under the "Control" menu and select "Disable Keyboard Shortcuts" to prevent the Flash IDE from capturing certain keys.
WhoCares357
12-10-2008, 10:11 PM
Okay, thanks everyone. You helped me a great deal :D.
|
vBulletin® v3.8.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.