1

我已经坚持了很长时间,并且在这里查看了关于类似问题的过去问题,例如这篇文章:如何使用 as3 类访问舞台上的电影剪辑?.

我使用构造函数来监听ADDED_TO_STAGE事件,然后启动 main 函数来设置eventListenersfromADDED_TO_STAGE处理程序。在同一个处理程序中,我还尝试MovieClip使用以下代码从舞台上获取一个:

player = stage.getChildByName("player") as MovieClip;

player被全局定义为 a MovieClip

在另一个处理程序中(在将类添加到阶段之后),我将 设置player为转到特定的frame labelusing player.gotoAndStop("jump");。但是出现输出警告说"Cannot access a property or method of a null object reference".

这是我使用的代码:

public var player:MovieClip;

public function PlayerControl():void {      
    this.addEventListener(Event.ADDED_TO_STAGE, addedToStage);          
}

private function addedToStage(event:Event):void{
    removeEventListener(Event.ADDED_TO_STAGE, addedToStage);

    this.addEventListener(Event.ENTER_FRAME, enterFrameHandler);

    stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);

    stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);

    player = stage.getChildByName("player") as MovieClip;

}

private function enterFrameHandler(event:Event):void{   
   if(up == true && moviePlaying == false){
      player.gotoAndStop("jump");
      moviePlaying = true;
   }
}
4

0 回答 0