AS3: Basic Multitouch Example


/ Published in: ActionScript 3
Save to your folder(s)

Found this handy example on Adobe DevNet. http://www.adobe.com/devnet/flash/articles/multitouch_gestures.html


Copy this code and paste it in your HTML
  1. package
  2. {
  3. import flash.display.Sprite;
  4. import flash.events.TouchEvent;
  5. import flash.text.AntiAliasType;
  6. import flash.text.TextField;
  7. import flash.text.TextFormat;
  8. import flash.ui.Multitouch;
  9. import flash.ui.MultitouchInputMode;
  10.  
  11. [SWF(width=320, height=460, frameRate=24, backgroundColor=0xEB7F00)]
  12. public class MultitouchExample extends Sprite
  13. {
  14. private var dots:Object;
  15. private var labels:Object;
  16. private var labelFormat:TextFormat;
  17. private var dotCount:uint;
  18. private var dotsLeft:TextField;
  19. private static const LABEL_SPACING:uint = 15;
  20.  
  21. public function MultitouchExample()
  22. {
  23. super();
  24.  
  25. this.labelFormat = new TextFormat();
  26. labelFormat.color = 0xACF0F2;
  27. labelFormat.font = "Helvetica";
  28. labelFormat.size = 11;
  29.  
  30. this.dotCount = 0;
  31.  
  32. this.dotsLeft = new TextField();
  33. this.dotsLeft.width = 300;
  34. this.dotsLeft.defaultTextFormat = this.labelFormat;
  35. this.dotsLeft.x = 3;
  36. this.dotsLeft.y = 0;
  37. this.stage.addChild(this.dotsLeft);
  38. this.updateDotsLeft();
  39.  
  40. this.dots = new Object();
  41. this.labels = new Object();
  42.  
  43. Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;
  44. this.stage.addEventListener(TouchEvent.TOUCH_BEGIN, onTouchBegin);
  45. this.stage.addEventListener(TouchEvent.TOUCH_MOVE, onTouchMove);
  46. this.stage.addEventListener(TouchEvent.TOUCH_END, onTouchEnd);
  47. }
  48.  
  49. private function onTouchBegin(e:TouchEvent):void
  50. {
  51. if (this.dotCount == Multitouch.maxTouchPoints) return;
  52. var dot:Sprite = this.getCircle();
  53. dot.x = e.stageX;
  54. dot.y = e.stageY;
  55. this.stage.addChild(dot);
  56. dot.startTouchDrag(e.touchPointID, true);
  57. this.dots[e.touchPointID] = dot;
  58.  
  59. ++this.dotCount;
  60.  
  61. var label:TextField = this.getLabel(e.stageX + ", " + e.stageY);
  62. label.x = 3;
  63. label.y = this.dotCount * LABEL_SPACING;
  64. this.stage.addChild(label);
  65. this.labels[e.touchPointID] = label;
  66.  
  67. this.updateDotsLeft();
  68. }
  69.  
  70. private function onTouchMove(e:TouchEvent):void
  71. {
  72. var label:TextField = this.labels[e.touchPointID];
  73. label.text = (e.stageX + ", " + e.stageY);
  74. }
  75.  
  76. private function onTouchEnd(e:TouchEvent):void
  77. {
  78. var dot:Sprite = this.dots[e.touchPointID];
  79. var label:TextField = this.labels[e.touchPointID];
  80.  
  81. this.stage.removeChild(dot);
  82. this.stage.removeChild(label);
  83.  
  84. delete this.dots[e.touchPointID];
  85. delete this.labels[e.touchPointID];
  86.  
  87. --this.dotCount;
  88.  
  89. this.updateDotsLeft();
  90. }
  91.  
  92. private function getCircle(circumference:uint = 40):Sprite
  93. {
  94. var circle:Sprite = new Sprite();
  95. circle.graphics.beginFill(0x1695A3);
  96. circle.graphics.drawCircle(0, 0, circumference);
  97. return circle;
  98. }
  99.  
  100. private function getLabel(initialText:String):TextField
  101. {
  102. var label:TextField = new TextField();
  103. label.defaultTextFormat = this.labelFormat;
  104. label.selectable = false;
  105. label.antiAliasType = AntiAliasType.ADVANCED;
  106. label.text = initialText;
  107. return label;
  108. }
  109.  
  110. private function updateDotsLeft():void
  111. {
  112. this.dotsLeft.text = "Touches Remaining: " + (Multitouch.maxTouchPoints - this.dotCount);
  113. }
  114. }
  115. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.