//Main Class
package{
public class Main extends MovieClip{
private var bombCarrier:BombCarrier;
private var building:Building;
private var bomb:Bomb;
public var buildingArray:Array;
public function Main(){
bombCarrier = new Carrier();
addChild(bombCarrier);
for(var i:int=0;i<5;i++){
var xPosition:Number = i*105;
building = new Building(xPosition, stage.stageHeight);
addChild(building);
buildingArray.push(building);
}
stage.addEventListener(Event.KeyboardEvent, keyDownFunction);
}
public function keyDownFunction(event:KeyboardEvent){
if(event.keyCode == 70){
bomb = new Bomb(bombCarrier.x, bombCarrier.y, 0.5);
addChild(bomb);
}
}
}
}
//Bomb Class
package{
public class Bomb extends MovieClip{
private var speed:Number;
public function Bomb(x, y, speed){
this.x = x;
this.y = y;
this.speed = speed;
addEventListener(Event.ENTER_FRAME, loop);
}
public function loop(event:Event){
this.y += speed;
for(var i:int=0;i<Main(parent).buildingArray.length;i++){
if(hitTestObject(Main(parent).buildingArray[i])){
this.removeEventListener(Event.ENTER_FRAME, loop);
Main(parent).buildingArray.splice(i, 1);
parent.removeChild(this); //This line got Error
}
}
}
}
}
我尝试了很多方法,但我仍然得到相同的错误,即 TypeError:错误 #1009:无法访问空对象引用的属性或方法。在炸弹/循环()。我已经尝试使用命令行逐行调试,似乎只是这行代码“parent.removeChild(this);” 显示问题。