Flash: Fixing getTimer() To Stop (Some) Game Cheaters

The one and only Flash Guru Jobe Maker posted this story on his blog today:

Cheating – Controlling the Speed of Games Externally

Jobe Writes:

From what I’ve heard this technique will not slow the Flash frame rate, but rather affects the return result of getTimer(). As you probably know, getTimer() returns the number of milliseconds since the SWF initialized. So, if you’re using getTimer() to move things around, then you are at risk of cheaters changing the game speed to their own benefit.

I have been wondering for a couple months just how some of the High Scores on my game Hot Wheels Track Mod had been achieved.  If you look at the high score table, you will see some scores that seem almost impossible.  In fact, we calculated that the top two scores would have taken almost 2 days to achieve.   This new revelation from Jobe might explain it.  I did use getTimer() for the time clock in the game.  Jobe explains that to fix the problem, you should use the Date() function in Flash.   I quickly worked-up a time clock for Flash that does not use getTimer().  To be honest, I had no idea that this would work, but it seems to work fine.  I will be substituting all my getTimer() clocks for this method in the future.

Here is the code I usedto create a time clock, with using getTimer().   This is certainly not optimized code, and I’m sure there are much better programmers out there that can do this in many fewer lines of code, but I this one works for me:

var timeStarted:Date = new Date();
var displayTime:String;
this.onEnterFrame = function() {
 var timeNow:Date = new Date();
 var elspasedTime:Number;
 var fullSeconds:Number;
 var displayMinutes:String;
 var displaySeconds:String;
 elapsedTime = timeNow.getTime()-timeStarted.getTime();
 fullSeconds = Math.floor(elapsedTime/1000);
 displayMinutes = Math.floor(fullSeconds/60).toString();
 displaySeconds = Math.ceil(fullSeconds%60).toString();
 if (displayMinutes.length == 1) {
  displayMinutes = “0”+displayMinutes;
 }
 if (displaySeconds.length == 1) {
  displaySeconds = “0”+displaySeconds;
 }
 displayTime = displayMinutes+”:”+displaySeconds;
}

You can download a simple .fla file with the working code here.

Leave a Reply