[英]As3 flash error “Cannot access a property or method of a null object reference”
I came up with this error when testing my movie 我在测试电影时想到了这个错误
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]
this is my code 这是我的代码
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");
}
i have marked the two lines with asterix. 我已经用星号标记了这两行。 I'm new to flash and Action Script-3, can anyone help me out?
我是Flash和Action Script-3的新手,有人可以帮助我吗?
After re-verifying your code, I think that your error is coming from this line : 重新验证代码后,我认为您的错误来自以下行:
gotoAndStop(3,"PlayTheGame");
because the EnterFrame
event on your turtle_mc
object is still being fired even after going to the 3rd frame of your PlayTheGame
scene, and that's why you got that error because some objects used in your EnterFrame
event handlers doesn't exist in that frame. 因为即使进入
PlayTheGame
场景的第3帧后, turtle_mc
对象上的EnterFrame
事件仍会被触发,这就是为什么会出现该错误的原因,因为EnterFrame
事件处理程序中使用的某些对象在该帧中不存在。
So to avoid that, you can do like this : 因此,为了避免这种情况,您可以这样做:
// you should know that you can use a single handler to handle all collisions //您应该知道可以使用一个处理程序来处理所有冲突
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 {}
}
then in your youLose()
function, you should remove the Event.ENTER_FRAME
event listener before going to the other scene : 然后在您的
youLose()
函数中,应先转到Event.ENTER_FRAME
事件侦听器,然后再转到另一个场景:
function youLose():void
{
// ...
turtle_mc.removeEventListener(Event.ENTER_FRAME, handleCollision);
gotoAndStop(3, 'PlayTheGame');
}
Hope that can help. 希望能对您有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.