Class used for object following mouse with elasticity including keyboard control capture


/ Published in: Other
Save to your folder(s)



Copy this code and paste it in your HTML
  1. package com.game
  2. {
  3. import caurina.transitions.Tweener;
  4.  
  5. import com.app.data.AppFlags;
  6. import com.app.data.Config;
  7. import com.app.data.Data;
  8. import com.game.utils.GameUtils;
  9. import com.robot.comm.DispatchManager;
  10.  
  11. import flash.display.Bitmap;
  12. import flash.display.BitmapData;
  13. import flash.display.MovieClip;
  14. import flash.display.Sprite;
  15. import flash.events.Event;
  16. import flash.events.EventDispatcher;
  17. import flash.events.IEventDispatcher;
  18. import flash.events.KeyboardEvent;
  19. import flash.events.MouseEvent;
  20. import flash.geom.Matrix;
  21. import flash.geom.Point;
  22. import flash.geom.Rectangle;
  23. import flash.ui.Mouse;
  24.  
  25. import swingpants.effect.MagnifyingGlass;
  26.  
  27. public class ViewfinderController extends EventDispatcher
  28. {
  29. public static const VIEWFINDER_CLICKED:String = "viewfinderClicked";
  30.  
  31. protected const FRICTION:Number = 0.1;
  32. protected const REFRACTION:Number=0.5;
  33. protected const RADIUS:Number = 80;
  34.  
  35. protected var isFirstKeyPress:Boolean = true;
  36. //----------------------------------------------------------------
  37. protected var _viewfinder_mc:Sprite;
  38. public function set viewfinder_mc(v:Sprite):void { _viewfinder_mc = v; };
  39. public function get viewfinder_mc():Sprite { return _viewfinder_mc; };
  40. //----------------------------------------------------------------
  41. protected var _magnifying_glass:MagnifyingGlass;
  42. public function set magnifying_glass(v:MagnifyingGlass):void { _magnifying_glass = v; };
  43. public function get magnifying_glass():MagnifyingGlass { return _magnifying_glass; };
  44. //----------------------------------------------------------------
  45. protected var _mousePoint:Point;
  46. public function set mousePoint(v:Point):void { _mousePoint = v; };
  47. public function get mousePoint():Point { return _mousePoint; };
  48. //----------------------------------------------------------------
  49. protected var _magPoint:Point;
  50. public function set magPoint(v:Point):void { _magPoint = v; };
  51. public function get magPoint():Point { return _magPoint; };
  52. //----------------------------------------------------------------
  53. public function ViewfinderController(target:IEventDispatcher=null)
  54. {
  55. super(target);
  56.  
  57. DispatchManager.addEventListener(AppFlags.FREEZE, freeze );
  58. DispatchManager.addEventListener(AppFlags.UNFREEZE, unfreeze );
  59. }
  60. //---------------------------------------------------
  61. public function addViewfinderSprite(mc:Sprite,mapBmpData:BitmapData):void
  62. {
  63. viewfinder_mc = mc;
  64. mousePoint = new Point(0,0);
  65. GameManager.getInstance().gameSprite.addChild(viewfinder_mc);
  66. viewfinder_mc.alpha = 0;
  67. viewfinder_mc.y = Config.O_H/2;
  68. viewfinder_mc.x = Config.O_W/2;
  69.  
  70. magnifying_glass = new MagnifyingGlass(REFRACTION,RADIUS, new Point(viewfinder_mc.x , viewfinder_mc.y), mapBmpData);
  71. viewfinder_mc.addChildAt(magnifying_glass,0);
  72. }
  73. //---------------------------------------------------
  74. public function enable():void
  75. {
  76. //Mouse.hide();
  77. Tweener.addTween( viewfinder_mc, { alpha:1, time:1, transition:'easeOutCubic'} ) ;
  78.  
  79. viewfinder_mc.addEventListener(Event.ENTER_FRAME,moveViewfinder);
  80. viewfinder_mc.addEventListener(MouseEvent.CLICK,click);
  81.  
  82. viewfinder_mc.stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDownHandler);
  83. viewfinder_mc.stage.addEventListener(KeyboardEvent.KEY_UP,keyUpHandler);
  84.  
  85. viewfinder_mc.focusRect = false;
  86. viewfinder_mc.stage.focus = viewfinder_mc;
  87. }
  88. //---------------------------------------------------
  89. public function disable():void
  90. {
  91. //Mouse.show();
  92. Tweener.addTween( viewfinder_mc, { alpha:0, time:1, transition:'easeOutCubic'} ) ;
  93.  
  94. viewfinder_mc.removeEventListener(Event.ENTER_FRAME,moveViewfinder);
  95. viewfinder_mc.removeEventListener(MouseEvent.CLICK,click);
  96.  
  97. viewfinder_mc.stage.removeEventListener(KeyboardEvent.KEY_DOWN,keyDownHandler);
  98. viewfinder_mc.stage.removeEventListener(KeyboardEvent.KEY_UP,keyUpHandler);
  99.  
  100. viewfinder_mc.tabEnabled = false;
  101. }
  102. //----------------------------------------------------------------
  103.  
  104.  
  105.  
  106. //-------------------------------------------------------------------------------------
  107. protected function click(e:Event = null):void
  108. {
  109. if(!GameManager.getInstance().debug) viewfinder_mc.removeEventListener(MouseEvent.CLICK,click);
  110.  
  111. if(!magPoint) magPoint = new Point();
  112. magPoint.x = viewfinder_mc.x;
  113. magPoint.y = viewfinder_mc.y;
  114.  
  115. //disable();
  116.  
  117. GameManager.getInstance().gameMain.playSound("click");
  118. dispatchEvent(new Event(VIEWFINDER_CLICKED,true));
  119. }
  120. //-------------------------------------------------------------------------------------
  121. protected function keyDownHandler(e:KeyboardEvent):void
  122. {
  123. /*
  124. Constants only available with the AIR runtime Keyboard class.
  125.  
  126. W = 87
  127. A = 65
  128. S = 83
  129. D = 68
  130. X = 88
  131. */
  132.  
  133. if(e.keyCode == 65 || e.keyCode == 83 || e.keyCode == 87 || e.keyCode == 68 || e.keyCode == 88)
  134. {
  135. var isShift:Boolean = e.shiftKey;
  136.  
  137. viewfinder_mc.removeEventListener(MouseEvent.MOUSE_MOVE,resumeMouseControl);
  138. viewfinder_mc.removeEventListener(Event.ENTER_FRAME,moveViewfinder);
  139.  
  140. if(e.keyCode == 87) mousePoint.y -= isFirstKeyPress? isShift? 10 : 1 : isShift? 20 : 10;
  141. if(e.keyCode == 83) mousePoint.y += isFirstKeyPress? isShift? 10 : 1 : isShift? 20 : 10;
  142. if(e.keyCode == 65) mousePoint.x -= isFirstKeyPress? isShift? 10 : 1 : isShift? 20 : 10;
  143. if(e.keyCode == 68) mousePoint.x += isFirstKeyPress? isShift? 10 : 1 : isShift? 20 : 10;
  144.  
  145. viewfinder_mc.x = mousePoint.x;
  146. viewfinder_mc.y = mousePoint.y;
  147.  
  148. magnifying_glass.magnifyingGlassPosition = new Point(viewfinder_mc.x, viewfinder_mc.y);
  149. magnifying_glass.update();
  150.  
  151. if(e.keyCode == 88)
  152. {
  153. click();
  154. }
  155.  
  156. isFirstKeyPress = false;
  157. }
  158. }
  159. //-------------------------------------------------------------------------------------
  160. protected function keyUpHandler(e:KeyboardEvent):void
  161. {
  162. isFirstKeyPress = true;
  163. if(e.keyCode == 65 || e.keyCode == 83 || e.keyCode == 87 || e.keyCode == 68 || e.keyCode == 88)
  164. {
  165. viewfinder_mc.addEventListener(MouseEvent.MOUSE_MOVE,resumeMouseControl);
  166. }
  167. }
  168. //-------------------------------------------------------------------------------------
  169. protected function resumeMouseControl(e:MouseEvent):void
  170. {
  171. viewfinder_mc.removeEventListener(MouseEvent.MOUSE_MOVE,resumeMouseControl);
  172. viewfinder_mc.addEventListener(Event.ENTER_FRAME,moveViewfinder);
  173. }
  174. //-------------------------------------------------------------------------------------
  175. protected function moveViewfinder(e:Event):void
  176. {
  177. mousePoint.x = GameManager.getInstance().gameSprite.mouseX;
  178. mousePoint.y = GameManager.getInstance().gameSprite.mouseY;
  179.  
  180. var revertX:int = viewfinder_mc.x;
  181. var revertY:int = viewfinder_mc.y;
  182.  
  183. viewfinder_mc.x = viewfinder_mc.x+((mousePoint.x-viewfinder_mc.x)*FRICTION);
  184. viewfinder_mc.y = viewfinder_mc.y+((mousePoint.y-viewfinder_mc.y)*FRICTION);
  185.  
  186. if(viewfinder_mc.x <= 0 || viewfinder_mc.x >= Config.O_W)
  187. viewfinder_mc.x = revertX;
  188.  
  189. if(viewfinder_mc.y <= 0 || viewfinder_mc.y >= Config.O_H)
  190. viewfinder_mc.y = revertY;
  191.  
  192. magnifying_glass.magnifyingGlassPosition = new Point(viewfinder_mc.x, viewfinder_mc.y);
  193. magnifying_glass.update();
  194. }
  195. //-------------------------------------------------------------------------------------
  196. protected function freeze(e:Event):void
  197. {
  198. trace("freeze viewfinder");
  199. disable();
  200. }
  201. //-------------------------------------------------------------------------------------
  202. protected function unfreeze(e:Event):void
  203. {
  204. trace("unfreeze viewfinder");
  205. enable();
  206. }
  207. }
  208. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.