actionscript-3 - 如何从 Flash CS4 AS3 中的屏幕中删除所有一类 child ?

标签 actionscript-3

好吧,我已经坚持了大约三个小时了,我终于想寻求帮助了。

基本上,当我的玩家飞船与其中一个敌人接触时,我试图从屏幕上移除所有敌方对象实例,因为他随后失去了生命并被放回屏幕上的安全位置。

编辑:这是我的 Enemy 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;
}

提前感谢您的帮助,如有任何建议,我们将不胜感激。

最佳答案

我建议将你的敌人存储在一个数组中。

例如,创建数组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;

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

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

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

关于actionscript-3 - 如何从 Flash CS4 AS3 中的屏幕中删除所有一类 child ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11182457/

相关文章:

actionscript-3 - 如何从列表中选择音频源Flash + ActionScript 3

actionscript-3 - DatagramSocket bind() 和 connect() 的区别?

xml - 更改 AS3 中 XML 对象的属性值

actionscript-3 - StageDisplayState 不允许全屏模式

java - 将 Java 移植到 AS3

apache-flex - 如何从给定键开始迭代 Flex 字典?

actionscript-3 - AS3 Try Catch URL 加载器

apache-flex - 是否可以在 flex 项目的 ActionScript 中检索 swf 元数据?

actionscript-3 - Flex 中的简单效果

javascript - 外部接口(interface)回调没有监听?