Multiple Rotation toward a point

Hello,

I am working on writing better code and using attach movie clip. As practice I am coding a very simple tower defense type game. What I have so far is you can click to set down a cannon and there is one badguy on the stage that you can drag around and the cannons should rotate and always point in the direction of the bad guy.

Here is my code - when I only have one on the stage it works fine - when I add another cannon it starts to act very buggy - they no longer rotate properly.

Here is the code


code.onLoad = function() {
	depth = 0;
	R = Q1=QQ1=Q2=QQ2=0;
};
code.onEnterFrame = function() {
	//trace(_xmouse);
};
code.onMouseDown = function() {
	cannon1 = _root.attachMovie("cannon", "cannon"+depth, depth);
	depth++;
	cannon1._x = _xmouse;
	cannon1._y = _ymouse;
	cannon1.onLoad = function() {
	};
	cannon1.onEnterFrame = function() {
		drag = 5;
		var distX = this._x-_root.badguy._x;
		var distY = this._y-_root.badguy._y;
		var radians = Math.atan2(distY, distX);
		targetRotation = ((radians/Math.PI)*180);
		if (targetRotation<0) {
			Q1 += (targetRotation-QQ1);
		}
		QQ1 = targetRotation;
		if (targetRotation>0) {
			Q2 += (targetRotation-QQ2);
		}
		QQ2 = targetRotation;
		if (Q3-180>targetRotation) {
			Q1 += 360;
		}
		Q3 = targetRotation;
		if (Q4+180<targetRotation) {
			Q2 -= 360;
		}
		Q4 = targetRotation;
		Q5 = Q1+Q2;
		rotateAmt = (Q5-this._rotation)/drag;
		R += ((Q5-R)-90)/drag;
		this._rotation = R;
	};
};


I have also attached the movie if anyone wants to take a look. As I said I don’t have lots of experience writing the code this way so I am open to helpful info or criticisms.

Thanks!