Actionscript 3 如何在Flash CS4 AS3中从屏幕上删除所有一类儿童?

Actionscript 3 如何在Flash CS4 AS3中从屏幕上删除所有一类儿童?,actionscript-3,Actionscript 3,好吧,我已经在这件事上纠缠了大约三个小时了,我终于想寻求帮助了 基本上,当我的玩家飞船与其中一个物体发生接触时,我试图从屏幕上移除所有敌人物体的实例,因为他会失去一条生命,并被放回屏幕上的安全位置 编辑:这是我的敌人的所有代码。作为文件,可能有点过火,但无论如何 package { import flash.display.MovieClip; import flash.events.*; import flash.media.Sound; import flas

好吧,我已经在这件事上纠缠了大约三个小时了,我终于想寻求帮助了

基本上,当我的玩家飞船与其中一个物体发生接触时,我试图从屏幕上移除所有敌人物体的实例,因为他会失去一条生命,并被放回屏幕上的安全位置

编辑:这是我的敌人的所有代码。作为文件,可能有点过火,但无论如何

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);
        }
    }
}
包
{
导入flash.display.MovieClip;
导入flash.events.*;
导入flash.media.Sound;
导入flash.media.SoundChannel;
公共类Enemydude扩展MovieClip
{
私有变量根:对象;
专用var速度:int=6;
private var shipps=this
公共函数Enemydude()
{
addEventListener(Event.ADDED,beginclass);
addEventListener(Event.ENTER\u FRAME,entFrame);
}
私有函数beginclass(事件:事件):无效
{
_根=MovieClip(根);
}
私有函数entFrame(事件:事件):void
{
x-=速度;
如果(此.x<-64)
{
removeEventListener(Event.ENTER_FRAME,entFrame);
_root.removeChild(此)
}
如果(_root.gameover)
{
x=-700
removeEventListener(Event.ENTER_FRAME,entFrame);
removeEventListener(Event.ADDED,beginclass);
}

对于(var i:int=0;i我建议将您的敌人存储在一个数组中

例如,创建数组
敌人

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;

下面是您可以为
Enemydude
创建的
.remove()
方法:

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

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

一种常见且简单的方法是创建一个子容器来保存对象并销毁该对象。这也使得一些碰撞检查变得容易,因为您可以使用容器对象对玩家进行一次检查

如果不想创建此对象,可以使用数组或向量来存储对该对象的引用,这样可以轻松遍历列表

我个人推荐一种方法:


向量比普通数组慢一点,但类型安全。在大多数情况下,性能上的差异几乎可以忽略不计。

问题再次出现,仍然无法使它们全部消失。@user1478811希望我在我的答案中添加一个函数,您可以在
Enemydude
中创建该函数,从而将其完全删除?我感觉有点奇怪不好意思问,是的,你可以吗?我按你说的那样添加了函数,但它们仍然没有从屏幕上全部删除,所以我把我能找到的与Enemyded业务相关的所有代码都放了出来,我有一种感觉,这是一件非常简单的事情,哈哈。哎呀,我想忘了@Marty Wallace。
Vectors
perform sl除了在处理数字类型(int、uint、Number)时,向量是类型安全的,因此,如果不在一秒钟内对它们使用数百个操作,那么它会变得更好。即使如此,在大多数情况下,差异几乎可以忽略不计。
public function remove():void
{
    if(parent) parent.removeChild(this);

    // Remove any event listeners etc from this object.
}
var enemyList:Vector.<Enemy> = new Vector.<Enemy>();
for each(var e:Enemy in enemyList) {
    container.removeChild(e);
}
enemyList.length = 0; // empty vector