屏幕震动效果

package tools {

    import flash.display.DisplayObject;

    import flash.events.TimerEvent;

    import flash.utils.Timer;    



    public class Earthquake {

       /**

        * 参数

        */

        private var _timer:Timer;

        private var _displayObject:DisplayObject;

        private var _originalX:int;

        private var _originalY:int;

        private var _intensity:int;

        private var _intensityOffset:int;

		/**

		 * 时间

		 */

		private var _seconds:Number;

		/**

		 * 方向

		 */

		public static const LEFT_RIGHT_UP_DOWN:int = 0;

		public static const LEFT_RIGHT:int = 1;

		public static const UP_DOWN:int = 2;

		/**

		 * 帧频

		 */

		public static const FRAME_RATE:int = 60;

		/**

		 * 方向

		 */

		private var _direction:int;

		/**

		 * 震动中

		 */

		private var _bShaking:Boolean = false;

		/**

		 * 是否自动销毁

		 */

		private var _bDestroy:Boolean = false;

		

		 /**

		  * 对一个显示对象应用地震效果。

		  * @param	displayObject 抖动对象

		  * @param	intensity 强度

		  * @param	seconds 持续时间 秒

		  * @param	direction 方向

		  * @param	autoDestroy 自动销毁

		  */

		public function Earthquake(displayObject:DisplayObject, intensity:Number = 10, seconds:Number = 1, direction:int = 0, autoDestroy:Boolean = false):void

		{

            _displayObject = displayObject;

            _originalX = displayObject.x;

            _originalY = displayObject.y;

			_intensity = intensity;

            _intensityOffset = intensity / 2;

			_seconds = seconds;

			_direction = direction;

			_bDestroy = autoDestroy;

		}

		/**

		 * 开始后抖动

		 */

		public function go():void

		{

			if (_bShaking) return;

			_bShaking = true;

			var msPerUpdate:int = int(1000 / FRAME_RATE);

            var totalUpdates:int = int(_seconds * 1000 / msPerUpdate);

			

			if (!_timer)

			{

				_timer = new Timer(msPerUpdate, totalUpdates);

				_timer.addEventListener(TimerEvent.TIMER, quake);

				_timer.addEventListener(TimerEvent.TIMER_COMPLETE, resetImage);

			}

			else

			{

				_displayObject.x = _originalX;

				_displayObject.y = _originalY;

				_timer.reset();

			}

            _timer.start();

		}

		/**

		 * 抖动

		 * @param	event

		 */

        private function quake( event:TimerEvent ):void 

		{

			var newX:int = _originalX;

			var newY:int = _originalY;

			

			switch(_direction)

			{

				case LEFT_RIGHT_UP_DOWN:

					newX = _originalX + Math.random() * _intensity - _intensityOffset;

					newY = _originalY + Math.random() * _intensity - _intensityOffset;

					break;

				case LEFT_RIGHT:

					newX = _originalX + Math.random() * _intensity - _intensityOffset;

					break;

				case UP_DOWN:

					newY = _originalY + Math.random() * _intensity - _intensityOffset;

					break;

			}

            _displayObject.x = newX;

            _displayObject.y = newY;

        }

		/**

		 * 重置

		 * @param	event

		 */

        private function resetImage( event:TimerEvent = null ):void

		{

            _displayObject.x = _originalX;

            _displayObject.y = _originalY;

            cleanup();

        }

		/**

		 * 清除

		 */

        private function cleanup():void 

		{

			_bShaking = false;

            /*_timer = null;

			_timer.stop();*/

			_timer.reset();

			if (_bDestroy)

			{

				_timer.removeEventListener(TimerEvent.TIMER, quake);

				_timer.removeEventListener(TimerEvent.TIMER_COMPLETE, resetImage);

				_timer = null;

				_displayObject = null;

			}

			delete [this];

            //_displayObject = null;

        }

    }

}

 

你可能感兴趣的:(屏幕)