# Thread: Rotation problem - AS2.0

1. ## Rotation problem - AS2.0

I haven't coded in 2 years, been trying to solve this for almost 2 days now. I know there is an easy solution to this but I couldn't find it in a search:

I have a movieclip which moves in the direction its facing, and rotates left/right in order to face another movieclip. Occasionally the rotation will think that the longer direction is shorter, and vice-versa.

If this has been asked a hundred times, just redirect me and delete the thread, but I couldn't find it.

( Flash CS3 - ActionScript 2.0: )

Code:
```onClipEvent (load) {
speed = 2;
}
onClipEvent (enterFrame) {
target_angle = (Math.atan2(_root._ymouse-this._y, _root._xmouse-this._x)*180/Math.PI)-90;
if (this._rotation>target_angle) {
this._rotation -= 5;
} else if (this._rotation<target_angle) {
this._rotation += 5;
}
xspeed = Math.sin((this._rotation+180)*(Math.PI/180))*2;
yspeed = Math.cos((this._rotation+180)*(Math.PI/180))*2*-1;
this._x += xspeed*speed;
this._y += yspeed*speed;
}```

2. Hey StringTheory1, i was looking around and saw your post and decided to give it a try, it's actually a very simple fix, just replace your rotation portion with this:

Code Actionscript:
```/* All I did was make your code cleaner and easier to read, added the fix for that rotation problem, and changed the rotation of your movieclip on stage */
speed = 2;
rotSpeed = 5;
}
onClipEvent (enterFrame) {
target_angle = (Math.atan2(_root._ymouse-this._y, _root._xmouse-this._x)/(Math.PI/180))+180;//add 180 so we don't get negative numbers, ex. only #'s 0-360
solvedRotation = this._rotation+180//make sure we dont get negatives with the rotation
/*
What the next block does is it finds the distance between solvedRotation and target_angle
and will either + or - based on which is under 180, so you dont have the weird make a full 360
to get to a spot that was 3 degrees away
*/
if(target_angle-solvedRotation<-180){
this._rotation+=rotSpeed;
}else if(target_angle-solvedRotation>180){
this._rotation-=rotSpeed;
}else{
if (solvedRotation>target_angle+3) {
this._rotation -= rotSpeed;
}
/* We subtract and add a little bit to target_angle so the movieclip won't "shake"
remove the changes and you'll see what i mean */
if (solvedRotation<target_angle-3) {
this._rotation += rotSpeed;
}
}
xspeed = Math.cos(Math.PI/180 * this._rotation)*speed
yspeed = Math.sin(Math.PI/180 * this._rotation)*speed
this._x += xspeed
this._y += yspeed
}
/* That's it!
e-mail me if you need help
[email]andy@crockettfarms.com[/email]
*/```

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•