Sign in to follow this  
Jurrien

Rotation issues

Recommended Posts

I'm quite new to flash, and i was trying to make an object move to where i click. not just moving there in a straight line, but by turning its direction. i have this working quite well, but it still has 2 flaws. one small flaw, that if you make a certain (large) angle between the object's direction and the direction it needs to move to, it will turn the other way around (instead of the other, shorter way). the second flaw is that, when you click nearly horizontal left of the object, it starts moving in the shape of an "8" in the opposite direction. this is the code i use, i hope something could help me with this. Thanks for reading, Jurrien.
onClipEvent (load) {
	xto = _x+1;
	yto = _y+1;
	dir = 1;
	dirto = 0;
	rotspeed = 10;
	speed = 10;
	dis = Math.sqrt(((xto-_x)*(xto-_x))+((yto-_y)*(yto-_y)))
}
onClipEvent (mouseDown) {
	xto = _root._xmouse;
	yto = _root._ymouse;
}
onClipEvent (enterFrame) {
	dis = Math.sqrt(((xto-_x)*(xto-_x))+((yto-_y)*(yto-_y)));
	
	if (dis>10) {
		
		dirto = Math.atan2(yto-_y, xto-_x)/(Math.PI/180);
		
		if (dir>180 and dirto<dir-180) {
			dirto -= 360;
		}
		if (dir<180 and dirto>dir+180) {
			dirto += 360;
		}
		
		if (dir>dirto) {
			dir -= Math.min(dir - dirto, rotspeed);
		}
		if (dir<dirto) {
			dir += Math.min(dirto - dir, rotspeed);
		}
		
		_rotation = dir;
		
		_x += Math.cos(dir*Math.PI/180)*speed;
		_y += Math.sin(dir*Math.PI/180)*speed;
		
		
	}
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this