AS3 - 从类中的阶段访问MovieClip

时间:2013-08-01 16:03:11

标签: actionscript-3 flash movieclip

我已经坚持了很长时间,并且已经在这里查看了过去关于这个类似问题的问题,例如这篇文章:How do I access a movieClip on the stage using as3 class?

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

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

player全局定义为MovieClip

在另一个处理程序中(在将类添加到舞台之后),我使用playerframe label设置为转到特定的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;
   }
}

0 个答案:

没有答案