ForumsProgramming ForumCool Little Trick I figured out.

38 7422
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
Programpro
offline
Programpro
562 posts
Nomad

Cool whichello, I will try that.

Klaushouse
offline
Klaushouse
2,770 posts
Nomad

Very nice Which. But all I do is have a missile mc within an empty mc that goes this._x += 5; cause as you rotate it, it will always go forward, because it is going x inside the mc with is whichever direction the missile points(which is the mouse).

Programpro
offline
Programpro
562 posts
Nomad

Klaus, his equation is for pointing, not following, like urs. And I think urs could use some work, Klaus, no offense.

Klaushouse
offline
Klaushouse
2,770 posts
Nomad

What do you mean mine? I haven't written anything!

Danstanta
offline
Danstanta
1,702 posts
Blacksmith

What do you mean Programpro?

crazynaitor
offline
crazynaitor
2,611 posts
Jester

Yea what do you mean?

darkd00m
offline
darkd00m
505 posts
Nomad

this was help? more like a math lesson -_-

Programpro
offline
Programpro
562 posts
Nomad

geez...tagteamed against me...I mean the thing where there is a movie clip in another and the one inside changes x while the outer one rotates.

I'm sorry, Klaus, maybe I misunderstood it, but that's how it sounded to me. Just a little overcomplicated (no need to triple-team me : (

Also, darkd00m, I was wondering when someone would comment on confusing trigonometry. In this case, my equation makes a right triangle where the hypotenuse (longest side) is a line from the object to the mouse. Then, by using inverse (arc) tangent and dividing one side by the other, I find the angle nearest the object and that tells me how to rotate it.

Sorry if that's still confusing!

Showing 31-38 of 38