As3 闪存错误“无法访问 属性 或空对象引用的方法”

As3 flash error “Cannot access a property or method of a null object reference”

我在测试我的电影时遇到了这个错误

TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at ACTUALPROJECT_fla::MainTimeline/youLose()[ACTUALPROJECT_fla.MainTimeline::frame3:112]
    at ACTUALPROJECT_fla::MainTimeline/SharkEat()[ACTUALPROJECT_fla.MainTimeline::frame3:87]

这是我的代码

import flash.events.MouseEvent;
import flash.events.Event;
import flash.events.*;
import flash.utils.Timer;

var rectangle:Rectangle = new Rectangle(0,345,600,455);

turtle_mc.addEventListener(MouseEvent.MOUSE_DOWN, pickupObject);
turtle_mc.addEventListener(MouseEvent.MOUSE_UP, dropObject);
turtle_mc.addEventListener(Event.ENTER_FRAME, handleCollision);
turtle_mc.addEventListener(Event.ENTER_FRAME, handleCollision2);
turtle_mc.addEventListener(Event.ENTER_FRAME, handleCollision4);
turtle_mc.addEventListener(Event.ENTER_FRAME, SharkEat);
turtle_mc.buttonMode = true;
turtle_mc.originalY = turtle_mc.y;
turtle_mc.originalX = turtle_mc.x;

function resetTurtlePosition()
{
    turtle_mc.y = turtle_mc.originalY;
    turtle_mc.x = turtle_mc.originalX;
}

function pickupObject(event:MouseEvent):void
{
    event.target.startDrag(false, rectangle);
}

function dropObject(event:MouseEvent):void
{
    event.target.stopDrag();
}


function handleCollision(e:Event):void
{


    if (plasticBag_mc.hitTestPoint(turtle_mc.x,turtle_mc.y,false))
    {

        youLose();

    }
    else
    {

    }
}
function handleCollision2(e:Event):void
{


    if (fishingBoat_mc.hitTestPoint(turtle_mc.x,turtle_mc.y,false))
    {

        youLose();

    }
    else
    {

    }
}
function handleCollision4(e:Event):void
{


    if (oilSpillBoat_mc.hitTestPoint(turtle_mc.x,turtle_mc.y,false))
    {

        youLose();

    }
    else
    {

    }
}
function SharkEat(e:Event):void
{


    if (shark_mc.hitTestPoint(turtle_mc.x,turtle_mc.y,false))
    {

        **youLose();**

    }
    else
    {

    }
}
var nCount:Number = 0;
var myScore:Timer = new Timer(10,nCount);
counter_txt.text = nCount.toString();
myScore.start();
myScore.addEventListener(TimerEvent.TIMER, countdown);
function countdown(e:TimerEvent):void
{
    nCount++;
    counter_txt.text = nCount.toString();

}

function youLose():void
{
    myScore.stop();
    turtle_mc.stopDrag();
    resetTurtlePosition();
    **this.storedtxt = counter_txt.text;**
    gotoAndStop(3,"PlayTheGame");
}

我已经用星号标记了这两行。 我是 Flash 和 Action Script-3 的新手,有人可以帮我吗?

重新验证您的代码后,我认为您的错误来自这一行:

gotoAndStop(3,"PlayTheGame");

因为即使在进入 PlayTheGame 场景的第 3 帧后,您的 turtle_mc 对象上的 EnterFrame 事件仍会被触发,这就是您出现该错误的原因,因为一些您的 EnterFrame 事件处理程序中使用的对象在该框架中不存在。

为了避免这种情况,您可以这样做:

// 你应该知道你可以使用一个处理程序来处理所有的碰撞

turtle_mc.addEventListener(Event.ENTER_FRAME, handleCollision);

function handleCollision(e:Event):void
{
    if (        
        plasticBag_mc.hitTestPoint(turtle_mc.x,turtle_mc.y,false) ||
        fishingBoat_mc.hitTestPoint(turtle_mc.x,turtle_mc.y,false) ||
        oilSpillBoat_mc.hitTestPoint(turtle_mc.x,turtle_mc.y,false) ||
        shark_mc.hitTestPoint(turtle_mc.x,turtle_mc.y,false))       
    {
        youLose();
    }
    else {}
}

然后在您的 youLose() 函数中,您应该在转到另一个场景之前删除 Event.ENTER_FRAME 事件侦听器:

function youLose():void
{
    // ...

    turtle_mc.removeEventListener(Event.ENTER_FRAME, handleCollision);

    gotoAndStop(3, 'PlayTheGame');
}

希望能帮到你。