Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy my KeyboardEvent doesn't work in my game?
    text
    copied!<p>I need to use flash as3 to create a game, and I have tried to use 3 layer to load my swf. My game is in the third layer, and the first and second layers are just a preloader script.</p> <p>My problem is when the game is loaded onto the stage of first layer my <code>KeyboardEvent</code> function is not work until I press the stage.</p> <p>I have try to use <code>Event.ADDED_TO_STAGE</code> to solve it, but I also get a same error.</p> <p>this is my code for preloader</p> <pre><code>var request:URLRequest = new URLRequest("game.swf"); var loader:Loader = new Loader(); loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, loadProgress); loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadComplete); function loadProgress(event:ProgressEvent):void { var percentLoaded:Number = event.bytesLoaded / event.bytesTotal; percentLoaded = Math.round(percentLoaded * 100); //this.percentLoaded.text = String(uint(percentLoaded)) + "%"; } function loadComplete(event:Event):void { trace("Load Complete"); } loader.load(request); this.addChild(loader); </code></pre> <p>And this is my game script on the timeline</p> <pre><code>if(stage != null) { stageAddHandler(null); } else { addEventListener(Event.ADDED_TO_STAGE, stageAddHandler); } function stageAddHandler(e:Event = null):void { stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDownHandler); stage.addEventListener(KeyboardEvent.KEY_UP,keyUpHandler); } </code></pre> <p>Any idea on this?</p> <p>Thanks for advance</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload