【问题标题】:Can't remove object from stage无法从舞台上移除对象
【发布时间】:2014-06-30 11:41:32
【问题描述】:

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

其中有一些荷兰语单词,例如“hoeSpelen”表示指令文本,“af”表示游戏结束文本,“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;
        }
    }
}

【问题讨论】:

  • 当您检测到removeChild("myObjectHere") 已离开屏幕时尝试使用

标签: actionscript-3 flash


【解决方案1】:

未测试 - 可能充满错误,并且我有一段时间没有完成 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,如果它在可见舞台之外,请将其从其父级中删除(我只是放 this.parent 是因为您没有在舞台上添加障碍物)。

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

【讨论】:

  • 作为displayObject的扩展,已经定义了一个stage对象。 Flash 中的所有 displayObjects 都会获得一个名为 stage 的属性,一旦添加到显示列表中就会自动填充该属性。从根本上说,这是解决所提问题的正确方法。
猜你喜欢
  • 1970-01-01
  • 2014-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多