0

我对 stage.addeventlistner 有疑问。在做了一些研究之后,我发现了一些提示,它可能没有被实例化。

我怎么做?

typeerror 错误 #1009 无法访问空对象引用的属性或方法

(它所指的行是stage.addeventlistener)

只有重要位的代码

package 
{
import flash.display.MovieClip;
import flash.events.*;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.geom.Point;

public class TesTrun extends MovieClip 
{
    var leftPressed:Boolean = false;
    var rightPressed:Boolean = false;
    var upPressed:Boolean = false;
    var downPressed:Boolean = false;
public function TestRun() 
    {    

stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
stage.addEventListener(Event.ENTER_FRAME, loop);
      //there is more code down here but i don't think that has to be included
  }
 }
}
4

1 回答 1

1

您可以使用 ADDED_TO_STAGE 事件来触发您的初始化代码:

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

public function init(e:Event):void
{
    stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
    stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
    stage.addEventListener(Event.ENTER_FRAME, loop);

}
于 2013-01-13T18:28:54.800 回答