ForumsProgramming ForumCool Little Trick I figured out.

38 7415
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
Arax_Nisanu
offline
Arax_Nisanu
1,305 posts
Nomad

wow this is awesome! As a flash n00b, this isn't much help to me but it will help others! Awesome little trick man!

Programpro
offline
Programpro
562 posts
Nomad

Thx. Also, that "_root.awesome.txt" line is for something I was using for testing something. I had a dynamic text box to show me the _rotation of the object.

Also, I'm a newbie flah person also but I still think this can be useful. I think I'm gonna use it in my first game ever, which I'll probably post here.

So, yeah, thanks again for the compliment!

Klaushouse
offline
Klaushouse
2,770 posts
Nomad

This is alright but not so efficient, better ways to handle things so that you can rotate multiple objects and at the same time with more variables make it so they know where the tip of the gun is to shoot. I have one more efficient then this for my game right now... xD

Programpro
offline
Programpro
562 posts
Nomad

Gee...thx a lot, Klaus (Is that pronounced "Clozz" or "Clouse"?)

Also, I liked ur old picture better.

Anyway, my system does work with multiple objects. Also, you could modify it to work from one object to another, and you could make it adjust an angular velocity variable instead of the angle.

I'm just saying what makes yours so much better than mine?

Klaushouse
offline
Klaushouse
2,770 posts
Nomad

I am just saying that you're neat little trick is just a lower grade version of a proper trigonometry based rotation system for MCs. Sorry to bust your bubble but this is simple if not basic stuff in AS. It's pronounces Clouse, like Mouse, or Claus like Santa Claus I have no preference.

Programpro
offline
Programpro
562 posts
Nomad

Well, if you're gonna put me down at least put it on my message board and not in my open forum. Still, good feedback (I whipped that equation up in 15 minutes).

And I think [Clouse]house sounds cooler than [Clause]house.

Programpro
offline
Programpro
562 posts
Nomad

Thx...

Mousehouse?

<lol>

Klaushouse
offline
Klaushouse
2,770 posts
Nomad

I thinkk Clousehouse sounds better too. Someone actually spelled it MouseHouse once. And I didn't mean to put you down it's just common for me so I guess I overeacted! My apologies. Still good that you figured this out by yourself and not from an online tut, better than alot of people can do!

Klaushouse
offline
Klaushouse
2,770 posts
Nomad

Why you little...

I will get you. Mark my words!

Programpro
offline
Programpro
562 posts
Nomad

I sure hope ur joking.

But we are waaaaaaaaaaaaaaaaaaaaaaaaaay off-topic (though I gues this isn't really a topic). Well, if mine can't do the things yours can, then how could I improve it?

Klaushouse
offline
Klaushouse
2,770 posts
Nomad

Well I you need to do to improve it enough for what most people need(unlike me, I need some complicated things) is a variable that you put your final degree that it will be going to on. Like MyDegrees or something. That was you can move multiple items at the same time and if it shoots you can shoot something and already know which angle it must be attached at?

Programpro
offline
Programpro
562 posts
Nomad

I'm sorry you're going to have to repeat that.

Why do I need MyDegrees? This is setting the _rotation, so if I need to use it's degrees, I'll use _root.theitem._rotation.

Are you saying that this should generate a target point, to which the object will gradually rotate?

If those are not what you meant, then, again, please repeat that.

Klaushouse
offline
Klaushouse
2,770 posts
Nomad

Well see what should be happenning is you need to find the amount of degrees between you're MC and the mouse. this amount of degrees should be kept in a variable which I call MyDregrees. So then all you need to do is something._rotation += Mydegrees. So then you can start rotating many items off of just one equation and also you just use my degrees to know where the gun is pointing so that you can attachMovie something at the tip of it.

Programpro
offline
Programpro
562 posts
Nomad

I think i see what you are getting at. For my game, I need it to face the mouse so when I click it shoots something (so original). I think that my thing will work in that situation. May I ask what advanced thing you need your MyDegrees for, or would it be a spoiler of your game.

Klaushouse
offline
Klaushouse
2,770 posts
Nomad

Well for some of my work I need it not only because something is come from the spot but to it. So I will need arc angles and parabolas and huge equations to get an arc going on towards the spot that is moving to my mouse and such. But it's all just for my own gain not a game itself. It's useful to have these things lying around for a game whenever you need it!

Showing 1-15 of 38