AS3无法访问null对象引用的属性或方法,与gotoAndPlay一起出现问题

时间:2013-04-17 11:50:21

标签: actionscript-3

所以我还是相当新的AS3,我正在弄清楚错误和东西,我想弄清楚为什么这不起作用应该如何,该功能仍然执行,但它留下了一个错误在我的控制台上。这是我的代码:

import flash.events.MouseEvent;
import flash.display.MovieClip;

stop();

var activeHitArray:Array = new Array(word_select_box);
var activeDropArray:Array = new Array(answer1_word, answer2_word, answer3_word);
var hitPositionsArray: Array = new Array();

for (var numi:int = 0; numi < activeDropArray.length; numi++) {
activeDropArray[numi].buttonMode = true;
activeDropArray[numi].addEventListener(MouseEvent.MOUSE_DOWN, mousedown1);
activeDropArray[numi].addEventListener(MouseEvent.MOUSE_UP, mouseup1);

     hitPositionsArray.push({xPos:activeDropArray[numi].x,        yPos:activeDropArray[numi].y});

}

function mousedown1(event:MouseEvent):void {
event.currentTarget.startDrag();
setChildIndex(MovieClip(event.currentTarget), numChildren - 1);
}

function mouseup1(event:MouseEvent):void {
var dropindex1:int = activeDropArray.indexOf(event.currentTarget);
var target:MovieClip = event.currentTarget as MovieClip;
target.stopDrag();

if(target.hitTestObject(activeDropArray[dropindex1])){
           // target.x = activeHitArray[dropindex1].x;
            //target.y = activeHitArray[dropindex1].y;

            if(answer1_word.hitTestObject(word_select_box)){
                   gotoAndStop("6");
            }

} else {
        target.x = hitPositionsArray[dropindex1].xPos;
        target.y = hitPositionsArray[dropindex1].yPos;
}


}

我遇到的错误是:

TypeError: Error #1009: Cannot access a property or method of a null object reference.
  at game_flv_fla::MainTimeline/frame6()
  at flash.display::MovieClip/gotoAndStop()
  at game_flv_fla::MainTimeline/mouseup1()

我唯一能想到的是它与gotoAndPlay()有关,因为当我将痕迹放入其中时,我没有错误。

1 个答案:

答案 0 :(得分:0)

我复制了你的代码,在Flash IDE中编译并推出了它。有用! :) 没有错误。

但我知道这可能是什么。无论是否还有链接到的对象,addEventListeners仍然处于打开状态。在进入下一帧之前,您需要清除所有活动的东西:

if(target.hitTestObject(activeDropArray[dropindex1])){
    if(answer1_word.hitTestObject(word_select_box)){
        for (var i:uint = 0; i < activeDropArray.length; i++) {
            activeDropArray[i].removeEventListener(MouseEvent.MOUSE_DOWN, mousedown1);
            activeDropArray[i].removeEventListener(MouseEvent.MOUSE_UP, mouseup1);
        }
        gotoAndStop("6");
    }
}