Rotator类,第三方类

/**
 * Copyright (c) 2008 Bartek Drozdz (http://www.everydayflash.com)
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */

 package  {
	
	import flash.display.DisplayObject;
	import flash.geom.Point;

	/**
	 * This class can be used as a replacement for the DisplayObject.rotation property.
	 * 
	 * It rotates a DisplayObject, but it does not limit itself to rotate around the it's 
	 * registration point, instead it can rotate the object around any point. The point is
	 * defined in the objects parent coodrinate system.
	 * 
	 * @author Bartek Drozdz (http://www.everydayflash.com)
	 * @version 1.0
	 */
	public class Rotator {
		
		private var target:DisplayObject;
		
		/**
		 * A value that is based on the initial rotation of the display object itself, and
		 * the angle between the registration point of the display object and of the rotator
		 */
		private var offset:Number;
		
		/**
		 * Registration point - the point around which the rotation takse place
		 */
		private var point:Point;
		
		/**
		 * Distance between the registration point of the display object and the registration 
		 * point of the rotator
		 */
		private var dist:Number;

		/**
		 * Registers a DisplayObject that will be rotated and an registration Point around which it will be rotated.
		 * 
		 * @param	target DisplayObject to rotate
		 * @param	registrationPoint Point containing the coodrinates around which the object should be rotated 
		 *          (in the targets parent coordinate space) If omitted, the displays object x and y coordinates are used
		 */
		public function Rotator(target:DisplayObject, registrationPoint:Point=null) {
			this.target = target;
			setRegistrationPoint(registrationPoint);
		}
		
		/**
		 * Once set in the constructor, the rotation registration point can be modified an any moment
		 * 
		 * @param	registrationPoint, if null defaults to targets x and y coordinates
		 */
		public function setRegistrationPoint(registrationPoint:Point=null):void {
			if (registrationPoint == null) point = new Point(target.x, target.y);
			else point = registrationPoint;
			
			var dx:Number = point.x - target.x;
			var dy:Number = point.y - target.y;
			dist = Math.sqrt( dx * dx + dy * dy );
			
			var a:Number = Math.atan2(dy, dx) * 180 / Math.PI;
			offset = 180 - a + target.rotation;
		}
		
		/**
		 * Sets the rotation to the angle passed as parameter.
		 * 
		 * Since it uses a getter/setter Rotator can easily be used with Tween or Tweener classes.
		 */
		public function set rotation(angle:Number):void {
			var tp:Point = new Point(target.x, target.y);

			var ra:Number = (angle - offset) * Math.PI / 180;
			
			target.x = point.x + Math.cos(ra) * dist;
			target.y = point.y + Math.sin(ra) * dist;
			
			target.rotation =  angle;
		}
		
		/**
		 * Returns current rotation of the target in degrees
		 */
		public function get rotation():Number {
			return target.rotation;
		}
		
		/**
		 * Rotates the target by the angle passed as parameter. 
		 * Works the same as Rotator.rotation += angle;
		 * 
		 * @param angle angle by which to rotate the target DisplayObject
		 */
		public function rotateBy(angle:Number):void {
			var tp:Point = new Point(target.x, target.y);

			var ra:Number = (target.rotation + angle - offset) * Math.PI / 180;
			
			target.x = point.x + Math.cos(ra) * dist;
			target.y = point.y + Math.sin(ra) * dist;
			
			target.rotation =  target.rotation + angle;
		}
	}
}













  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值