-
-
Save PrimaryFeather/6143ef6a359b4384d94f3db657f4a0aa to your computer and use it in GitHub Desktop.
Experiment regarding touch and mouse event queuing in ActionScript.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package flash | |
{ | |
import flash.display.Bitmap; | |
import flash.display.BitmapData; | |
import flash.display.Shape; | |
import flash.display.Sprite; | |
import flash.events.MouseEvent; | |
import flash.events.TouchEvent; | |
import flash.events.Event; | |
import flash.geom.ColorTransform; | |
import flash.geom.Matrix; | |
import flash.text.TextField; | |
import flash.ui.Multitouch; | |
import flash.ui.MultitouchInputMode; | |
import flash.utils.getTimer; | |
public class TouchExperimentsFla extends Sprite | |
{ | |
private var _contents:Sprite; | |
private var _bitmapData:BitmapData; | |
private var _brush:Shape; | |
private var _stats:TextField; | |
private static var sMatrix:Matrix = new Matrix(); | |
private static var sColorTransform:ColorTransform = new ColorTransform(); | |
public function TouchExperimentsFla() | |
{ | |
super(); | |
Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT; | |
addEventListener(Event.ADDED_TO_STAGE, init); | |
addEventListener(Event.ENTER_FRAME, onEnterFrame); | |
addEventListener(Event.ENTER_FRAME, measureFPS); | |
} | |
private function init(e:Event):void | |
{ | |
trace("starting ..."); | |
removeEventListener(Event.ADDED_TO_STAGE, init); | |
_brush = new Shape(); | |
_brush.graphics.beginFill(0xffffff); | |
_brush.graphics.drawCircle(0, 0, 3); | |
_brush.graphics.endFill; | |
_bitmapData = new BitmapData(stage.stageWidth, stage.stageHeight, false, 0xffffff); | |
_contents = new Sprite(); | |
addChild(_contents); | |
var bitmap:Bitmap = new Bitmap(_bitmapData); | |
_contents.addChild(bitmap); | |
_contents.addEventListener(TouchEvent.TOUCH_BEGIN, onTouch); | |
_contents.addEventListener(TouchEvent.TOUCH_END, onTouch); | |
_contents.addEventListener(TouchEvent.TOUCH_MOVE, onTouch); | |
_contents.addEventListener(MouseEvent.MOUSE_DOWN, onMouse); | |
_contents.addEventListener(MouseEvent.MOUSE_UP, onMouse); | |
_contents.addEventListener(MouseEvent.MOUSE_MOVE, onMouse); | |
_stats = new TextField(); | |
_stats.width = 40; | |
_stats.height = 30; | |
_stats.border = true; | |
addChild(_stats); | |
} | |
private var _sum:Number = 0.0; | |
private function onEnterFrame(event:Event):void | |
{ | |
const count:int = 30000; | |
var sum:Number = 0.0; | |
for (var i:int = 0; i < count; ++i) | |
{ | |
sum += Math.sin(i / count * Math.PI); | |
sum += Math.cos(i / count * Math.PI); | |
} | |
_sum += sum / count; | |
} | |
private function onTouch(e:TouchEvent):void | |
{ | |
drawAt(e.stageX, e.stageY); | |
} | |
private function onMouse(e:MouseEvent):void | |
{ | |
drawAt(e.stageX, e.stageY); | |
} | |
private function drawAt(x:int, y:int):void | |
{ | |
sMatrix.tx = x; | |
sMatrix.ty = y; | |
sColorTransform.redMultiplier = Math.random(); | |
sColorTransform.greenMultiplier = Math.random(); | |
sColorTransform.blueMultiplier = Math.random(); | |
_bitmapData.draw(_brush, sMatrix, sColorTransform); | |
} | |
private var _lastFrameTime:int; | |
private var _totalTime:int; | |
private var _frameCount:int; | |
private function measureFPS(event:Event):void | |
{ | |
var now:int = getTimer(); | |
var passedTime:int = now - _lastFrameTime; | |
_totalTime += passedTime; | |
_frameCount += 1; | |
if (_totalTime >= 1000) | |
{ | |
var fps:Number =_frameCount / _totalTime * 1000; | |
_stats.text = fps.toFixed(2); | |
_totalTime = 0; | |
_frameCount = 0; | |
} | |
_lastFrameTime = now; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment