Posted By

burnandbass on 04/18/12


Tagged


Versions (?)

ActionScript3 Touch Event


 / Published in: ActionScript 3
 

by Bassta

  1. package com.burnandbass {
  2.  
  3. import flash.display.*;
  4. import flash.events.MouseEvent;
  5. import flash.events.Event;
  6. import flash.geom.Point;
  7. import com.greensock.TweenMax;
  8. import com.greensock.easing.Strong;
  9.  
  10. /*
  11. ActiveSwipe - iPhone like flash slider
  12. Version 1.0 - nov 7 2011
  13. Author: Hristo Panayotov ( burnandbass[at]gmail.com )
  14.  
  15.  
  16. */
  17.  
  18. public class Slider extends Sprite {
  19.  
  20. //const for internal function
  21. private static const MOUSE_DOWN_DECAY: Number = 0.5
  22. private static const SPEED_SPRINGNESS: Number = 0.4
  23. private static const DECAY: Number = 0.93
  24.  
  25. private var _container:Sprite;
  26. private var _visible_rect:Sprite = new Sprite();
  27.  
  28. //variables for slide logic
  29. private var _visible_width:Number;
  30. private var _visible_height:Number;
  31. private var _mouseDown:Boolean;
  32. private var _velocity:Number = 0;
  33. private var _mouseDownX:Number = 0;
  34. private var _mouseDownPoint:Point;
  35. private var _lastMouseDownPoint:Point;
  36. private var slidePadding:Number = 0;
  37. private var _padding:Number;
  38. private var _page:Number = 0;
  39.  
  40. //used for setting up the slidePadding
  41. private static const _speedLimit:Number = 20
  42.  
  43. //all the pages
  44. private var _pages:Array = [];
  45.  
  46. public function Slider(sliderWidth:Number = 240, sliderHeight:Number = 320, padding:Number = 10){
  47. _container = new Sprite();
  48. _visible_width = sliderWidth;
  49. _visible_height = sliderHeight;
  50. _padding = padding;
  51.  
  52. addChild(_container);
  53.  
  54. //draw mask
  55. _visible_rect.graphics.beginFill(0xffffff);
  56. _visible_rect.graphics.drawRect(0,0,_visible_width,_visible_height);
  57. addChild(_visible_rect);
  58. this.mask = _visible_rect;
  59. }
  60.  
  61. //call this after you add all pages
  62. public function initSlider():void{
  63. this.attach();
  64. addEventListener(MouseEvent.ROLL_OVER, attach);
  65. addEventListener(MouseEvent.ROLL_OUT, dettach);
  66. }
  67.  
  68. //this function add page to the slider. Make shure it is the same width and hight as the slider
  69. //if the pages (screens) are not the same dimensions, use addForceSizePage() method
  70. public function addPage(_newPage:DisplayObject):void{
  71. _pages.push(_newPage);
  72. _container.addChild(_newPage);
  73. _newPage.x = (_visible_width*_pages.length) - _visible_width;
  74.  
  75. dispatchEvent(new SliderEvent(SliderEvent.PAGE_ADDED));
  76.  
  77. if(_newPage.width != _visible_width){
  78. trace("Slider error: the page you want to add have differenr width: \nSlider width: "
  79. + _visible_width + " || page width: " + _newPage.width + " . Some errors may occure!");
  80. }
  81. }
  82.  
  83. //the same as addPage(), but forses the size to be exactly they should be for this screen
  84. public function addForceSizePage(_newPage:DisplayObject):void{
  85. _newPage.width = _visible_width;
  86. _newPage.height = _visible_height;
  87. addPage(_newPage);
  88. }
  89.  
  90. //slide to page. The first page have index 0 (setPage(2) will lead you to third page);
  91. public function setPage(n:Number):void{
  92. if (n < 0){
  93. n = 0;
  94. } else if (n > _container.width / _visible_width - 1){
  95. n = _container.width / _visible_width - 1;
  96. }
  97. TweenMax.to(_container, 1, {x: -n * _visible_width, ease:Strong.easeOut , onComplete:function():void {
  98. dispatchEvent(new SliderEvent(SliderEvent.CHANGE,n));
  99. }})
  100. }
  101.  
  102. //if you have a lot of vectors, set bitmapMode to true for better performance
  103. public function set bitmapMode(_newBitmapMode:Boolean):void{
  104. if(_newBitmapMode){
  105. _container.cacheAsBitmap = true;
  106. } else {
  107. _container.cacheAsBitmap = false;
  108. }
  109. }
  110.  
  111. ///////////////////////////////////////////////////////////////////////////////////////////
  112. // Internal functions
  113.  
  114. private function attach(event:Event = null):void {
  115. addEventListener( MouseEvent.MOUSE_DOWN, _onMouseDown);
  116. addEventListener( Event.ENTER_FRAME, onEnterFrame);
  117. }
  118.  
  119. private function dettach(event:Event = null):void {
  120. removeEventListener( MouseEvent.MOUSE_DOWN, _onMouseDown);
  121. removeEventListener( Event.ENTER_FRAME, onEnterFrame);
  122. _onMouseUp();
  123. }
  124.  
  125. private function onEnterFrame(e:Event):void {
  126. if (_mouseDown){
  127. _velocity *= MOUSE_DOWN_DECAY;
  128. } else{
  129. _velocity *= DECAY;
  130. }
  131. }
  132.  
  133. private function _onMouseUp(e:MouseEvent = null):void {
  134. if (_mouseDown) {
  135.  
  136. _mouseDown = false
  137. removeEventListener( MouseEvent.MOUSE_UP, _onMouseUp);
  138. removeEventListener( MouseEvent.MOUSE_MOVE, _onMouseMove);
  139.  
  140. _page = getPage();
  141.  
  142. slidePadding = setSlidePadding();
  143.  
  144. var id:Number;
  145. if (_container.x > -_page * _visible_width){
  146. id = Math.floor( -_container.x / _visible_width) + 1;
  147. } else{
  148. id = Math.floor( -_container.x / _visible_width);
  149. }
  150.  
  151. if (_container.x < -id * _visible_width - slidePadding){
  152. setPage(id + 1);
  153. } else if (_container.x < -id * _visible_width + slidePadding){
  154. setPage(id);
  155. } else{
  156. setPage(id - 1);
  157. }
  158. }
  159.  
  160. }
  161.  
  162. private function _onMouseDown(e:MouseEvent):void {
  163. if (!_mouseDown) {
  164. _mouseDown = true;
  165. _mouseDownX = _container.x;
  166. _mouseDownPoint = new Point(this.mouseX, this.mouseY);
  167. _lastMouseDownPoint = _mouseDownPoint;
  168.  
  169. addEventListener(MouseEvent.MOUSE_MOVE, _onMouseMove);
  170. addEventListener(MouseEvent.MOUSE_UP, _onMouseUp);
  171.  
  172. TweenMax.killTweensOf(_container);
  173. }
  174. }
  175.  
  176. private function _onMouseMove(e:MouseEvent):void {
  177. if (_mouseDown) {
  178. var point:Point = new Point(this.mouseX, this.mouseY)
  179. _container.x = _mouseDownX + (point.x - _mouseDownPoint.x)
  180. _velocity += (point.x - _lastMouseDownPoint.x) * SPEED_SPRINGNESS
  181.  
  182. slidePadding = setSlidePadding()
  183.  
  184. _lastMouseDownPoint = point
  185. }
  186. e.updateAfterEvent();
  187. }
  188.  
  189. //internal
  190. private function setSlidePadding():Number{
  191. if (Math.abs(_velocity) < _speedLimit){
  192. return _visible_width / 2 - _padding;
  193. }
  194. return _padding;
  195. }
  196.  
  197. //internal
  198. private function getPage():Number{
  199. return Math.floor((this.mouseX - _container.x) / _visible_width)
  200. }
  201.  
  202. //////////////////////////////////////////////////////////////////////////////////////////////////
  203.  
  204. //return array of all pages
  205. public function get pages():Array{
  206. return _pages;
  207. }
  208.  
  209. public function get sliderWidth():Number{
  210. return _visible_width;
  211. }
  212.  
  213. public function get sliderHeight():Number{
  214. return _visible_height;
  215. }
  216.  
  217. ///// DESTROY
  218.  
  219.  
  220. //use this to destroy and remove all the listeners
  221. public function destroy():void {
  222. try{
  223. this.dettach();
  224.  
  225. removeEventListener(MouseEvent.ROLL_OVER, attach);
  226. removeEventListener(MouseEvent.ROLL_OUT, dettach);
  227.  
  228. removeEventListener( MouseEvent.MOUSE_UP, _onMouseUp);
  229. removeEventListener( MouseEvent.MOUSE_MOVE, _onMouseMove);
  230.  
  231. removeChild(_container);
  232. _container = null;
  233. } catch(e:Error){ trace("[SliderError: ] " + e.message) }
  234. }
  235.  
  236. }//end
  237. }

Report this snippet  

You need to login to post a comment.