# Need help with attaching movieclips for a gun

**URL:** https://forum.kirupa.com/t/need-help-with-attaching-movieclips-for-a-gun/264673
**Category:** programming
**Created:** [June 28, 2008, 12:45pm UTC](https://forum.kirupa.com/t/need-help-with-attaching-movieclips-for-a-gun/264673 "2008-06-28T12:45:31Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![sam\_vickery](https://avatars.discourse-cdn.com/v4/letter/s/a88e4f/32.png) [@sam\_vickery](https://forum.kirupa.com/u/sam_vickery)
#### Post date: [June 28, 2008, 12:45pm UTC](https://forum.kirupa.com/t/need-help-with-attaching-movieclips-for-a-gun/264673/1 "2008-06-28T12:45:31Z")

</div>

I really need some help in making a bullet that would fire out of the gun towards the angle the gun is at. I have not found a tutorial that shows how you can do this. Any help will be much appreacheated. I have attached the fla file so if you want to alter any of the code feel free to. After you have changed it reply to this and attach the file. thanks.

---

<div class="post-metadata">

### Author: ![dlgamer](https://avatars.discourse-cdn.com/v4/letter/d/7993a0/32.png) [@dlgamer](https://forum.kirupa.com/u/dlgamer)
#### Post date: [June 29, 2008, 1:25am UTC](https://forum.kirupa.com/t/need-help-with-attaching-movieclips-for-a-gun/264673/2 "2008-06-29T01:25:20Z")

</div>

You can use the law of cosines to find the x and y movement per frame from the total movement per frame (a/sinA = b/sinB = c/sinC) [Law Of sines.](http://en.wikipedia.org/wiki/Law_of_sines)  
If this math is al little advanced for you It’s okay I will walk you thru it. (look at the bitmap for a visual explanation)

using this math:  
bulletSpeed/sin(90) = ySpeed/sin(gunAngle) = xSpeed/sin(90-gunAngle)

this simplifies to:  
ySpeed = bulletSpeed_sin(gunAngle) and  
xSpeed = bulletSpeed_sin(90-gunAngle)

to calculate find the sine in flash use **Math.sin()**

One problem You will have is that when you get a decimal for xSpeed or ySpeed the \>\_x and \_y coordinated will be rounded off. To get a more accurate result do something like:

```auto
bullet_mc.onEnterFrame = function() {
    xPos += xSpeed;
    yPos += ySpeed;
    this._x = xPos;
    this._y = yPos;
}

```

oh, and by the way, you didn’t include the flash file 😑

---

<div class="post-metadata">

### Author: ![sam\_vickery](https://avatars.discourse-cdn.com/v4/letter/s/a88e4f/32.png) [@sam\_vickery](https://forum.kirupa.com/u/sam_vickery)
#### Post date: [June 29, 2008, 3:27pm UTC](https://forum.kirupa.com/t/need-help-with-attaching-movieclips-for-a-gun/264673/3 "2008-06-29T15:27:05Z")

</div>

thanks for that. I sort of understand it but can you please fill in the code on the flash file(now attached)🙂
