Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/flash/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Actionscript 3 阶段中的removeChild()时发生AS3错误#1004 //主类 包装{ 公共类Main扩展了MovieClip{ 私人航空公司:航空公司; 私人楼宇:大厦; 私人var炸弹:炸弹; 公共var构建阵列:阵列; 公共功能Main(){ bombCarrier=新载体(); addChild(bombCarrier); for(var i:int=0;i_Actionscript 3_Flash_Flash Cs5 - Fatal编程技术网

Actionscript 3 阶段中的removeChild()时发生AS3错误#1004 //主类 包装{ 公共类Main扩展了MovieClip{ 私人航空公司:航空公司; 私人楼宇:大厦; 私人var炸弹:炸弹; 公共var构建阵列:阵列; 公共功能Main(){ bombCarrier=新载体(); addChild(bombCarrier); for(var i:int=0;i

Actionscript 3 阶段中的removeChild()时发生AS3错误#1004 //主类 包装{ 公共类Main扩展了MovieClip{ 私人航空公司:航空公司; 私人楼宇:大厦; 私人var炸弹:炸弹; 公共var构建阵列:阵列; 公共功能Main(){ bombCarrier=新载体(); addChild(bombCarrier); for(var i:int=0;i,actionscript-3,flash,flash-cs5,Actionscript 3,Flash,Flash Cs5,看起来您多次尝试移除炸弹(在for循环中)。因此,在第一次之后,它不再是父对象的子对象 此外,在将炸弹添加到阶段之前,您会启动enter frame处理程序,因此在此之前父级将为null 一旦你第一次移除炸弹,你就会想要打破;你的循环 //Main Class package{ public class Main extends MovieClip{ private var bombCarrier:BombCarrier; private var building:Build

看起来您多次尝试移除炸弹(在for循环中)。因此,在第一次之后,它不再是父对象的子对象

此外,在将炸弹添加到阶段之前,您会启动enter frame处理程序,因此在此之前父级将为null

一旦你第一次移除炸弹,你就会想要打破;你的循环

//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
        }
      }
    }
  }
}
//炸弹类
包装{
公共级炸弹扩展了MovieClip{
私人var速度:数字;
私有var构建阵列:阵列;
公共功能炸弹(x、y、速度){
这个.x=x;
这个。y=y;
速度=速度;
addEventListener(事件。添加到\u阶段,添加了statage,false,0,true);
}
私有函数addedStatage(e:事件):void{
buildingArray=Main(parent).buildingArray;//最好在构造函数中传递它
addEventListener(Event.ENTER_FRAME,循环);
}
公共函数循环(事件:事件){
这个.y+=速度;

对于(var i:int=0;iIt有效。还有一件事,我如何移除建筑物的child()?你想在移除子弹的同时移除建筑物吗?是~之后,我需要使用gotoAndPlay()添加爆炸效果最佳实践,就是创建一个
dispose()
(或任何你想称之为它的东西)在
Building
类中的公共函数,然后在循环中断之前调用该函数。
buildingArray[i].dispose()
。您应该将构建数组更改为向量(与数组相同,但每个项都是特定的对象类型)
var buildingArray:vector=new vector();
爆炸可以是建筑处置方法的一部分(爆炸动画,完成后从父对象移除建筑),也可以是创建和管理的单独对象
//Bomb Class
package{
  public class Bomb extends MovieClip{
    private var speed:Number;
    private var buildingArray:Array;

    public function Bomb(x, y, speed){
      this.x = x;
      this.y = y;
      this.speed = speed;
      addEventListener(Event.ADDED_TO_STAGE,addedToStage,false,0,true);
    }

    private function addedToStage(e:Event):void {
        buildingArray = Main(parent).buildingArray;  //it would be better to just pass it in the constructor
        addEventListener(Event.ENTER_FRAME, loop);
    }

    public function loop(event:Event){
      this.y += speed;
      for(var i:int=0;i<buildingArray.length;i++){
        if(hitTestObject(buildingArray[i])){
          this.removeEventListener(Event.ENTER_FRAME, loop);
          buildingArray.splice(i, 1);
          parent.removeChild(this);
          break; //get out of the loop
        }
      }
    }
  }
}