When someone clicks in my flash activity, sometimes it triggers a lot of computation. If the user clicks again (e.g. a double click), this event gets queued and dispatched after the heavy computation is complete. If I clicked many times, the problem is just compounded -- if clicked fast enough, the queued heavy computation finishes up to ten seconds later, with each clickEvent slowly dribbling out the next task.
I have two questions.
First: how can I get the accurate time for when the click took place? In the example below, I am getting the dispatch of rapid click events long after the click was clacked (sp?).
Second: what is a good design pattern for collecting every click? Off the top of my head I suppose I should
defer any computation until the next EnterFrame event, but if someone clicks during the computation on the EnterFrame event... well then, I've got the same problem!
I suppose breaking down the heavy computation into a psuedo-thread is another solution, but depending on the speed of the processor, finding the granularity is difficult.
Adding a flag after the first click to disregard the next clicks... but this solution doesn't let me track what the user was trying to do while he was locked out. A solution to my first question is what I need here.
Thanks for any advice. Here is some sample code to demonstrate the issue:
package
{
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.MouseEvent;
import flash.geom.Rectangle;
public class clicky extends Sprite
{
private static var _lastTraceTime:Number = new Date().getTime();
private var _sp:Sprite;
private var _state1:Boolean;
public function clicky( ):void
{ super( );
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
_state1 = true;
_sp = new Sprite( );
addChild( _sp );
_sp.graphics.beginFill( 0xFF00AA, 1 );
_sp.graphics.drawRect( 10, 10, 100, 100 );
_sp.graphics.endFill( );
_sp.addEventListener(MouseEvent.MOUSE_DOWN, mDnCb, false, 0, true);
}
private function mDnCb( evt:MouseEvent ):void
{ traceTime( "click" );
_state1 = !_state1;
var c:uint = 0xFF0000;
if (_state1)
{ c = 0x00FFAA;
}
paintThatRect( c );
killTime( );
}
private function paintThatRect( c:uint ):void
{
_sp.graphics.beginFill( c, 1 );
_sp.graphics.drawRect( 10, 10, 100, 100 );
_sp.graphics.endFill( );
}
private function killTime( ):void
{ var r:Rectangle = new Rectangle( 0, 0, 100, 100 );
for (var i:uint = 0; i < 500000; i++)
{
var t:Rectangle = new Rectangle( i, i, i, i );
if (t.intersects(r) || r.containsRect(t) || t.containsRect(r))
{ r = t.union(r);
}
}
}
public static function traceTime( note:String ):Number
{ var nowTime:Number = new Date().getTime();
var diff:Number = (nowTime-_lastTraceTime);
trace( "[t" + diff + "] " + note );
_lastTraceTime = nowTime;
return diff;
}
}
}