无法从舞台中删除对象

时间:2014-06-30 11:41:33

标签: actionscript-3 flash

我有问题。我正在尝试使用ActionScript 3.0在Adobe Flash中制作直升机游戏,但现在游戏正常运行,但障碍无法从舞台上移除。障碍仍然远离舞台。我怎样才能消除障碍?同样的问题,如果你是游戏结束,如果你是游戏结束的游戏,但你看到的最后产生的障碍仍然没有被删除。如何在一段时间后让障碍更快?

其中有一些荷兰语,例如“hoeSpelen”等。这是指示文字en' af'是用于gameover文本和' tijd' =时间和' balkje' =障碍。

我希望你能帮助我。

package
{
    import flash.display.MovieClip;
    import flash.events.MouseEvent;
    import flash.utils.getTimer;
    import flash.events.Event;

    public class iCopter extends MovieClip 
    {
        private var copter : Copter = null;
        private var gameover : GameOver = null;
        private var balkje : Balkje = null;
        private var tijd : int
        var score = 0;
        var highscore = 0;

        public function onStartButton(event:MouseEvent)
        {
            startiCopter()
        }

        public function iCopter()
        {
            startButton.addEventListener(MouseEvent.CLICK, onStartButton);

            af.visible = false
            output.visible = false
            hscore.visible = false
        }

        public function startiCopter()
        {
            removeChild(startButton);
            removeChild(hoeSpelen);
            removeChild(af);

            score = 0

            icopterlogo.visible = false

            output.visible = true
            hscore.visible = true

            copter = new Copter();
            copter.x = 100;
            copter.y = 200;

            addChild(copter);

            tijd = getTimer();

            addEventListener(Event.ENTER_FRAME, onEnterFrame);
        }

        public function onEnterFrame(event:Event)
        {
            var now:int = getTimer();

            if (now - tijd  > 1250)
            {
                var balkje = new Balkje();
                balkje.x = 350;
                balkje.y = Math.random() * 150;

                addChild (balkje);

                tijd = now

                score = score + 10;

                output.text = "score: "+score;

                if (balkje.x <= -10) //don't work.
                { //don't work.
                    removeChild (balkje); //don't work.
                } //don't work.
            }

            addEventListener(Event.ENTER_FRAME, botsing);

        }

        function botsing (event:Event)
        {

            for (var i = 0; i < numChildren; i++)
            {
                if (getChildAt(i) is Balkje || getChildAt(i) is Vloer)
                {
                    var b = getChildAt(i) as MovieClip;

                    if (b.hitTestObject(copter))
                    {
                        removeChild (copter);
                        removeEventListener(Event.ENTER_FRAME, onEnterFrame);

                        var gameover = new GameOver();

                        addChild(af);

                        af.visible = true

                        addChild(hoeSpelen);

                        addChild(startButton);

                        if (score > highscore)
                        {
                            highscore = score
                            hscore.text = "highscore: "+highscore;
                        }
                    }
                }
            }
        }
    }
}

以下是直升机和障碍物的脚本

直升机:

muisKlik = mouseClick
muisDruk = mousePush
muisOmhoog = mouseUp

package
{
    import flash.display.MovieClip;
    import flash.events.MouseEvent;
    import flash.events.Event;

    public class Copter extends MovieClip
    {
        var vy : Number = 0;
        var muisKlik : Boolean = false;

        public function Copter()
        {
             vy = 5;
             addEventListener(Event.ENTER_FRAME, onEnterFrame);
             addEventListener(Event.ADDED_TO_STAGE, init);
        }

        private function init(event:Event)
        {
            stage.addEventListener(MouseEvent.MOUSE_DOWN, muisDruk);
            stage.addEventListener(MouseEvent.MOUSE_UP, muisOmhoog);
        }

        public function onEnterFrame(event:Event)
        {
            if (muisKlik == true)
            {
                y -= vy;
            }
            else
            {
                y += vy;
            }
        }

        public function muisDruk (event:MouseEvent)
        {
            muisKlik = true
        }

        public function muisOmhoog (event:MouseEvent)
        {
            muisKlik = false
        }
    }
}

障碍:

package
{
    import flash.display.MovieClip;
    import flash.events.Event;

    public class Balkje extends MovieClip
    {
        var vx : Number = 1;

        public function Balkje() 
        {
            vx = 5;
            addEventListener( Event.ENTER_FRAME, onEnterFrame );
        }

        public function onEnterFrame( event:Event )
        {
            x -= vx;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

未经测试 - 可能充满错误,我暂时没有完成AS3:

初始化障碍物时,传入舞台对象(不确定这是否是最佳做法)

package {
import flash.display.MovieClip;
import flash.events.Event;

public class Balkje extends MovieClip
{
    var vx : Number = 1;

    public function Balkje() 
    {
        vx = 5;
        if(!stage){
            //if stage isn't populated yet, wait for it
            this.addEventListner(Event.ADDED_TO_STAGE,addedToStage);
        }else{
            init();
        }
    }

    private function addedToStage(e:Event):void {
        this.removeEventListener(Event.ADDED_TO_STAGE,addedToStage);
        init();
    }

    protected function init():void {
        this.addEventListener(Event.ENTER_FRAME, onEnterFrame);
    }

    public function onEnterFrame( event:Event )
    {
        x -= vx;

        //check x
        if(this.x + this.width <= 0 || this.x >= stage.stageWidth) {
            if(this.parent) this.parent.remmoveChild(this);
            removeEventListener( Event.ENTER_FRAME, onEnterFrame );
        }
    }
}
}

同样,没有经过测试(stage.stageWidth是否正确以获得舞台宽度?)但你明白了。检查对象的x,如果它在可见阶段之外,将其从父对象中移除(我只是把它放在了.parent,因为你没有将障碍物添加到舞台上)。

另外,为什么要在ENTER_FRAME调用的函数中向ENTER_FRAME添加事件监听器?