View Full Version : Auto Rotate
cr125rider
May 1st, 2004, 10:58 PM
I am fairly new to flash and was wondering if someone could help me make a code so that a MC rotates in the direction it just moved. Say I make it go left, it would then rotate from its facing up to pointing left. I am trying to make it do this automaticly. I was thinking of using XPrevious and YPrevious variables to determine the direction it moved. I dont know if AS has a build in function for determining the direction to a point (If it doesn't, could you help me make one). and then adding 180 to the MC's current direction: Direction. If you could pass me off to a tut that would be much appreciated also.
Thanks for your help,
SeiferTim
May 2nd, 2004, 09:02 AM
You can use the:
object_mc._rotate = 90;
to have the MC rotate in a certain direction.... this is what I've done for my game (http://seifertim.no-ip.com/files/walking_block.html)...
Now, when you say "automatically", do you mean that you'll have an MC that is moving around on its own? And will need to determine the direction it's heading?
Try something like this:
//put in the place that makes the MC move:
oldX = this._x;
oldY = this._y;
this._x += xMove;
this._y += yMove;
if (oldX < this._x) {
if (oldY > this._y) {
this._rotate = 45;
} else if (oldY < this._y) {
this._rotate = 135;
} else {
this._rotate = 90;
} else if (oldX > this._x) {
if (oldY > this._y) {
this._rotate = 315;
} else if (oldY < this._y) {
this._rotate = 225;
} else {
this._rotate = 270;
} else {
this._rotate = 180;
}
I'll be using this code for my enemies, me thinks...
Clown Staples
May 2nd, 2004, 10:16 AM
it.move = function(x,y){
this._rotation = Math.atan2(y,x)/Math.PI*180;
this._x += x;
this._y += y;
}
onMouseDown=function(){
it.move(_xmouse-it._x,_ymouse-it._y);
}
SeiferTim
May 2nd, 2004, 10:53 AM
Clownstaples: Would that make the MC follow the mouse, when it's down?
cr125rider
May 2nd, 2004, 12:00 PM
Is that in MX 2004? Could you tell me what version that is for? I tried it and it came back with a couple of errors. I like ClownStaples because his is more precice. (SeiferTim, yours woork good in your game though.) I am trying to have a random movement so it needs to be pretty precice in order not to look cheap. Thanks guys once we get the errors resolved it will be sweet!
Couldn't you just add 180 instead of /pi * 180? Please explain!
this._rotation = Math.atan2(y,x)/Math.PI*180;
Thanks,
cr125rider
May 3rd, 2004, 08:42 PM
Ohh another thing, is that in radians? Caus if that is the conversion then it would explain evvvvvvery thing.
Thanks,
tofu
May 7th, 2004, 02:00 AM
it would be in teh ol' radians
pom
May 7th, 2004, 01:29 PM
Not MX04, and yes, in radians :) Can you post the errors you get?
cr125rider
May 7th, 2004, 04:12 PM
Alright,
I asked my math teacher and she helpd me out on the conversion,
this.Xdiff = this._x-_root._xmouse;
this.Ydiff = this._y-_root._ymouse;
this._rotation = (Math.atan2(this.Ydiff, this.Xdiff)*(180/Math.PI)-90);
I had to add the minus 90 because I drew the pic pointing up instead of right. It is working great now, thanks for all your help,
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.