重新加载Flash游戏+禁用计时器

时间:2014-05-29 12:45:58

标签: actionscript-3 flash

我的朋友和我必须从零开始为学校创建一个Flash游戏。游戏运行良好,直到我们通过屏幕到达游戏。我一直试图禁用对象计时器,以便加电和敌人进入屏幕,没有运气。 此外,当我尝试重新启动游戏时,它会从上一个游戏结束的地方开始。以同样的速度,敌人等。有没有办法重置它?

package
{
    import flash.display.*;
    import flash.text.TextField;
    import flash.utils.Timer;
    import flash.events.*;
    import flash.ui.*;

    public class Main extends MovieClip
    {
        private var level:uint = 0;
        private var lives:int = 0;
        private var score:uint = 0
        private var speed:uint = 10;

        public function Main()
        {
            stop();
        }

        public function verwijderObstacles():void
        {

            if(getChildByName("obstacle_mc") != null){
                mcObstacle(getChildByName("obstacle_mc")).destroy();
            }
        }

        public function verwijderPowerups():void
        {
            if(getChildByName("score_mc") != null){
                mcScore(getChildByName("score_mc")).destroy();
            }
        }

        public function verwijderLife():void
        {
            if(getChildByName("life_mc") != null){
                mcLife(getChildByName("life_mc")).destroy();
            }
        }


        public function initGame()
        {
            stage.focus = stage;
            var gravity:Number = 3;
            var jumping:Boolean = false;
            var jumpPower:Number = 0;
            var score_mc:MovieClip = new mcScore();
            var obstacle_mc:MovieClip = new mcObstacle();

            if(!contains(score_mc))
            {
                var scoreTimer:Timer = new Timer(Math.ceil(Math.random() * 5) * 1000 ,1);
                scoreTimer.addEventListener(TimerEvent.TIMER, spawnScore);
                scoreTimer.start();
            }
            if(!contains(obstacle_mc))
            {
                var obstacleTimer:Timer = new Timer(Math.ceil(Math.random() * 2) * 1000 ,1);
                obstacleTimer.addEventListener(TimerEvent.TIMER, spawnObstacle);
                obstacleTimer.start();
            }
            stage.addEventListener(Event.ENTER_FRAME, scrollBackground);
            stage.addEventListener(Event.ENTER_FRAME, scrollClouds);
            stage.addEventListener(KeyboardEvent.KEY_DOWN, jump);
            stage.addEventListener(Event.ENTER_FRAME, update);
            stage.addEventListener(Event.ENTER_FRAME, moveScore);
            stage.addEventListener(Event.ENTER_FRAME, moveObstacle);
            stage.addEventListener(Event.ENTER_FRAME, hitScore);
            stage.addEventListener(Event.ENTER_FRAME, hitObstacle);
            stage.addEventListener(Event.ENTER_FRAME, checkOverlap);
            stage.addEventListener(Event.ENTER_FRAME, checkGame);

            function scrollBackground(e:Event):void
            {
                if(!(getChildByName("background1_mc") == null) && !(getChildByName("background2_mc") == null))
                {
                    background1_mc.x -= speed;
                    background2_mc.x -= speed;
                    if(background1_mc.x <= -background1_mc.width)
                    {
                        background1_mc.x = background2_mc.x + background2_mc.width;
                    }
                    else if(background2_mc.x <= -background2_mc.width)
                    {
                        background2_mc.x = background1_mc.x + background1_mc.width;
                    }
                }
            }

            function scrollClouds(e:Event):void
            {
                if(!(getChildByName("clouds1_mc") == null) && !(getChildByName("clouds2_mc") == null))
                {
                    var cloudSpeed = speed / 5;
                    clouds1_mc.x -= cloudSpeed;
                    clouds2_mc.x -= cloudSpeed;

                    if(clouds1_mc.x <= -clouds1_mc.width)
                    {
                        clouds1_mc.x = clouds2_mc.x + clouds2_mc.width;
                    }
                    else if(clouds2_mc.x <= -clouds2_mc.width)
                    {
                        clouds2_mc.x = clouds1_mc.x + clouds1_mc.width;
                    }
                }
            }

            function jump(e:KeyboardEvent):void
            {
                if(e.keyCode == Keyboard.SPACE)
                {
                    if(jumping != true)
                    {
                        jumpPower = -30;
                        jumping = true;
                        player_mc.gotoAndPlay(46);
                    }
                }   
            }

            function update(e:Event):void
            {
                if(jumping)
                {
                    player_mc.y += jumpPower;
                    jumpPower += gravity;

                    if(player_mc.y >= 320)
                    {
                        jumping = false;
                        player_mc.y = 320;
                        player_mc.gotoAndPlay(61);
                    }
                }
            }
            function spawnScore(e:TimerEvent):void
            {
                var randomNumber:Number = Math.round(Math.random());
                addChild(score_mc);
                score_mc.name = "score_mc";
                if(randomNumber == 0)
                {
                    score_mc.x = 800;
                    score_mc.y = 240;
                }
                else
                {
                    score_mc.x = 800;
                    score_mc.y = 30;
                }
            }
            function spawnObstacle(e:TimerEvent):void
            {
                addChild(obstacle_mc);
                obstacle_mc.x = 800;
                obstacle_mc.y = 240;
                obstacle_mc.name = "obstacle_mc";
            }

            function moveScore(e:Event):void
            {
                score_mc.x -= speed;
                if(!(getChildByName("score_mc") == null))
                {
                    if(score_mc.x < -86)
                    {
                        removeChild(getChildByName("score_mc"));
                        var scoreTimer:Timer = new Timer(Math.ceil(Math.random() * 5) * 1000 ,1);
                        scoreTimer.addEventListener(TimerEvent.TIMER, spawnScore);
                        scoreTimer.start();
                    }
                }
            }
            function moveObstacle(e:Event):void
            {
                obstacle_mc.x -= speed;
                if(!(getChildByName("obstacle_mc") == null))
                {
                    if(obstacle_mc.x < -72)
                    {
                        removeChild(getChildByName("obstacle_mc"));
                        var obstacleTimer:Timer = new Timer(Math.ceil(Math.random() * 5) * 1000 ,1);
                        obstacleTimer.addEventListener(TimerEvent.TIMER, spawnObstacle);
                        obstacleTimer.start();
                    }
                }
            }

            function hitScore(e:Event):void
            {
                if(!(getChildByName("score_mc") == null) && !(getChildByName("player_mc") == null))
                {
                    if (player_mc.hitTestObject(score_mc))
                    {
                        removeChild(getChildByName("score_mc"));
                        var scoreTimer:Timer = new Timer(Math.ceil(Math.random() * 5) * 1000 ,1);
                        scoreTimer.addEventListener(TimerEvent.TIMER, spawnScore);
                        scoreTimer.start();
                        player_mc.gotoAndPlay(2);
                        score++;
                        if(score == 1)
                        {
                            gauge_mc.gotoAndPlay(2);
                        }
                        else if(score == 2)
                        {
                            gauge_mc.gotoAndPlay(16);
                        }
                        else if(score == 3)
                        {
                            gauge_mc.gotoAndPlay(31);
                        }
                        else if(score == 4)
                        {
                            gauge_mc.gotoAndPlay(46);
                        }
                        else if(score == 5)
                        {
                            gauge_mc.gotoAndPlay(61);
                        }
                    }
                }
            }
            function hitObstacle(e:Event):void
            {
                if(!(getChildByName("obstacle_mc") == null) && !(getChildByName("player_mc") == null))
                {
                    if(player_mc.hitTestPoint(obstacle_mc.x, obstacle_mc.y, true))
                    {
                        removeChild(getChildByName("obstacle_mc"));
                        var obstacleTimer:Timer = new Timer(Math.ceil(Math.random() * 5) * 1000 ,1);
                        obstacleTimer.addEventListener(TimerEvent.TIMER, spawnObstacle);
                        obstacleTimer.start();
                        player_mc.gotoAndPlay(16);
                        lives--;
                        lives_txt.text = "x" + lives.toString();
                    }
                }
            }
            function checkOverlap(e:Event):void
            {
                if(obstacle_mc.hitTestObject(score_mc))
                {
                    if(!(getChildByName("score_mc") == null))
                    {
                        removeChild(getChildByName("score_mc"));
                        var scoreTimer:Timer = new Timer(Math.ceil(Math.random() * 5) * 1000 ,1);
                        scoreTimer.addEventListener(TimerEvent.TIMER, spawnScore);
                        scoreTimer.start();
                    }
                }
            }


            function checkGame(e:Event):void
            {
                if(lives < 0)
                {
                    gaNaarGameOver();
                }
            }


        }
            private function gaNaarGameOver():void{

            //verwijderPowerups();
            //verwijderObstacles();
            gotoAndStop("gameover");

        }
    }
}

1 个答案:

答案 0 :(得分:1)

要停止Timer,您需要致电stop()reset()。如果你想让它完全消失,你也想从计时器中删除事件监听器并将其设置为null。

要做任何这些事情,您需要对计时器对象的引用。最简单的方法是将计时器创建为Main类的实例变量(而不是像现在那样创建局部变量)。如果您不想这样做,您还可以使用currentTargetTimerEvent属性从事件处理程序内部获取对Timer的引用。