AS3:无法访问空对象引用的属性

时间:2011-12-19 22:14:03

标签: actionscript-3 null mouse

我正试图用动画片段跟踪鼠标X的位置。

到目前为止我做了什么:

public class LuckyHitBeta extends MovieClip {


    var ballReady:ballReady_mc;



private function liveIcon():void {
            ballReady=new ballReady_mc();
            addChild(ballReady);
            ballReady.y=1;
        }

 private function onEnterFrm(e:Event):void
     {          
        ballReady.x=mouseX;

    }

}

运行时错误:

TypeError: Error #1009: Cannot access a property or method of a null object reference.
at LuckyHitBeta/onEnterFrm()

2 个答案:

答案 0 :(得分:2)

我没有看到你调用函数liveIcon()。似乎enterFrame事件在ballready初始化之前发生。

答案 1 :(得分:1)

我认为这意味着ballReadyNULL

我会做像

这样的事情
private function onEnterFrm(e:Event):void
{          
    if (ballReady) {
        ballReady.x=mouseX;
    }
}