Firing lasers

hey guys,

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);
}

function moveLaser(event:Event):void{
event.target.y -=5;
if(event.target.y <= -10){
event.target.removeEventListener(Event.ENTER_FRAME, moveLaser);
removeChild(MovieClip(event.target));
}
}

thanks :slight_smile:

function moveLaser(event:Event):void{
event.target.y -=5;
if(event.target.y <= -10){
event.target.removeEventListener(Event.ENTER_FRAME , moveLaser);
removeChild(MovieClip(event.target));
}
}

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]

oh, and you’ll also need to check for the other boundaries the ship can get past to delete it when it goes off-screen.

if your ship is rotating, it may take a bit of math to get the direction…