I'm making a game where you operate a cannon turret and shoot at yet-undetermined enemies. Here's all the code I have:
CODE
this.stop();
//variables
var BBs:Array=new Array();
var uniVar:Object=new Object();
//event listeners
stage.addEventListener(MouseEvent.CLICK, addBullet);
stage.addEventListener(MouseEvent.MOUSE_MOVE, function(e:Event) {
canon_1.rotation=(180/Math.PI)*Math.atan((275-mouseX)/mouseY);
});
//Game Loop is called every 10 miliseconds
var interval=setInterval(function() {
if (BBs.length>0) {
monitorBullets();
}
}, 10);
//adds a bullet
function addBullet(e:Event):void {
var cBul:Bullet=new Bullet();
var dir:Number=-(canon_1.rotation/90);
cBul.x=275+100*Math.cos((180/Math.PI)*(-canon_1.rotation+270));
cBul.y=100*Math.sin((180/Math.PI)*(-canon_1.rotation+270));
addChild(cBul);
BBs.push([cBul, dir]);
}
//handles the bullets
function monitorBullets():void {
uniVar.todo=false;
BBs.forEach(function(item:*, index:int, arr:Array) {
BBs[index][0].x+=3*BBs[index][1];
BBs[index][0].y+=3*(1-Math.abs(BBs[index][1]));
if (BBs[index][0].x>570 || BBs[index][0].x<-20 || BBs[index][0].y>420) {
uniVar.todo=true;
uniVar.numb=index;
}
});
if (uniVar.todo) {
removeChild(BBs[uniVar.numb][0]);
BBs.splice(uniVar.numb, 1);
}
}
Everything works (there's no actionscript errors), but it doesn't behave the way I want it to. The cannon rotates fine (by the way, it's located top-center of the stage) and always points at the mouse, and the bullets have the right direction and speed, but they're getting placed weird. It's at lines 21 and 22 (located in the function "addBullet") that places the bullets. I'm trying to have them start at the tip of the cannon (like normal bullets would) and shoot off screen, and seeing as I have the polar coordinates of the tip of the cannon, I tried to convert it to cartesian coordinates, but it's not working. sometimes the bullets start out of the screen and shoot down. Sometimes they start on the other side of the cannon. But they always have the right direction

Does anybody know how I could get the cartesian coordinates of a polar point where r=100 and theta is variable?