Skip to content

Instantly share code, notes, and snippets.

@quakeboy
Last active December 21, 2015 04:29
Show Gist options
  • Save quakeboy/6249816 to your computer and use it in GitHub Desktop.
Save quakeboy/6249816 to your computer and use it in GitHub Desktop.
Calculating clicks per minute based on mouse clicks. Needed to calculate something for my game
if (e.type == MouseEvent.MOUSE_DOWN)
{
if (last_dt == 0) //first time. no avg yet.
{
last_dt = getTimer();
}
else
{
if (new_dt == 0) //second time, first cps, first average
{
data_count++;
new_dt = getTimer();
average_dt = 60000 / (new_dt - last_dt);
sum += average_dt;
}
else
{
data_count++;
new_dt = getTimer();
//latest clicks per sec calculation
cps = 60000 / (new_dt - last_dt);
//new average
average_dt = (average_dt * (data_count-1)/data_count) + (cps/data_count);
}
debug_tf.text = Math.round(average_dt) + "";
last_dt = new_dt;
}
}
@quakeboy
Copy link
Author

******* var declarations ******

private var debug_tf:TextField = new TextField();
private var last_dt:Number = 0, new_dt:Number = 0, cps:Number = 0;
private var average_dt:Number = 0, data_count:Number = 0, sum:Number = 0;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment