tommy-gee37
tommy-gee37

Reputation: 1

How to get my game to restart after 2 minutes?

so I'm new to AS3, and have struggled to get to this point, so sorry for any errors or nooby mistakes.

All I want now, is for my game to restart after 2 minutes has passed. Does anyone know how I can do this? I'm at a complete loss.

Here is my code:

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);

   }

  }
  }

Any help is greatly appreciated. Thanks.

Upvotes: 0

Views: 78

Answers (1)

Cadin
Cadin

Reputation: 4649

Generally speaking, you would use a Timer to trigger a function when 2 minutes is up.

Something like this:

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();
}

Upvotes: 1

Related Questions