ForumsProgramming ForumCool Little Trick I figured out.

38 7419
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

Ahhhh...I see.

UNDERLINEBOLDITALIC

Sorry, never done that before.

Well, my game may appear on AG (hopefully it is good enough) so be on the lookout! (I'll look for yours, too)

Klaushouse
offline
Klaushouse
2,770 posts
Nomad

Haha alright, what you up to? If ever you need any help or advice I am here for you! :P

Programpro
offline
Programpro
562 posts
Nomad

Good to know, I'll probably need help at some point.

Let's see...

You can't use educational version for commercial purposes, but can you still post on AG (assuming no profit)?

Klaushouse
offline
Klaushouse
2,770 posts
Nomad

As long as you make no money from it(AKA as in ArmorGames sponsoring you and sending you money). But if you were planning on making money might as well spend the 700$ cause the sponsorship could pay for the entire fee of the flash program, you'd only make a few bucks but it's profit!

Programpro
offline
Programpro
562 posts
Nomad

Yah, I probably should.

And you actually (if sponsored) would make a BIG profit.

See here:

Armor Sponsorship

Programpro
offline
Programpro
562 posts
Nomad

lol cool.

Friends, Klaus?

Klaushouse
offline
Klaushouse
2,770 posts
Nomad

I am pretty sure the price range of 1500 to 7000 is a bit off. If not then holy crap sponsorship and a new wii here I come. I will check it out.

Klaushouse
offline
Klaushouse
2,770 posts
Nomad

Yes I have friends Program...
...
...

...
...

...
...

...
...

...
...

...
...
Not but seriously sure friend it up.

Programpro
offline
Programpro
562 posts
Nomad

I can't tell if you were joking or not...

If not I meant do you want to be friends?

If so I'm pretty sure "friend it up" means yes...lol

Klaushouse
offline
Klaushouse
2,770 posts
Nomad

Yes friend it up is indeed an acceptance of friendship and an invitation to click my very royal Add Friend button.

Programpro
offline
Programpro
562 posts
Nomad

lol...

Also, I realized what you meant and I went ahead and changed my code. Now, instead of flipping the image when the mouse is to the left, it changes the rotation so it's facing the correct way. With it, I coded a little missile to follow my mouse (it was kewl).

But, yeah, is that the weaknesss in my code you were referring to?

Klaushouse
offline
Klaushouse
2,770 posts
Nomad

Sounds good. And by a missile following your mouse I presume all you did is have an MC with the missile MC inside and on the actions of the missile MC all you need is this._x += 5; looping the entire time and have the parent MC rotating and the missile follows.

Code sounds better program.

Programpro
offline
Programpro
562 posts
Nomad

No, actually this is waaaaay better. The missile is a standalone MC, and the code:

this._x += Math.cos(this._rotation*(Math.PI / 180)) * 10
this._y += Math.sin(this._rotation*(Math.PI / 180)) * 10

Coupled with the "Point at Mouse" code moves it perfectly! (The PI thing is to convert from degrees to radians).

So, yah.

Whichello
offline
Whichello
3 posts
Nomad

its better if u do this

player.onEnterFrame = function(){
this._rotation=Math.atan2(_ymouse-this._y,_xmouse-this_x)/(Math.PI/180)
}

thats off the top of my head but it should work

Whichello
offline
Whichello
3 posts
Nomad

player.onEnterFrame = function(){
this._rotation=Math.atan2(_ymouse-this._y,_xmouse-this._x)/(Math.PI/180)
this._x = _xmouse
this._y = _ymouse
}
this is better soz

Showing 16-30 of 38