As3: Multitouch Zoom and Rotate


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

Another winning example. This shows you how to Zoom and Rotate. 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.Bitmap;
  4. import flash.display.Sprite;
  5. import flash.events.TransformGestureEvent;
  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=0x000000)]
  12. public class GestureExample extends Sprite
  13. {
  14. [Embed(source="african_elephant.jpg")]
  15. public var ElephantImage:Class;
  16. public var scaleDebug:TextField;
  17. public var rotateDebug:TextField;
  18.  
  19. public function GestureExample()
  20. {
  21. // Debug
  22. var tf:TextFormat = new TextFormat();
  23. tf.color = 0xffffff;
  24. tf.font = "Helvetica";
  25. tf.size = 11;
  26. this.scaleDebug = new TextField();
  27. this.scaleDebug.width = 310;
  28. this.scaleDebug.defaultTextFormat = tf;
  29. this.scaleDebug.x = 2;
  30. this.scaleDebug.y = 2;
  31. this.stage.addChild(this.scaleDebug);
  32. this.rotateDebug = new TextField();
  33. this.rotateDebug.width = 310;
  34. this.rotateDebug.defaultTextFormat = tf;
  35. this.rotateDebug.x = 2;
  36. this.rotateDebug.y = 15;
  37. this.stage.addChild(this.rotateDebug);
  38.  
  39. var elephantBitmap:Bitmap = new ElephantImage();
  40. var elephant:Sprite = new Sprite();
  41.  
  42. elephant.addChild(elephantBitmap);
  43.  
  44. elephant.x = 160;
  45. elephant.y = 230;
  46.  
  47. elephantBitmap.x = (300 - (elephantBitmap.bitmapData.width / 2)) * -1;
  48. elephantBitmap.y = (400 - (elephantBitmap.bitmapData.height / 2)) *-1;
  49.  
  50. this.addChild(elephant);
  51.  
  52. Multitouch.inputMode = MultitouchInputMode.GESTURE;
  53. elephant.addEventListener(TransformGestureEvent.GESTURE_ZOOM, onZoom);
  54. elephant.addEventListener(TransformGestureEvent.GESTURE_ROTATE, onRotate);
  55. }
  56.  
  57. private function onZoom(e:TransformGestureEvent):void
  58. {
  59. this.scaleDebug.text = (e.scaleX + ", " + e.scaleY);
  60. var elephant:Sprite = e.target as Sprite;
  61. elephant.scaleX *= e.scaleX;
  62. elephant.scaleY *= e.scaleY;
  63. }
  64.  
  65. private function onRotate(e:TransformGestureEvent):void
  66. {
  67. var elephant:Sprite = e.target as Sprite;
  68. this.rotateDebug.text = String(e.rotation);
  69. elephant.rotation += e.rotation;
  70. }
  71. }
  72. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.