While making this flying spaceship, I want the ship to rotate to my mouse, to do this, here is the basic code to do so….

var dx:Number = mouseX - x;
var dy:Number = mouseY - y;
var angle:Number = Math.atan2(dy, dx);
this.rotation = angle * 180 / Math.PI;

Application Source

Please note this is straight out of the Actionscript Cookbook by Lott, Schall, and Peters all of which are some of the best writers I’ve read. Peters also is the author of the great book Animating with Actionscript Making things move. of which i’m in the middle of reading.

I figured to get the result I wanted all I would have to do is grab the ships width and height, then divide by two. Once I tried that I got a little better results, but from what I read online, one way to accomplish what were looking for is to offset everything that is in your sprite…. if you have a movie clip that is inside the sprite, you just offset it, so it’s center actually ends up in negative coordinates.

I suppose this is a work around? There is a lot of actionscript 2 resources out there and not so many actionscript 3. I’m not sure how much I like this solution simply because now, I have to adjust my bounds code.

If I’m checking to see if my sprite is off the display list, or hitting the edges, I am probably testing on it’s x and y plus the width’s and height’s (works fine if it’s a square object, you would use radius if dealing with something round)… the reason this is kind of a weird issue, is because if I don’t change my bounds code to adjust for the new offset, I will end up having the right side of the bounds look like it’s not quite hitting the boundary, and the left side will be overlapping the boundary. So what you need to do now, is take a look at my last post Manager bounds in Actionscript 3.0 and use the code, and adjust it accordingly for the new offset.

Application Source

I haven’t adjusted for the bounds in this code yet, but its a constant work in progress.