开发者

keyboard event as3 not working

This was something that had me banging my head for 2 hours before I figured it out.

I dec开发者_运维问答ided to post it here, to help others not pull their hair out :).

Essentially the bug was I was not receiving the keyboard event from within my flash builder environment (The same bug/issue is visible with adobe flash cs5). I set the stage.focus = stage, did not help. I added other event listeners (mouse_down, frame_enter) which worked fine, I added MovieClip children and listened for events on those children, still the same issue.

package
{
  public class Test extends Sprite 
  {

    public function Test() 
    {
        this.addEventListener(Event.ADDED_TO_STAGE,init);
    }

    public function init(stage:Stage):void 
    {
        this.removeEventListener(Event.ADDED_TO_STAGE,init);
        stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
        stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);
    }


    private function keyPressed(e:KeyboardEvent):void 
    {
        trace("keyPressed");
    }

    private function keyReleased(e:KeyboardEvent):void 
    {
        trace("keyReleased");
    }
  }
}


Using keyboard commands requires listening to keyboard events. This process is identical to the process for listening to any other event in AS3. You need to use the addEventListener() method to register with a KeyboardEvent. However, unlike other objects, due to the fact that the keyboard is not necessary attached to any specific object in the project, the Keyboard Event is usually registered with the stage. In the code below, the stage object registers for a keyboard event to be triggered each time a keyboard key is pressed down.

Unlike in AS2, in AS3 Keyboard events are not global. They are issued to the stage, and they bubble through the display list to whatever display object has focus.

package
{
import flash.display.*;
import flash.events.*;

  public class Test extends Sprite 
  {
   public function Test() 
   {
     init();
   }

   public function init():void 
   {
      stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
      stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);
   }


   private function keyPressed(e:KeyboardEvent):void 
   {
      trace("keyPressed");
   }

   private function keyReleased(e:KeyboardEvent):void 
   {
    trace("keyReleased");
   }
  }
}


public function init(stage:Stage):void 

ADDED_TO_STAGE is a `listener Event` not a stage instance. 

so instead of stage:Stage use event:Event.

and u needs to import needed classes.


Marked the line that changed. Your code doesnt compile btw, check for error logs.

package  {

import flash.display.Sprite; /// changed line
import flash.events.Event; /// changed line
import flash.events.KeyboardEvent; /// changed line


public class Test extends Sprite 
{

public function Test() 
{
    this.addEventListener(Event.ADDED_TO_STAGE,init);
    /* i like it this way
    stage ? init(null) : addEventListener(Event.ADDED_TO_STAGE,init);
    */

}

public function init(e:Event):void  /// changed line
{
    this.removeEventListener(Event.ADDED_TO_STAGE,init);
    stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
    stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);
}


private function keyPressed(e:KeyboardEvent):void 
{
    trace("keyPressed");
}

private function keyReleased(e:KeyboardEvent):void 
{
    trace("keyReleased");
}
}

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜