如何让我的游戏在2分钟后重启?

时间:2014-08-21 17:38:12

标签: actionscript-3 flash actionscript

所以我是AS3的新手,并且努力达到这一点,对于任何错误或任何错误都很抱歉。

我现在想要的只是让我的游戏在2分钟后重新开始。有谁知道我怎么做到这一点?我完全失去了。

这是我的代码:

package 
{
import flash.display.Sprite;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import Ship;
import Coin;
import CoinB;
import flash.events.Event;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.utils.getTimer;

public class Main extends Sprite
{
    // constructor code
    private var Player1:Ship;
    private var Coin1:Coin;
    private var Coin2:Coin;
    private var Coin3:CoinB;
    private var Coin4:CoinB;
    private var score:int = 0;
    private var container;
    /*private var timer:Timer;*/

    public function Main():void
    {
        //Sets the position of player
        //adds player to stage
        this.Player1 = new Ship(259,365);
        addChild(this.Player1);

        Cont.visible = false;

                {
        addChild(interval_timer);
        /*addChild(frame_timer);*/
        /*frame_timer.y = 50;*/         
        var time_count:Timer = new Timer(1000);
        time_count.addEventListener(TimerEvent.TIMER, show_time);
        stage.addEventListener(Event.ENTER_FRAME,on_enter_frame);
        time_count.start();
    }

        //adds event listeners
        stage.addEventListener(KeyboardEvent.KEY_DOWN, hit);
        stage.addEventListener(Event.ENTER_FRAME, death);
        stage.addEventListener(KeyboardEvent.KEY_DOWN, retry);

        //add a Coin here
        Coin1 = new Coin(stage.stageWidth / 2,stage.stageHeight / 2,75,10);
        stage.addChild(Coin1);
        Coin2 = new Coin(stage.stageWidth / 2,stage.stageHeight / 2,125,-5);
        stage.addChild(Coin2);
        Coin3 = new CoinB(stage.stageWidth / 2,stage.stageHeight / 2,25,15);
        stage.addChild(Coin3);
        this.addEventListener(Event.ENTER_FRAME, fire);
    }

            public function show_time(event:TimerEvent)
    {
        interval_timer.text = event.target.currentCount;
    }
    public function on_enter_frame(event:Event)
    {
        var elapsed = getTimer();


    public function fire(e:Event)
    {

        if (Player1.hitTestObject(Coin1))
        {
            score += 50;
            Score.text = score.toString();

        }
        if (Player1.hitTestObject(Coin2))
        {
            score +=  10;
            Score.text = score.toString();
        }

    }

    public function death(e:Event)
    {

        if (Player1.hitTestObject(Coin3))
        {
            stage.removeEventListener(KeyboardEvent.KEY_DOWN, hit);
            score = 0;
            Score.text = score.toString();
/*          gameOver.visible = true;*/

            Cont.visible = true;
            this.removeChild(this.Player1);
            this.Player1 = new Ship(259,365);
            Coin1.visible = false;
            Coin2.visible = false;
            Player1.visible = false;
            Coin3.visible = false;
            removeChild(interval_timer);



        }


    }

    public function hit(evt:KeyboardEvent):void
    {
        //this will move the player right if the "Right" arrow key is pressed.
        if (evt.keyCode == Keyboard.RIGHT)
        {
            this.Player1.right();
            this.Player1.rotation = 90;
        }
        //this will move the player left if the "Left" arrow key is pressed.

        else if (evt.keyCode == Keyboard.LEFT)
        {
            this.Player1.left();
            this.Player1.rotation = 270;
        }
        if (evt.keyCode == Keyboard.DOWN)
        {
            this.Player1.down();
            this.Player1.rotation = 180;
        }
        //this will move the player up if the "Up" arrow key is pressed.
        else if (evt.keyCode == Keyboard.UP)
        {
            this.Player1.up();
            this.Player1.rotation = 0;
        }
}

public function retry(evt:KeyboardEvent):void
{
//restarts game. 
//adds event listener so that the player can move again
if (evt.keyCode == Keyboard.R)


    Cont.visible = false;
    stage.addEventListener(KeyboardEvent.KEY_DOWN, hit);
    Coin1.visible = true;
    Coin2.visible = true;
    Player1.visible = true;
    Coin3.visible = true        
    addChild(this.Player1);

    addChild(interval_timer);

   }

  }
  }

非常感谢任何帮助。感谢。

1 个答案:

答案 0 :(得分:1)

一般来说,当2分钟结束时,你会使用Timer来触发一个功能。

这样的事情:

var gameTimer:Timer = new Timer(120 * 1000); // 120 seconds * 1000 milliseconds
gameTimer.addEventListener(TimerEvent.TIMER, onGameTimerTick);
gameTimer.start();

function onGameTimerTick(e:TimerEvent):void {
    //stop the timer
    gameTimer.removeEventListener(TimerEvent.TIMER, onGameTimerTick);
    gameTimer.stop();

    restartMyGame();
}