ForumsProgramming ForumCharacter Face Mouse Actionscript 3.0

26 11513
LennonTheMage
offline
LennonTheMage
55 posts
Nomad

So now I have another issue :3
This code down here does make the character rotate, but he rotates like a weirdo. If you want to see how he rotates go here:[url=http://megaswf.com/serve/1131412]

(Wait for it to load :3)

Current Code:
character.rotation = Math.atan((mouseY - character.y)/(mouseX - character.x)) * 180/Math.PI + 90
}

  • 26 Replies
LennonTheMage
offline
LennonTheMage
55 posts
Nomad

[url=http://www.megaswf.com/serve/1131412]

This link better work!

P.S. Just put this here for all the lazy people who don't want to copy/paste the link into the address bar.

driejen
offline
driejen
486 posts
Nomad

Not too sure what is going on there, but as a temporary fix you can try this.

var dy:int = mouseY - character.y;
var dx:int = mouseX - character.x;
character.rotation = Math.atan(dy,dx)*57.296;
if(dx<0) character.rotation += 180;

driejen
offline
driejen
486 posts
Nomad

I made some errors on that because I just thought about what could work, but testing it on flash it works when I have this;

var dy:int = mouseY - character.y;
var dx:int = mouseX - character.x;
character.rotation = Math.atan(dy/dx)*57.296+90;
if(dx<0) character.rotation += 180;

master565
offline
master565
4,104 posts
Nomad

With just looking at your code, i can tell your trying to have him rotate towards your mouse. If he is flipping weirdly, what you have to do is go to your library, edit the symbol, and if you see in the center, there is a little circle (your person is usually to the bottom right of that). That circle is the point of rotation (the point its rotating from), so if its not directly on your symbol, its not going to rotate correctly. What you do is move him to the middle, but this isn't fully accurate. Go to properties, take the characters height and divide it by 2, then subtract that from the characters Y position. Then do the same with the width and the X position. This should put the character in the absolute middle.

LennonTheMage
offline
LennonTheMage
55 posts
Nomad

Is there any other way to change the dot? There isn't just one center point in my character. Every part of his body is separated because they all react differently to what happens in the game :/

master565
offline
master565
4,104 posts
Nomad

Is there any other way to change the dot?


Not that i know of.
LennonTheMage
offline
LennonTheMage
55 posts
Nomad

Hm... I don't know what I'm going to do then :/

& @Driejen Your code comes up with some weird output about things being null.

master565
offline
master565
4,104 posts
Nomad

I just want to say, that dot is the registration point. When you set an movieclips X to 100, that point is what is placed at 100, not the movieclip. If the movie clip is to the bottom right of that, it will be placed with an offset to 100.

LennonTheMage
offline
LennonTheMage
55 posts
Nomad

so I want the + to be center on the character? or the O?

LennonTheMage
offline
LennonTheMage
55 posts
Nomad

OH WAIT! I need to make the character itself to an offset of the registration point?

master565
offline
master565
4,104 posts
Nomad

I need to make the character itself to an offset of the registration point?


I'm not really sure what you mean.
driejen
offline
driejen
486 posts
Nomad

@Driejen Your code comes up with some weird output about things being null.

Mine has no problems at all. And you are going to have to be more specific if you want it fixed. Also changing the registration point isn't going to fix your rotation. Sure the center point seems a little off your characters head but it's not like it is orbiting an empty space when rotating.

The problem is that when your mouse is to the left of the character, its rotation is offset by an amount compared to when your mouse is to the right of the character. Hence why I added the line;
if(dx<0) character.rotation += 180;
LennonTheMage
offline
LennonTheMage
55 posts
Nomad

I must have inserted the code incorrectly. Now it does a full turn, but it does not face the mouse. For example if I have my cursor on the right side of the character it looks down.

driejen
offline
driejen
486 posts
Nomad

It depends on where your character is facing when the angle is 0, on my example it started facing up so I have a +90, you will have to adjust it. In your case you must have started with the character facing right at 0 degrees? remove the +90.

LennonTheMage
offline
LennonTheMage
55 posts
Nomad

Ah okay. It works now ^_^. This involves trig correct?

Showing 1-15 of 26