A Flash Developer Resource Site

Results 1 to 9 of 9

Thread: [F8] Reliable Timer For Game?

  1. #1
    Senior Member
    Join Date
    Dec 2005
    Posts
    426

    [F8] Reliable Timer For Game?

    I am trying to come up with a reliable timer for my game that counts how long the user has been playing a level.

    I have tried setInterval but it is unreliable. Is there a better way to do it?

  2. #2
    Ryan Thomson EvolveDesigns's Avatar
    Join Date
    Oct 2001
    Location
    British Columbia
    Posts
    3,338
    why is setInterval unreliable?
    Evolve Designs Interactive Media
    the natural selection

  3. #3
    Senior Member
    Join Date
    Dec 2005
    Posts
    426
    I cannot get it to clear and then re-enable well.

  4. #4
    Robot Master the--flash's Avatar
    Join Date
    Jul 2005
    Location
    The year 20XX...
    Posts
    132
    What's wrong with using getTimer(); ?

    EDIT: To clarify, I mean what's wrong with getting the time when a level starts, and then continuing to use getTimer(); to time it.

  5. #5
    Senior Member
    Join Date
    Dec 2005
    Posts
    426
    I tried getTimer(); but for some reason it froze after counting about 4 seconds. I will go back and try it again though.

    update: it does freeze.
    Code:
    function timer()
    {
    	currentTime=getTimer();
    	time=Math.floor((currentTime-startTime)/1000);
    	timeDisp.text=time;
    }
    I call timer(); on the enterFrame event.
    Last edited by jasonpeinko; 08-18-2008 at 06:34 PM.

  6. #6
    FK'n_dog a_modified_dog's Avatar
    Join Date
    Apr 2003
    Location
    "aaarf"
    Posts
    9,176
    the code below might help
    put 3 textfields in a movieclip
    add a couple of buttons to the clip for testing purposes
    add code to frame#1 of the movieclip -

    PHP Code:
    timeDisplay.text lapDisplay.text "00 : 00 : 00 : 000";

    function 
    pad(num){ return ("0"+String(num)).substr(-22); }

    function 
    init(){
    = new Date(); t0 getTimer(); method run
    };

    function 
    run(){
    d.setTime(getTimer()-t0);
    var 
    pad(d.getHours());
    var 
    pad(d.getMinutes());
    var 
    pad(d.getSeconds());
    var 
    ms d.getMilliseconds();
    if (
    ms<100)ms "0"+ms;  if (ms<10)ms "0"+ms;
    timeDisplay.text h+" : "+m+" : "+s+" : "+ms;
    };

    function 
    startTimer(){
    method init;  lapDisplay.text "00 : 00 : 00 : 000";
    };

    function 
    stopTimer(){
    method null;
    lapDisplay.text timeDisplay.text
    addUP(lapDisplay.text);
    timeDisplay.text "00 : 00 : 00 : 000";
    };

    btnGo.onPress = function(){ startTimer(); };
    btnStop.onPress = function(){ stopTimer(); };

    this.onEnterFrame = function(){ method(); };

    /////////////add up lap times/////////////////////

    0;

    function 
    addUP(str){
    totalnum 0;
    this["arr"+a] = new Array();
    this["arr"+a] = str.split(" : ");
    a++;
    for( var 
    b=0;b!=a;b++){
    Number(this["arr"+b][0])*360000;
    Number(this["arr"+b][1])*60000;
    Number(this["arr"+b][2])*1000
    ms parseInt(Number(this["arr"+b][3]),10);
    totalnum += h+m+s+ms;
    }
    trace("total = "+totalnum+" msecs");
    trace(time(totalnum));
    total.text time(totalnum);
    };

    ////////////////convert msecs to time///////////////////

    function two(n){ return ((n>9)?"":"0")+n; };
    function 
    three(n){ return ((n>99)?"":"0")+((n>9)?"":"0")+n; };

    function 
    time(ms){
    var 
    sec Math.floor(ms/1000);
    ms ms 1000;
    three(ms);

    var 
    min Math.floor(sec/60);
    sec sec 60;
    two(sec) + " : " t;

    var 
    hr Math.floor(min/60);
    min min 60;
    two(min) + " : " t;

    var 
    day Math.floor(hr/60);
    hr hr 60;
    two(hr) + " : " t;
    //t = day + " : " + t

    return t
    }; 
    if you give the movieclip an instance name - mcTimer
    you can call the functions from the main timeline - mcTimer.startTimer();

    code prolly needs tidying up .. but it works

  7. #7
    Senior Member
    Join Date
    Dec 2005
    Posts
    426
    I went for a much simpler version of that
    Code:
    function pad(num){ return ("0"+String(num)).substr(-2, 2); }
    function timer()
    {
    	d.setTime(getTimer()-startTime);
    	var h = pad(d.getHours());
    	var m = pad(d.getMinutes());
    	var s = pad(d.getSeconds()); 
    	timeDisp.text=m+":"+s;
    }
    And it looked like it was working then it stops after a few seconds.

  8. #8
    Robot Master the--flash's Avatar
    Join Date
    Jul 2005
    Location
    The year 20XX...
    Posts
    132
    Do you mean the swf froze completely, or the timer just stopped? Maybe post the .fla file, hopefully someone would be able to find something.

    EDIT:This works for me, hopefully it will for you...the button push is supposed to be when a level starts.
    Attached Files Attached Files
    Last edited by the--flash; 08-19-2008 at 12:23 AM.

  9. #9
    Senior Member
    Join Date
    Dec 2005
    Posts
    426
    Thanks for all the help. I moved the code in the timer function to the enterFrame function and it worked .... .!?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center