I am trying to make a game in which a cannons barrel is lifted or lowerd according to the cursor's position.
Here is the code I inserted inside the Barrel mc:
import flash.events.Event;
var hypotenuse,ratio,angleRad,angleDeg:Number;
stage.addEventListener(Event.ENTER_FRAME,spin);
function spin (e:Event):void{
trace(mouseX+ " " + mouseY);
hypotenuse = Math.sqrt((mouseX)*(mouseX)+(mouseY)*(mouseY));
ratio = (Math.abs(mouseX))/hypotenuse;
angleRad = Math.asin(ratio);
angleDeg = angleRad*180/Math.PI;
if(mouseX >= 0 && mouseY <= 0 ){
this.rotation = angleDeg;
}
if(mouseX >= 0 && mouseY > 0){
this.rotation = (90-angleDeg)*2;
}
if(mouseX < 0 && mouseY > 0){
this.rotation = angleDeg + 180;
}
if( mouseX < 0 && mouseY <= 0){
this.rotation = (90-angleDeg)*2;
}
}
For some reason the barrel is just jerking around and not even in the direction where the mouse is at.. =/ I would love some advice or maybe a different coding suggestions this is a problem I'v been trying to solve for a while now and i dont know what i am doing wrong.