鼠标按下,获取器材内部鼠标按下的位置(不动点)。
鼠标移动,旋转器材,使得旋转之后,不动点仍然在鼠标下方。
实现方式:记住上一次的旋转角度,计算出旋转了多少度(可以用全局坐标系),然后加上去。
ActionScript3.0实现代码如下(点击拖动TouchRotItem2,旋转TouchRotItem2的parent):
package { import flash.display.MovieClip; import flash.display.MovieClip; import flash.display.Sprite; import flash.events.MouseEvent; import flash.geom.Point; /** * ... * @author hanyeah */ public class TouchRotItem2 extends MovieClip { protected var lastAngle: Number; public function TouchRotItem2() { super(); addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler); } private function mouseDownHandler(e:MouseEvent):void { e.stopPropagation(); lastAngle = calcAngle(new Point(e.stageX, e.stageY)); stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler); stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler); } private function mouseMoveHandler(e:MouseEvent):void { var angle: Number = calcAngle(new Point(e.stageX, e.stageY)); parent.rotation += angle - lastAngle; lastAngle = angle; } private function mouseUpHandler(e:MouseEvent):void { stage.removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandler); stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler); } private function calcAngle(globalP: Point): Number { var p: Point = parent.parent.globalToLocal(globalP); return Math.atan2(p.y - parent.y, p.x - parent.x) * 180 / Math.PI; } } }
发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。