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

Posted

技术标签:

【中文标题】AS3:TypeError:错误 #1009:无法访问空对象引用的属性或方法。需要帮助【英文标题】:AS3: TypeError: Error #1009: Cannot access a property or method of a null object reference. Help Needed 【发布时间】:2013-05-31 12:32:12 【问题描述】:

我是 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;

    

非常感谢您的帮助,谢谢。

【问题讨论】:

请查看此问题右侧“相关”列下的 827 个问题 - 它们将帮助您理解此错误。 【参考方案1】:

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

伙计 键 硬币1 乐谱盒 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();
    

【讨论】:

以上是关于AS3:TypeError:错误 #1009:无法访问空对象引用的属性或方法。需要帮助的主要内容,如果未能解决你的问题,请参考以下文章

AS3 - TypeError:MethodInfo 中的错误 #1009 “null”

错误 #1009:无法访问空对象引用 AS3 项目的属性或方法

TypeError:错误 #1009:无法访问空对象的属性或方法

AS3 中的错误 #1009

TypeError:错误 #1009:无法访问空对象引用的属性或方法。尝试创建按钮时。

TypeError:错误 #1009:无法访问空对象引用的属性或方法 - Event.ENTER_FRAME