# Firing lasers

**URL:** https://forum.kirupa.com/t/firing-lasers/261013
**Category:** Uncategorized
**Created:** [May 22, 2008, 2:30am UTC](https://forum.kirupa.com/t/firing-lasers/261013 "2008-05-22T02:30:18Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![chuck\_spidena](https://avatars.discourse-cdn.com/v4/letter/c/b487fb/32.png) [@chuck\_spidena](https://forum.kirupa.com/u/chuck_spidena)
#### Post date: [May 22, 2008, 2:30am UTC](https://forum.kirupa.com/t/firing-lasers/261013/1 "2008-05-22T02:30:18Z")

</div>

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 🙂

---

<div class="post-metadata">

### Author: ![Exodar](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@Exodar](https://forum.kirupa.com/u/Exodar)
#### Post date: [May 22, 2008, 11:46pm UTC](https://forum.kirupa.com/t/firing-lasers/261013/2 "2008-05-22T23:46:56Z")

</div>

> 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]

---

<div class="post-metadata">

### Author: ![Exodar](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@Exodar](https://forum.kirupa.com/u/Exodar)
#### Post date: [May 22, 2008, 11:49pm UTC](https://forum.kirupa.com/t/firing-lasers/261013/3 "2008-05-22T23:49:26Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![johnlouis](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/johnlouis/32/568_2.png) [@johnlouis](https://forum.kirupa.com/u/johnlouis)
#### Post date: [May 23, 2008, 1:55am UTC](https://forum.kirupa.com/t/firing-lasers/261013/4 "2008-05-23T01:55:59Z")

</div>

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