Return to Snippet

Revision: 56832
at April 18, 2012 18:33 by burnandbass


Initial Code
package com.burnandbass {
	
	import flash.display.*;
	import flash.events.MouseEvent;
	import flash.events.Event;
	import flash.geom.Point;
	import com.greensock.TweenMax;
	import com.greensock.easing.Strong;
	
	/*
	ActiveSwipe - iPhone like flash slider
	Version 1.0 - nov 7 2011
	Author: Hristo Panayotov ( burnandbass[at]gmail.com )
	
	
	*/
	
	public class Slider extends Sprite {
		
		//const for internal function
		private static const MOUSE_DOWN_DECAY:		Number 	= 0.5
        private static const SPEED_SPRINGNESS:		Number 	= 0.4
		private static const DECAY:					Number	= 0.93
		
		private var _container:Sprite;
		private var _visible_rect:Sprite = new Sprite();
		
		//variables for slide logic
		private var _visible_width:Number;
		private var _visible_height:Number;
		private var _mouseDown:Boolean;
		private var _velocity:Number = 0;
		private var _mouseDownX:Number = 0;
		private var _mouseDownPoint:Point;
		private var _lastMouseDownPoint:Point;
		private var slidePadding:Number = 0;
		private var _padding:Number;
		private var _page:Number = 0;
		
		//used for setting up the slidePadding
		private static const _speedLimit:Number = 20
		
		//all the pages
		private var _pages:Array = [];
		
		public function Slider(sliderWidth:Number = 240, sliderHeight:Number = 320, padding:Number = 10){
			_container = new Sprite();
			_visible_width = sliderWidth;
			_visible_height = sliderHeight;
			_padding = padding;
			
			addChild(_container);
			
			//draw mask
			_visible_rect.graphics.beginFill(0xffffff);
			_visible_rect.graphics.drawRect(0,0,_visible_width,_visible_height);
			addChild(_visible_rect);
			this.mask = _visible_rect;
		}
		
		//call this after you add all pages
		public function initSlider():void{
			this.attach();
			addEventListener(MouseEvent.ROLL_OVER, attach);
			addEventListener(MouseEvent.ROLL_OUT, dettach);
		}
		
		//this function add page to the slider. Make shure it is the same width and hight as the slider
		//if the pages (screens) are not the same dimensions, use addForceSizePage() method
		public function addPage(_newPage:DisplayObject):void{
			_pages.push(_newPage);
			_container.addChild(_newPage);
			_newPage.x = (_visible_width*_pages.length) - _visible_width;
			
			dispatchEvent(new SliderEvent(SliderEvent.PAGE_ADDED));
			
			if(_newPage.width != _visible_width){
				trace("Slider error: the page you want to add have differenr width: \nSlider width: " 
					  + _visible_width + "   ||  page width: " + _newPage.width + " . Some errors may occure!");
			}
		}
		
		//the same as addPage(), but forses the size to be exactly they should be for this screen
		public function addForceSizePage(_newPage:DisplayObject):void{
			_newPage.width = _visible_width;
			_newPage.height = _visible_height;
			addPage(_newPage);
		}
		
		//slide to page. The first page have index 0 (setPage(2) will lead you to third page);
		public function setPage(n:Number):void{
			if (n < 0){
				n = 0;
			} else if (n > _container.width / _visible_width - 1){
				n = _container.width / _visible_width - 1;
			}
			TweenMax.to(_container, 1, {x: -n * _visible_width, ease:Strong.easeOut , onComplete:function():void { 
						dispatchEvent(new SliderEvent(SliderEvent.CHANGE,n)); 
						}})
		}
		
		//if you have a lot of vectors, set bitmapMode to true for better performance
		public function set bitmapMode(_newBitmapMode:Boolean):void{
			if(_newBitmapMode){
				_container.cacheAsBitmap = true;
			} else {
				_container.cacheAsBitmap = false;
			}
		}
		
		///////////////////////////////////////////////////////////////////////////////////////////
		//   Internal functions
		
		private function attach(event:Event = null):void {
			addEventListener( MouseEvent.MOUSE_DOWN, _onMouseDown);
			addEventListener( Event.ENTER_FRAME, onEnterFrame);
		}
		
		private function dettach(event:Event = null):void {
			removeEventListener( MouseEvent.MOUSE_DOWN, _onMouseDown);
			removeEventListener( Event.ENTER_FRAME, onEnterFrame);
			_onMouseUp();
		}
		
		private function onEnterFrame(e:Event):void {
			if (_mouseDown){
				_velocity *= MOUSE_DOWN_DECAY;
			} else{
				_velocity *= DECAY;
			}
		}
		
		private function _onMouseUp(e:MouseEvent = null):void {
			if (_mouseDown) {
				
				_mouseDown = false
				removeEventListener( MouseEvent.MOUSE_UP, _onMouseUp);
				removeEventListener( MouseEvent.MOUSE_MOVE, _onMouseMove);
				
				_page = getPage();
				
				slidePadding = setSlidePadding();
				
				var id:Number;
				if (_container.x > -_page * _visible_width){
					id = Math.floor( -_container.x / _visible_width) + 1;
				} else{
					id = Math.floor( -_container.x / _visible_width);
				}
				
				if (_container.x < -id * _visible_width - slidePadding){
					setPage(id + 1);
				} else if (_container.x < -id * _visible_width + slidePadding){
					setPage(id);
				} else{
					setPage(id - 1);
				}
			}
			
		}
		
		private function _onMouseDown(e:MouseEvent):void {
			if (!_mouseDown) {
				_mouseDown = true;
				_mouseDownX = _container.x;
				_mouseDownPoint = new Point(this.mouseX, this.mouseY);
				_lastMouseDownPoint = _mouseDownPoint;
				
				addEventListener(MouseEvent.MOUSE_MOVE, _onMouseMove);
				addEventListener(MouseEvent.MOUSE_UP, _onMouseUp);
				
				TweenMax.killTweensOf(_container);
			}
		}
		
		private function _onMouseMove(e:MouseEvent):void {
			if (_mouseDown) {
				var point:Point = new Point(this.mouseX, this.mouseY)
				_container.x = _mouseDownX + (point.x - _mouseDownPoint.x)
				_velocity += (point.x - _lastMouseDownPoint.x) * SPEED_SPRINGNESS
				
				slidePadding = setSlidePadding()
			
				_lastMouseDownPoint = point
			}
			e.updateAfterEvent();
		}
		
		//internal
		private function setSlidePadding():Number{
			if (Math.abs(_velocity) < _speedLimit){
				return _visible_width / 2 - _padding;
			}
			return _padding;
		}
		
		//internal
		private function getPage():Number{
			return Math.floor((this.mouseX - _container.x) / _visible_width) 
		}
		
		//////////////////////////////////////////////////////////////////////////////////////////////////
		
		//return array of all pages
		public function get pages():Array{
			return _pages;
		}
		
		public function get sliderWidth():Number{
			return _visible_width;
		}
		
		public function get sliderHeight():Number{
			return _visible_height;
		}
		
		///// DESTROY
		
		
		//use this to destroy and remove all the listeners
		public function destroy():void {
			try{
				this.dettach();
				
				removeEventListener(MouseEvent.ROLL_OVER, attach);
				removeEventListener(MouseEvent.ROLL_OUT, dettach);
				
				removeEventListener( MouseEvent.MOUSE_UP, _onMouseUp);
				removeEventListener( MouseEvent.MOUSE_MOVE, _onMouseMove);
				
				removeChild(_container);
				_container = null;
			} catch(e:Error){ trace("[SliderError: ] " + e.message) }
		}
		
	}//end
}

Initial URL


Initial Description
by Bassta

Initial Title
ActionScript3 Touch Event

Initial Tags


Initial Language
ActionScript 3