Community

Cool Little Trick I figured out.

Posted Jul 28, '08 at 2:02pm

Programpro

Programpro

303 posts

Wood - Knight

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!

Posted Jul 28, '08 at 3:13pm

Arax_Nisanu

Arax_Nisanu

1,344 posts

Iron - Lord

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

 

Posted Jul 28, '08 at 3:22pm

Programpro

Programpro

303 posts

Wood - Knight

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!

 

Posted Jul 28, '08 at 6:10pm

Klaushouse

Klaushouse

1,612 posts

Iron - Lord

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

 

Posted Jul 28, '08 at 6:36pm

Programpro

Programpro

303 posts

Wood - Knight

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?

 

Posted Jul 28, '08 at 6:54pm

Klaushouse

Klaushouse

1,612 posts

Iron - Lord

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.

 

Posted Jul 28, '08 at 7:00pm

Programpro

Programpro

303 posts

Wood - Knight

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.

 

Posted Jul 28, '08 at 9:10pm

Klaushouse

Klaushouse

1,612 posts

Iron - Lord

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!

 

Posted Jul 28, '08 at 9:09pm

Programpro

Programpro

303 posts

Wood - Knight

Thx...

Mousehouse?

<lol>

 

Posted Jul 28, '08 at 9:23pm

Klaushouse

Klaushouse

1,612 posts

Iron - Lord

Why you little...

I will get you. Mark my words!

 

Posted Jul 28, '08 at 9:38pm

Programpro

Programpro

303 posts

Wood - Knight

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?

 
Reply to Cool Little Trick I figured out.

You must be logged in to post a reply!