ForumsProgramming ForumCool Little Trick I figured out.

38 7413
Programpro
offline
Programpro
562 posts
Nomad

I was curious as to how...in "The Last Stand 1" / "2", as well as with some other games...they were able to make the movie clip point at the mouse.

Well, I did a little trigonometry in flash and came up with this:


onClipEvent (enterFrame) {

if (_root._xmouse > this._x) {

this._xscale = 100;

} else {

this._xscale = -100;

}

this._rotation = Math.atan((_root._ymouse - this._y) / (_root._xmouse - this._x)) * (180 / Math.PI);

_root.awesome.text = this._rotation;

}


This works if you make something like a gun with a big laser scope attached to it and make sure that the central rotation point (the little plus) is in the handle of the gun.

The way it works is it calculates the ratio of the difference in X and Y values, then uses them to make a right triangle. By dividing one by the other and taking the arc-tangent, it produces the angle that the thing needs to be rotated by in radians. Multiply that by [180/Math.PI] and you have it in degrees.

I also had a problem where this would only work if the mouse was to the right of the symbol. The first two "if" statements fix that.

I hope this helps someone! You can use it, but just make sure you tell me so that I know it was useful!

  • 38 Replies
Showing 46-45 of 38