Flash中的AS3错误#1009

时间:2012-03-16 11:47:38

标签: actionscript-3 flash

弹出此错误:

Error #1009: Cannot access a property or method of a null object reference. 
at test9_fla::MainTimeline/PlayerMovement()

这是我的代码:

stage.addEventListener(Event.ENTER_FRAME, PlayerMovement); 

function PlayerMovement(event:Event) {      

    if(down) {
        Jack.y += speed;
        if(Jack.hitTestObject(Muur1))
        {
            Jack.y -= speed;
        }}else if(up)
    {
        Jack.y -= speed;
        if(Jack.hitTestObject(Muur1))
        {
            Jack.y += speed;
        }}else if(left)
    {
        Jack.x -= speed;
        if(Jack.hitTestObject(Muur1))
        {
            Jack.x += speed;
        }}else if(right)
    {
        Jack.x += speed;
        if(Jack.hitTestObject (Muur1))
        {
            Jack.x -= speed;
        }}

我该如何解决错误?

1 个答案:

答案 0 :(得分:0)

很可能stage未定义,您需要等待onAddedToStage事件。但是你为什么要直接进入舞台呢?您不需要,只需将事件监听器添加到当前影片剪辑(在您的情况下很可能是文档根目录)。

addEventListener(Event.ENTER_FRAME, PlayerMovement);