13 Replies Latest reply on Jun 16, 2007 7:07 AM by mortentwellmann

# _rotation value problem

I have tried to make a movieclip rotate in relation to the position of the mouse and this was solved by finding a script as follows:

function rotateTowards (clip, targetX, targetY) {
// Create a point object that stores the x and y coordinates of
// the clip relative to its parent's registration point
var clipPoint = {x:clip._x, y:clip._y};

// Convert local clip coordinates to global (Stage) coordinates
clip._parent.localToGlobal(clipPoint);

// Measure the distance between the registration
// point of the clip and the target point
var deltaX = targetX - clipPoint.x;
var deltaY = targetY - clipPoint.y;

// Calculate the angle of the line from the registration point
// of this clip to the mouse

// Convert the radian version of the angle to degrees

// Update the rotation of this clip to point to the mouse
clip._rotation = rotationAngle;
}
}

//The movieclip then has the following code attached:
onClipEvent (enterFrame) {
_root.rotateTowards(this, _root._xmouse, _root._ymouse);
}

This works very well, but I wanted to put a delay on the rotation, i.e. the rotation should follow slowly after having moved the mouse to give a more smooth feel.

To do this I tried calculating the difference between the rotation and then dividing by 16 and assigning this number to the _rotation property and it works fine UNTIL: The value passes the gap from 180 to -180 degrees or -180 degrees. The movieclip suddendly spins the other way around (as it is apparently closer to spin counter-clockwise than clock-wise...)

What is the workaround for this?

- Morten
• ###### 1. Re: _rotation value problem
change:

• ###### 2. Re: _rotation value problem
Thank you, this works well with the speed, however the problem is still the same:

If I hold the mouse to the left of the MC below the center and move it up above the center, the MC rotates counter-clockwise all the back.

The problem is basically when the _rotation value moves up towards 180 degrees (rotating the MC clock-wise) and passes 180 degrees, it goes to -180 degrees instead of continuing to increase (181, 182, 183, etc.). And since the movement passes over several frames, the rotation angle starts going COUNTER-clockwise until it reaches the point close to -180 (i.e. 179, 178, 177, ..., 0, -1, -2, ..., -179).

The exact problem occurs when the mouse is moved counter-clockwise and passes the center on the X-axis, where it then rotates all the way back, clockwise.
• ###### 3. Re: _rotation value problem
what do you mean by, "...the movement passes over several frames..."?
• ###### 4. Re: _rotation value problem
I have uploaded the SWF, so you can see how it "jumps back".

http://www.twellmann.dk/flash/rotate.swf

- Morten
• ###### 5. Re: _rotation value problem
and what's that have to do with "...the movement passes over several frames..."?
• ###### 6. Re: _rotation value problem
What I mean is just that the new rotation based on where the mouse is, is not assigned to the MC in one frame, but several frames, because of the "speed" variable.

If the MC would get the exact value of the mouse position in one frame, there would be no delay and then it wouldn't matter if it spun one way or the other, but as it takes several frames for the MC to rotate to the final destination, it is visible. That is why I uploaded the Flash file to illustrate it.
• ###### 7. Re: _rotation value problem
i can't duplicate that problem with the shown code.

that problem usually occurs because of direct updating of an objects _rotation property instead of updating a variable and assigning that variable to the objects _rotation property.

however, the shown code does just what is required to prevent the problem. if you're using that code, post a link to your fla.
• ###### 8. Re: _rotation value problem
hmm, okay.
I'm not exactly sure what you mean, so here is the FLA:

http://www.twellmann.dk/flash/rotate.fla

- Morten
• ###### 9. _rotation value problem
Is this what you're trying to achieve? I wasn't sure exactly what you wanted the behaviour to be.

I removed your onClipEvent handler an put it in the frame...and gave the sunflower the instance name flower just so I could see what was going on. You can avoid copying the onEnterFrame part and it should work with what you had originally.

• ###### 10. Re: _rotation value problem
THIS IS GREAT!! It works!!

Thank you very much. :)
• ###### 11. Re: _rotation value problem
No problem. I think it works a little better with a lower speed. e.g 0.85. If you circle the mouse too quickly it can still have hiccups.
• ###### 12. Re: _rotation value problem
and to simplify things it could be

var speed:Number = 0.15;

var newRotation = clip._rotation360+ (speed*difference)

• ###### 13. Re: _rotation value problem
Thanks!! :)