下面是一个设置任意旋转中心的类:
先看效果
1 //Copyright © 2008. Http://L4cd.Net All Rights Reserved.
2 //使用方法也很简单~
3 //import net.L4cd.iPointRegister;
4 //iPointRegister.register(mc,new Point(新的旋转点x,新的旋转点y));
5 //iPointRegister.rotate(mc,角度);
6
7 //需要注意的点..使用register注册新的点时..是当前的状态为去注册旋转点,
8 //就是说如果当前mc已经转了一定的角度..重新注册旋转点..与没旋转过的mc注册旋转点时位置会不一样..
9 //这里是需要注册的地方..
10
11 package net.L4cd
12 {
13 import flash.display.DisplayObject;
14 import flash.geom.Point;
15 import flash.utils.Dictionary;
16
17
18 /**
19 * 动态“修改”注册点,实现任意点旋转
20 *
21 * @author L4cd.Net
22 * @playerversion Flash player 9
23 * @langversion 3.0
24 * @version 2008/9/1 16:28
25 */
26 public class iPointRegister
27 {
28 static private var _target:Dictionary;
29 public function iPointRegister()
30 {
31
32 }
33
34 /**
35 * 注册旋转点
36 *
37 * @param target 目标
38 * @param point 新注册点
39 */
40 static public function register(target:DisplayObject, point:Point):void
41 {
42 if (!_target)_target = new Dictionary();
43 _target[target] = { };
44 var s:Object = _target[target];
45 s["x"] = point.x;
46 s["y"] = point.y;
47 s["r"] = Point.distance(new Point(target.x, target.y), new Point(s["x"], s["y"]));
48 s["d"] = Math.asin(Math.abs(s["y"] - target.y)/s["r"])/(Math.PI / 180);
49 if (s["x"] > target.x && s["y"] < target.y)
50 {
51 s["d"] = 180 - s["d"];
52 }else if (s["x"] > target.x && s["y"] > target.y)
53 {
54 s["d"] += 180;
55 }else if (s["x"] < target.x && s["y"] > target.y)
56 {
57 s["d"] = 360 - s["d"];
58 }
59 }
60
61 /**
62 * 取消注册
63 *
64 * @param target 目标
65 */
66 static public function unregister(target:DisplayObject):void
67 {
68 if (!_target) return;
69 if (!_target[target]) return;
70 _target[target] = null;
71 }
72
73 /**
74 * 以新的注册点旋转
75 *
76 * @param target 目标
77 * @param rotation 角度
78 */
79 static public function rotate(target:DisplayObject,rotation:Number):void
80 {
81 if (!_target) return;
82 if (!_target[target]) return;
83 var s:Object = _target[target];
84 var r:Number = rotation - target.rotation;
85 target.rotation = rotation;
86 s["d"] += r;
87 target.x = s["x"] + s["r"] * Math.cos((s["d"]) * Math.PI / 180);
88 target.y = s["y"] + s["r"] * Math.sin((s["d"]) * Math.PI / 180);
89 }
90 }
91
92 }
2 //使用方法也很简单~
3 //import net.L4cd.iPointRegister;
4 //iPointRegister.register(mc,new Point(新的旋转点x,新的旋转点y));
5 //iPointRegister.rotate(mc,角度);
6
7 //需要注意的点..使用register注册新的点时..是当前的状态为去注册旋转点,
8 //就是说如果当前mc已经转了一定的角度..重新注册旋转点..与没旋转过的mc注册旋转点时位置会不一样..
9 //这里是需要注册的地方..
10
11 package net.L4cd
12 {
13 import flash.display.DisplayObject;
14 import flash.geom.Point;
15 import flash.utils.Dictionary;
16
17
18 /**
19 * 动态“修改”注册点,实现任意点旋转
20 *
21 * @author L4cd.Net
22 * @playerversion Flash player 9
23 * @langversion 3.0
24 * @version 2008/9/1 16:28
25 */
26 public class iPointRegister
27 {
28 static private var _target:Dictionary;
29 public function iPointRegister()
30 {
31
32 }
33
34 /**
35 * 注册旋转点
36 *
37 * @param target 目标
38 * @param point 新注册点
39 */
40 static public function register(target:DisplayObject, point:Point):void
41 {
42 if (!_target)_target = new Dictionary();
43 _target[target] = { };
44 var s:Object = _target[target];
45 s["x"] = point.x;
46 s["y"] = point.y;
47 s["r"] = Point.distance(new Point(target.x, target.y), new Point(s["x"], s["y"]));
48 s["d"] = Math.asin(Math.abs(s["y"] - target.y)/s["r"])/(Math.PI / 180);
49 if (s["x"] > target.x && s["y"] < target.y)
50 {
51 s["d"] = 180 - s["d"];
52 }else if (s["x"] > target.x && s["y"] > target.y)
53 {
54 s["d"] += 180;
55 }else if (s["x"] < target.x && s["y"] > target.y)
56 {
57 s["d"] = 360 - s["d"];
58 }
59 }
60
61 /**
62 * 取消注册
63 *
64 * @param target 目标
65 */
66 static public function unregister(target:DisplayObject):void
67 {
68 if (!_target) return;
69 if (!_target[target]) return;
70 _target[target] = null;
71 }
72
73 /**
74 * 以新的注册点旋转
75 *
76 * @param target 目标
77 * @param rotation 角度
78 */
79 static public function rotate(target:DisplayObject,rotation:Number):void
80 {
81 if (!_target) return;
82 if (!_target[target]) return;
83 var s:Object = _target[target];
84 var r:Number = rotation - target.rotation;
85 target.rotation = rotation;
86 s["d"] += r;
87 target.x = s["x"] + s["r"] * Math.cos((s["d"]) * Math.PI / 180);
88 target.y = s["y"] + s["r"] * Math.sin((s["d"]) * Math.PI / 180);
89 }
90 }
91
92 }
fla代码
1 import net.L4cd.iPointRegister;
2
3 var s:Array = [];
4 var sh:Sprite = new Sprite()
5 addChild(sh)
6
7
8 var a:Sprite = new Sprite();
9 a.mouseEnabled = false;
10 a.alpha = 0.5
11 a.graphics.beginFill(0xFF9900);
12 a.graphics.drawRect(0,0,100,100);
13 a.graphics.endFill();
14 a.x = 150;
15 a.y = 150;
16 addChild(a);
17 var f:TextField = new TextField();f.htmlText = "<font size='90'>国</font>";
18 var bit:BitmapData = new BitmapData(100,100,true,0x00FFFFFF);
19 bit.draw(f)
20 a.addChild(new Bitmap(bit));
21
22
23
24
25 draw(100,100)
26 function draw(x:Number,y:Number):void
27 {
28 s["x"] = x;
29 s["y"] = y;
30 graphics.clear()
31 graphics.lineStyle(1)
32 graphics.moveTo(0,y)
33 graphics.lineTo(550,y)
34 graphics.moveTo(x,0)
35 graphics.lineTo(x,400)
36
37 sh.graphics.clear()
38 sh.graphics.lineStyle(1)
39 sh.graphics.moveTo(s["x"],s["y"])
40 sh.graphics.lineTo(a.x,a.y)
41 }
42
43
44
45 bt.addEventListener(MouseEvent.CLICK,click)
46 function click(e:MouseEvent):void
47 {
48 if(e.target["label"] == "start")
49 {
50 e.target["label"] = "stop";
51 iPointRegister.register(a,new Point(s["x"],s["y"]));
52 addEventListener(Event.ENTER_FRAME,r);
53 stage.removeEventListener(MouseEvent.MOUSE_DOWN,d)
54 }else
55 {
56 e.target["label"] = "start";
57 removeEventListener(Event.ENTER_FRAME,r);
58 stage.addEventListener(MouseEvent.MOUSE_DOWN,d)
59 }
60
61 }
62 stage.addEventListener(MouseEvent.MOUSE_DOWN,d)
63 function d(e:MouseEvent):void
64 {
65 if(e.currentTarget!=e.target)return;
66 stage.addEventListener(MouseEvent.MOUSE_MOVE,m)
67 stage.addEventListener(MouseEvent.MOUSE_UP,u)
68 m(e)
69 }
70 function m(e:MouseEvent):void
71 {
72 draw(e.stageX,e.stageY)
73 }
74 function u(e:MouseEvent):void
75 {
76 stage.removeEventListener(MouseEvent.MOUSE_MOVE,m)
77 stage.removeEventListener(MouseEvent.MOUSE_UP,u)
78 }
79
80
81
82
83 function r(e:Event):void
84 {
85 iPointRegister.rotate(a,a.rotation+1);
86 sh.graphics.clear()
87 sh.graphics.lineStyle(1)
88 sh.graphics.moveTo(s["x"],s["y"])
89 sh.graphics.lineTo(a.x,a.y)
90 }
2
3 var s:Array = [];
4 var sh:Sprite = new Sprite()
5 addChild(sh)
6
7
8 var a:Sprite = new Sprite();
9 a.mouseEnabled = false;
10 a.alpha = 0.5
11 a.graphics.beginFill(0xFF9900);
12 a.graphics.drawRect(0,0,100,100);
13 a.graphics.endFill();
14 a.x = 150;
15 a.y = 150;
16 addChild(a);
17 var f:TextField = new TextField();f.htmlText = "<font size='90'>国</font>";
18 var bit:BitmapData = new BitmapData(100,100,true,0x00FFFFFF);
19 bit.draw(f)
20 a.addChild(new Bitmap(bit));
21
22
23
24
25 draw(100,100)
26 function draw(x:Number,y:Number):void
27 {
28 s["x"] = x;
29 s["y"] = y;
30 graphics.clear()
31 graphics.lineStyle(1)
32 graphics.moveTo(0,y)
33 graphics.lineTo(550,y)
34 graphics.moveTo(x,0)
35 graphics.lineTo(x,400)
36
37 sh.graphics.clear()
38 sh.graphics.lineStyle(1)
39 sh.graphics.moveTo(s["x"],s["y"])
40 sh.graphics.lineTo(a.x,a.y)
41 }
42
43
44
45 bt.addEventListener(MouseEvent.CLICK,click)
46 function click(e:MouseEvent):void
47 {
48 if(e.target["label"] == "start")
49 {
50 e.target["label"] = "stop";
51 iPointRegister.register(a,new Point(s["x"],s["y"]));
52 addEventListener(Event.ENTER_FRAME,r);
53 stage.removeEventListener(MouseEvent.MOUSE_DOWN,d)
54 }else
55 {
56 e.target["label"] = "start";
57 removeEventListener(Event.ENTER_FRAME,r);
58 stage.addEventListener(MouseEvent.MOUSE_DOWN,d)
59 }
60
61 }
62 stage.addEventListener(MouseEvent.MOUSE_DOWN,d)
63 function d(e:MouseEvent):void
64 {
65 if(e.currentTarget!=e.target)return;
66 stage.addEventListener(MouseEvent.MOUSE_MOVE,m)
67 stage.addEventListener(MouseEvent.MOUSE_UP,u)
68 m(e)
69 }
70 function m(e:MouseEvent):void
71 {
72 draw(e.stageX,e.stageY)
73 }
74 function u(e:MouseEvent):void
75 {
76 stage.removeEventListener(MouseEvent.MOUSE_MOVE,m)
77 stage.removeEventListener(MouseEvent.MOUSE_UP,u)
78 }
79
80
81
82
83 function r(e:Event):void
84 {
85 iPointRegister.rotate(a,a.rotation+1);
86 sh.graphics.clear()
87 sh.graphics.lineStyle(1)
88 sh.graphics.moveTo(s["x"],s["y"])
89 sh.graphics.lineTo(a.x,a.y)
90 }
利用matrix改变旋转位置的基本使用方法:
1 var a:MovieClip =new mc();
2 addChild(a);
3 var myMatrix:Matrix=a.transform.matrix;//复制矩阵
4
5 //计算坐标原点
6
7 var tempx:Number =a.x+20;
8
9 var tempy:Number =a.y+20;
10
11 //对自定义Matrx进行操作
12 //myMatrix.translate(-tempx,-tempy);
13 //myMatrix.identity ()
14 myMatrix.tx=myMatrix.ty=0;
15 myMatrix.translate(-20,-20);
16
17 myMatrix.rotate(2 * Math.PI * (30 / 360));
18
19 myMatrix.translate(tempx,tempy);
20
21 //应用变换到显示对象
22
23 a.transform.matrix = myMatrix;
24
2 addChild(a);
3 var myMatrix:Matrix=a.transform.matrix;//复制矩阵
4
5 //计算坐标原点
6
7 var tempx:Number =a.x+20;
8
9 var tempy:Number =a.y+20;
10
11 //对自定义Matrx进行操作
12 //myMatrix.translate(-tempx,-tempy);
13 //myMatrix.identity ()
14 myMatrix.tx=myMatrix.ty=0;
15 myMatrix.translate(-20,-20);
16
17 myMatrix.rotate(2 * Math.PI * (30 / 360));
18
19 myMatrix.translate(tempx,tempy);
20
21 //应用变换到显示对象
22
23 a.transform.matrix = myMatrix;
24
不好意思啊,才知道的方法:
import fl.motion.MatrixTransformer;
fang.addEventListener(MouseEvent.CLICK,rota);
function rota(e:MouseEvent) {
var m:Matrix=e.currentTarget.transform.matrix;
//MatrixTransformer.rotateAroundInternalPoint(m,e.currentTarget.mouseX,e.currentTarget.mouseY,1);
MatrixTransformer.rotateAroundExternalPoint(m,a.x,a.y,1);
fang.transform.matrix=m;
}
fang.addEventListener(MouseEvent.CLICK,rota);
function rota(e:MouseEvent) {
var m:Matrix=e.currentTarget.transform.matrix;
//MatrixTransformer.rotateAroundInternalPoint(m,e.currentTarget.mouseX,e.currentTarget.mouseY,1);
MatrixTransformer.rotateAroundExternalPoint(m,a.x,a.y,1);
fang.transform.matrix=m;
}