Question

I developed a flash game in AS3, similar to Music Challenge on Facebook, where you need to guess from a selection of songs, wich one is the one playing, score is based on how many did you guess in 30 seconds, this timeout, is handled by an instance of Timer.

Everything works good, but someone pointed out that by reducing cpu performance, timer goes slower, allowing users to play longer, guess more songs and ultimately get a higher score, then I did some research, and read that to prevent this, I need to use systems time, the problem is, I don't know how to do this.

Any help is very much appreciated.

Was it helpful?

Solution

You could have a repeating timer with a short interval, 100ms or so, and check new Date().getTime() on each tick. By default, a new Date has the current system time, and getTime() gives you the time in milliseconds for a simpler comparison. Compare that with a start time and end the game once at least 30 seconds have passed. Something like:

import flash.utils.Timer;
import flash.events.TimerEvent;

var ticker:Timer = new Timer(100, 0);
ticker.addEventListener(TimerEvent.TIMER, checkElapsedTime);

var startTime:Number = new Date().getTime();
ticker.start();

function checkElapsedTime(e:TimerEvent):void {

    var now:Number = new Date().getTime();

    if (now - startTime >= 30 * 1000) {

        // End the game and stop the ticker.

        ticker.stop();
        ticker.removeEventListener(TimerEvent.TIMER, checkElapsedTime);
    }

}

There is obviously still some slight lag if the timer is very slow, so just in case I would run the same check when an answer is submitted and ignore it if it was after the deadline.

Finally, to check if a user has changed their system clock you could store the previous now and make sure the new now is always larger.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top