2012-06-25 39 views
3

好吧,所以我一直堅持這個問題大約三個小時,我終於想要求助了。如何從Flash CS4 AS3的屏幕中刪除所有的一類兒童?

基本上,我試圖在玩家接觸其中一個玩家的時候從屏幕上移除所有敵方物體的實例,因爲他然後失去了生命並被放回到屏幕上的安全位置。

編輯:這是從我的敵人Dude .as文件的所有代碼,有點過度可能但仍然。

package 
{ 
    import flash.display.MovieClip; 
    import flash.events.*; 
    import flash.media.Sound; 
    import flash.media.SoundChannel; 
    public class Enemydude extends MovieClip 
    { 
     private var _root:Object; 
     private var speed:int = 6; 
     private var shipps = this 
     public function Enemydude() 
     { 
      addEventListener(Event.ADDED, beginclass); 
      addEventListener(Event.ENTER_FRAME, entFrame); 
     } 

     private function beginclass(event:Event):void 
     { 
      _root = MovieClip(root); 
     } 

     private function entFrame(event:Event):void 
     { 
      x -= speed; 
      if(this.x < -64) 
      { 
       removeEventListener(Event.ENTER_FRAME, entFrame); 
       _root.removeChild(this) 
      } 
      if(_root.gameover) 
      { 
       x = -700 
       removeEventListener(Event.ENTER_FRAME, entFrame); 
       removeEventListener(Event.ADDED, beginclass); 
      } 
      for (var i:int = 0; i<_root.playerBulletContainer.numChildren; i++) 
      { 
       var bulletTarget:MovieClip = _root.playerBulletContainer.getChildAt(i) 
       if (hitTestObject(bulletTarget)) 
       { 
        removeEventListener(Event.ENTER_FRAME, entFrame); 
        _root.removeChild(this); 
        _root.playerBulletContainer.removeChild(bulletTarget); 
        bulletTarget.removeListeners(); 
        _root.Score += 10 
        makeExplosion(); 
       } 
      } 
      if(hitTestObject(_root.mcship)) 
      { 
       makeExplosion(); 
       shipPos(); 
       removethis(); 
      } 

     } 
     private function makeExplosion() 
     { 
      var sndExplode:snd_explosion1; 
      var sndExplodeChannel:SoundChannel; 
      sndExplode=new snd_explosion1(); 
      sndExplodeChannel=sndExplode.play(); 
      var newExplosion01:explosionEffect=new explosionEffect ; 
      newExplosion01.x=this.x; 
      newExplosion01.y=this.y; 
      _root.explosionContainer.addChild(newExplosion01); 

     } 
     private function shipPos() 
     { 
      _root.lives -= 1; 
      _root.mcship.x = 80; 
      _root.mcship.y = 225; 
      for each(var i:Enemydude in _root.enemies) 
     { 
      removethis(); 
     } 

     _root.enemies.length = 0; 
     } 
     public function removethis():void 
     { 
      if(parent) parent.removeChild(this) 
      removeEventListener(Event.ENTER_FRAME, entFrame); 
     } 
    } 
} 

編輯:這是我的主時間軸代碼我現在有涉及Enemydude,這一切很抱歉。

var enemies:Array = []; 
var Shipheight:Number = 300; 
var Enemytime:int = 0; 
var Enemylimit:int = 16; 

    if (Enemytime<Enemylimit) 
     { 
      Enemytime ++; 
     } 
     else 
     { 
      var newEnemy01 = new Enemydude(); 
      newEnemy01.y = Shipheight; 
      newEnemy01.x = stage.stageWidth + 64; 
      addChild(newEnemy01); 
      enemies.push(newEnemy01); 
      Enemytime = 0 

function shipY(event:Event):void 
{ 
    Shipheight = Math.ceil(Math.random()* 250) + 80; 
} 

謝謝您的幫助提前,任何意見表示讚賞。

回答

1

我建議將你的敵人存儲在一個數組中。

例如,創建數組enemies

var enemies:Array = []; 

,然後修改您的代碼:

else 
{ 
    var newEnemy01 = new Enemydude(); 

    newEnemy01.y = Shipheight; 
    newEnemy01.x = stage.stageWidth + 64; 

    addChild(newEnemy01); 
    enemies.push(newEnemy01); 

    Enemytime = 0; 
} 

這樣,你可以使用這個新的數組中刪除所有的敵人:

for each(var i:Enemydude in enemies) 
{ 
    i.remove(); // Or whatever function Enemydude has to remove itself. 
} 

// Empty the enemies Array. 
enemies.length = 0; 

這裏的.remove()方法,你都可以成爲Enemydude

public function remove():void 
{ 
    if(parent) parent.removeChild(this); 

    // Remove any event listeners etc from this object. 
} 
+0

再次出現問題,仍然無法讓它們全部消失。 – CodeMode

+0

@ user1478811想要添加一個可以在'Enemydude'內創建的函數來添加到我的答案中,該函數應該完全刪除它? – Marty

+0

我很慚愧的問,但是,可以嗎? – CodeMode

1

這樣做的一種常見和簡單的方法是創建一個子容器來保存對象並銷燬該對象來代替。對於一些碰撞檢查也很容易,因爲您可以使用持有者對象對玩家進行檢查。

如果你不想創建這個,你可以使用一個數組或向量來存儲對這個對象的引用,使得遍歷列表變得容易。

我persoally建議vector aprouch:

var enemyList:Vector.<Enemy> = new Vector.<Enemy>(); 

然後可以循環幾乎像一個數組(馬蒂華萊士表現出對他的回答):

for each(var e:Enemy in enemyList) { 
    container.removeChild(e); 
} 
enemyList.length = 0; // empty vector 

載體比正常情況下慢一點數組,但類型安全。在大多數情況下,性能差異幾乎可以忽略不計。

+0

'Vectors'對數組執行速度較慢,除了使用數字類型(int,uint,Number)時。 – Marty

+1

向量是類型安全的,所以如果你不會在一秒鐘內使用數百個操作,那麼它就會變得更好,即使如此,在大多數情況下差異幾乎可以忽略不計。 – rcdmk

+0

我用了第一個答案,不過謝謝你的輸入。 – CodeMode