im making a space shooter game in as3 and im really stuck on making the lasers fire in the direction that the ship is facing. if anyone can help me id really appreciate it. this is the code i got so far
function fireLaser(event:Event):void{
var laser:Laser = new Laser();
laser.x = spaceShip.x;
laser.y = spaceShip.y;
laser.rotation = spaceShip.rotation;
addChild(laser);
laser.addEventListener(Event.ENTER_FRAME, moveLaser);
}
what you’ve managed to do here is move the laser upwards instead of in the direction of the space ship. try this out and see if it works for you:
[AS]
function moveLaser(event:Event):void{
var angle:Number = event.target.rotation*Math.PI/180;
event.target.x += Math.cos(angle)*5;
event.target.y += Math.sin(angle)*5;
if(event.target.y <= -10){
event.target.removeEventListener(Event.ENTER_FRAME , moveLaser);
removeChild(MovieClip(event.target));
}
}[/AS]