1 Reply Latest reply on Mar 6, 2008 4:33 PM by injpix

    Container not dispatching an event via AS3

    injpix Level 3
      Why is it that the ‘canvas’ instance is not invoking the onKeyPress method in ‘Main’ class when a key is pressed down? Is this the best way of doing this?

      code;
      ------------------------------------------------------------------------------------------ ------------------------------------------------------
      in the main.mxml:

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml"
      layout="absolute"
      creationComplete="init();">
      <mx:Script>
      <![CDATA[
      import flash.display.Stage;
      import mx.core.Container;

      private function init():void
      {
      var main:Sprite = new Main(canvas);
      }
      ]]>
      </mx:Script>

      <mx:Canvas id="canvas" x="10" y="10" width="883" height="583" backgroundColor="white">

      </mx:Canvas>
      </mx:Application>

      ------------------------------------------------------------------------------------------ ------------------------------------------------------
      in the Main.as file:

      package
      {
      import flash.display.*;
      import flash.events.*;
      import mx.core.Container;

      public class Main extends Sprite
      {
      public function Main(canvas:Container)
      {
      canvas.addEventListener(KeyboardEvent.KEY_DOWN, this.onKeyPress);
      }

      internal function onKeyPress(event:KeyboardEvent):void
      {
      trace("event = "+event)
      }
      }
      }
        • 1. Re: Container not dispatching an event via AS3
          injpix Level 3
          I am able to invoke that function if I change the following 2 methods as below. So I am assuming the canvas instance doesn't like KeyboardEvents.

          code
          public function Main(canvas:Container)
          {
          canvas.addEventListener(MouseEvent.CLICK, this.onKeyPress);
          }

          internal function onKeyPress(event:MouseEvent):void
          {
          trace("event = "+event)
          }