My favorites | Sign in
Project Home Downloads Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package com.everydayflash.util {

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:Object;

/**
* 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:Object, 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;
}
}
}











Change log

r14 by drojdjou on Aug 25, 2008   Diff
[No log message]
Go to: 
Project members, sign in to write a code review

Older revisions

r10 by drojdjou on Jun 10, 2008   Diff
[No log message]
r9 by drojdjou on May 27, 2008   Diff
[No log message]
r8 by drojdjou on May 20, 2008   Diff
[No log message]
All revisions of this file

File info

Size: 3412 bytes, 116 lines
Powered by Google Project Hosting