How to give a score based on time?
-
13-07-2021 - |
Question
In the game I am making, I need to make a scoring system based on time.
If you get a match in the first second of gameplay. That match is worth more points, than if I get a match on the last second of gameplay.
So in other words, the faster you do the level, the more points you get. Does anyone know how to achieve this? I know the total time for each level.
The game is real time so it needs to be seen to show high scores early in the game with a decreasing score as time goes on.
Thanks.
Solution
Define a maximum time per level then do some math tricks:
score = Math.max(0, levelMaxTime - timeSpent) * levelScore;
Example:
timeSpent = 12; // The player completed the level in 12 seconds
levelMaxTime = 20; // The player has to completed the level within 30 seconds
levelScore = 50; // The player will be awarded of 50 points per remaining second
// Compute the final score
score = Math.max(0, levelMaxTime - timeSpent) * levelScore; // 400 points
OTHER TIPS
You use the Timer Class for this. So you should end with something similar to :
private var delay:uint = 1000;
private var repeat:uint = 3;
private var timer:Timer = new Timer(delay, repeat);
Add listeners for the ticks...
timer.addEventListener(TimerEvent.TIMER, timerHandler);
timer.addEventListener(TimerEvent.TIMER_COMPLETE, completeHandler);
// Reduce bonus with time by a percentage
private function timerHandler(e:TimerEvent):void { scoreBonus *= 0.9; }
// Set bonus to 0
private function completeHandler(e:TimerEvent):void { scoreBonus = 0; }
Now set the score bonus & start the timer at the point where you wish to begin the bonus from.
scoreBonus = 10;
timer.start();
So at any point of time, you just need to add the bonus to the total score.