我为MovieClip创建了一个类,为玩家(char)创建硬币。假设所有类都添加到得分和硬币收集变量,然后从舞台上移除一枚硬币。但是当我使用gotoAndStop(#);
更改框架时,控制台会发送垃圾邮件
ErrorType:错误#1009:无法访问空对象引用的属性或方法。"
硬币类:
public class coin extends MovieClip{
var char:MovieClip;
var MainTimeLine = MovieClip(root);
public function coin() {
// constructor code
this.addEventListener(Event.ENTER_FRAME,update);
}
function update(event:Event):void{
if(MainTimeLine.currentFrame!=5){
char=MovieClip(root).char;
if(this.hitTestObject(char)){
this.removeEventListener(Event.ENTER_FRAME,update);
parent.removeChild(this);
MainTimeLine.score++;
MainTimeLine.coinscollected++;
}
}
}
}
答案 0 :(得分:1)
在您的显示对象已添加到显示列表之前,不会填充Root。您需要在设置变量之前侦听该事件。
var char:MovieClip;
var MainTimeLine; //do not initialize here, root is null at this point
public function coin() {
// constructor code
//root is still null here sometimes too, so see if it's populated yet
if(root){
init(); //root is populated, skip to initialization
}else{
this.addEventListener(Event.ADDED_TO_STAGE,addedToStage); //root isn't populated yet, listen for added to stage and then initialize
}
}
private function addedToStage(e:Event = null):void {
this.removeEventListener(Event.ADDED_TO_STAGE,addedToStage);
init();
}
private function init():void {
MainTimeLine = MovieClip(root)
this.addEventListener(Event.ENTER_FRAME,update);
}