簡體   English   中英

As3 Flash錯誤“無法訪問空對象引用的屬性或方法”

[英]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');
}

希望能對您有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM