1
votes

I am currently working on a game where you need to survive as long as possible while dodging questions that come your way. (all with AS3)
At the moment I am going from 1 scene to another in between the game field and the question field, but everytime I go to the question scene the timer in the game scene resets itself. I was wondering if it was possible to have the timer continue while being in the question scene?
Also I have a movable character in between the menus which incidentally are also made in different scenes and the player is able to move him around, and I would very much like him to stay in the last position he was in the next screen, as in I move him to the top right in the main menu and when I go to the options menu I want him to still be in the top right and not in his initial position.

As for my timer this is the code I am using at the moment:

    import flash.utils.Timer;    
    import flash.events.Event;    
    import flash.events.TimerEvent;    
    import flash.globalization.DateTimeFormatter;    


    var timer:Timer = new Timer(100);    
            timer.start();    
    timer.addEventListener(TimerEvent.TIMER, timerTickHandler);    
    var timerCount:int = 0;    

    function timerTickHandler(Event:TimerEvent):void    
    {
        timerCount += 100;
        toTimeCode(timerCount);
    }

    function toTimeCode(milliseconds:int) : void {
//create a date object using the elapsed milliseconds
var time:Date = new Date(milliseconds);

//define minutes/seconds/mseconds
var minutes:String = String(time.minutes);
var seconds:String = String(time.seconds);
var miliseconds:String = String(Math.round(time.milliseconds)/100);

//add zero if neccecary, for example: 2:3.5 becomes 02:03.5
minutes = (minutes.length != 2) ? '0'+minutes : minutes;
seconds = (seconds.length != 2) ? '0'+seconds : seconds;

//display elapsed time on in a textfield on stage
timer_txt.text = minutes + ":" + seconds+"." + miliseconds;

    }

And my character is using this code:

    /* Move with Keyboard Arrows
    Allows the specified symbol instance to be moved with the keyboard arrows.

    Instructions:
    1. To increase or decrease the amount of movement, replace the number 5 below with                the number of pixels you want the symbol instance to move with each key press.
    Note the number 5 appears four times in the code below.
    */

    var upPressed:Boolean = false;    
    var downPressed:Boolean = false;    
    var leftPressed:Boolean = false;    
    var rightPressed:Boolean = false;    

    rutte.addEventListener(Event.ENTER_FRAME, fl_MoveInDirectionOfKey);    
    stage.addEventListener(KeyboardEvent.KEY_DOWN, fl_SetKeyPressed);    
    stage.addEventListener(KeyboardEvent.KEY_UP, fl_UnsetKeyPressed);    

    function fl_MoveInDirectionOfKey(event:Event)    
    {    
        if (upPressed)    
        {    
            rutte.y -= 5;    
        }    
        if (downPressed)    
        {    
            rutte.y += 5;    
        }    
        if (leftPressed)    
        {    
    rutte.x -= 5;    
    rutte.scaleX = 1; // face left    
}    
if (rightPressed)    
{    
    rutte.x += 5;    
    rutte.scaleX = -1; // face right    
}    
    }    

    function fl_SetKeyPressed(event:KeyboardEvent):void    
    {    
switch (event.keyCode)    
{    
    case Keyboard.UP:    
    {    
        upPressed = true;    
        break;    
    }    
    case Keyboard.DOWN:    
    {
        downPressed = true;    
        break;    
    }    
    case Keyboard.LEFT:    
    {    
        leftPressed = true;    
        break;    
    }    
    case Keyboard.RIGHT:    
    {    
        rightPressed = true;    
        break;    
    }    
}    
    }    

    function fl_UnsetKeyPressed(event:KeyboardEvent):void    
    {    
switch (event.keyCode)    
{    
    case Keyboard.UP:    
    {    
        upPressed = false;    
        break;    
    }    
    case Keyboard.DOWN:    
    {    
        downPressed = false;    
        break;    
    }    
    case Keyboard.LEFT:    
    {    
        leftPressed = false;     
        break;    
    }    
    case Keyboard.RIGHT:    
    {    
        rightPressed = false;    
        break;    
    }    

Thank you in advance for all the help you can give me.

Kind Regards.

1
Are you using scenes, as in gotoAndPlay(1,"Scene 2"); – if so you might want to look at putting things in a single scene and managing the items in your code instead of relying on the scenes.Gone3d

1 Answers

0
votes

in flash there is a fundamental aspect of how timeline and scenes works. Once you move away from a frame to another frame, The content of the frame and it's properties/states/actions are gone and reseted.

Personally I recommend you use a single scene with a timeline divided into frames labeled, there you can specify a layer for the actions and global variables, one for the user interface and another one for the specific actions of each frame. example:

Flash Timeline

So you never lose the reference of variables because the frame is not constantly recreated, all your important actions, including you timer, should be in your first frame.

I was testing, here you can see a working example: http://db.tt/FZuQVvt3. Here you can download the FLA file to be used as a base for your project: http://db.tt/RHG9G5lo