AS3:TypeError:错误#1009:无法访问空对象引用的属性或方法。需要帮助

时间:2013-06-04 23:41:44

标签: actionscript-3 flash debugging actionscript

我是ActionScript的新手,我真的需要帮助调试此问题。这就是我运行代码时输出的内容:

TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at StreetHobogame_fla::MainTimeline/pickup()
    at StreetHobogame_fla::MainTimeline/gameloop()

这是我的代码:

import flash.events.KeyboardEvent;

stop();
guy.stop();

var enemySpeed:Number = 3;
var wspeed:Number = 0;
var vy:Number = 0;
var gv:Number = 1;
var jumped:Boolean = false;
var score:Number = 0;
var lives:Number = 5;




livesbox.text = lives.toString();

stage.addEventListener(Event.ENTER_FRAME,gameloop);

trace(ground.x);
var motionspeed:int = 30;

left.addEventListener(MouseEvent.MOUSE_DOWN, leftMove);
left.addEventListener(MouseEvent.MOUSE_UP, leftUp);
right.addEventListener(MouseEvent.MOUSE_DOWN, rightMove);
right.addEventListener(MouseEvent.MOUSE_UP, rightUp);
jumpButton.addEventListener(MouseEvent.MOUSE_DOWN, jumpPressed);

function jumpPressed(e:Event):void
{
    if (! jumped)
    {
        vy = -14;
        jumped = true;
    }
}

function rightUp(e:Event):void
{
    wspeed = 0;


}


function rightMove(e:Event):void
{
    wspeed +=  10;
    guy.gotoAndStop(2);
}


function leftMove(event:Event):void
{
    wspeed = -10;
    guy.gotoAndStop(1);
}

function leftUp(event:Event):void
{
    wspeed = 0;

}



function gameloop(e:Event)
{
    moveplayer();
    jumpgravity();
    exitlevel1();
    //exitlevel2();
    pickup();
    enemy();
    spikes();
    enemyMove();

    return (0);

}

function pickup()
{
    if (guy.hitTestObject(key))
    {
        key.visible = false;
    }
    if (guy.hitTestObject(coin1))
    {
        coin1.x = 2000;
        score++;
        scorebox.text = score.toString();
    }
    if (guy.hitTestObject(coin2))
    {
        coin2.x = 2000;
        score++;
        scorebox.text = score.toString();
    }
    if (guy.hitTestObject(coin3))
    {
        coin3.x = 2000;
        score++;
        scorebox.text = score.toString();
    }
    if (guy.hitTestObject(coin4))
    {
        coin4.x = 2000;
        score++;
        scorebox.text = score.toString();
    }
}



function moveplayer()
{
    guy.x +=  wspeed;

    if (guy.x < 0)
    {
        guy.x = 0;
    }
    if (guy.x > 1024)
    {
        guy.x = 1024;
    }
}

function jumpgravity()
{
    vy +=  gv;
    if (! ground.hitTestPoint(guy.x,guy.y,true))
    {
        guy.y +=  vy;
    }
    if (ground.hitTestPoint(guy.x,guy.y,true))
    {

        guy.y--;
        vy = 0;
        jumped = false;

    }
}



function exitlevel1()
{
    if (guy.hitTestObject(exitMC))
    {
        if (key.visible == false)
        {
            stage.removeEventListener(Event.ENTER_FRAME,gameloop);
            left.removeEventListener(MouseEvent.MOUSE_DOWN, leftMove);
            left.removeEventListener(MouseEvent.MOUSE_UP, leftUp);
            right.removeEventListener(MouseEvent.MOUSE_DOWN, rightMove);
            right.removeEventListener(MouseEvent.MOUSE_UP, rightUp);
             jumped= false;
            wspeed = 0;
            guy.gotoAndStop(1);
            gotoAndStop(1,"level1Questions");



        }

    }

}
/*function exitlevel2()
{
    if (guy.hitTestObject(exit2MC))
    {
        if (key.visible == false)
        {
             jumped= false;
            wspeed = 0;
            guy.gotoAndStop(1);
            gotoAndStop(1,"level2Complete");



        }

    }

}
*/
function enemy()
{
    if (guy.hitTestObject(enemy1))
    {
        guy.x = 520.95;
        guy.y = 425.50;
        if (lives<=1)
        {
            lives = 0;
            gotoAndStop(1,"GameOver");
        }
        else
        {
            lives--;
            livesbox.text = lives.toString();
        }

    }
}

function spikes()
{
    if (guy.hitTestObject(Spike))
    {
        guy.x = 520;
        guy.y = 425.5;
        if (lives<=1)
        {
            lives = 0;
            gotoAndStop(1,"GameOver");
        }
        else
        {


            lives--;
            livesbox.text = lives.toString();
        }
    }
}


function enemyMove():void
{
    enemy1.x +=  enemySpeed;
    if (enemy1.x >= 350)
    {
        enemySpeed *=  -1;
        enemy1.scaleX *=  -1;

    }
    else if (enemy1.x < 60)
    {
        enemySpeed *=  -1;
        enemy1.scaleX *=  -1;

    }
}

非常感谢帮助,谢谢。

1 个答案:

答案 0 :(得分:0)

pickup()函数执行时,至少有一个变量为空:

  • COIN1
  • scorebox
  • COIN2
  • coin3
  • coin4

您可以使用trace()功能来检查哪一个。

function pickup()
{
    trace('guy==' + guy);
    trace('key==' + key);
    trace('scorebox==' + scorebox);
    trace('coin1==' + coin1);
    trace('coin2==' + coin2);
    trace('coin3==' + coin3);
    trace('coin4==' + coin4);

    if (guy.hitTestObject(key))
    {
        key.visible = false;
    }
    if (guy.hitTestObject(coin1))
    {
        coin1.x = 2000;
        score++;
        scorebox.text = score.toString();
    }
    if (guy.hitTestObject(coin2))
    {
        coin2.x = 2000;
        score++;
        scorebox.text = score.toString();
    }
    if (guy.hitTestObject(coin3))
    {
        coin3.x = 2000;
        score++;
        scorebox.text = score.toString();
    }
    if (guy.hitTestObject(coin4))
    {
        coin4.x = 2000;
        score++;
        scorebox.text = score.toString();
    }
}