# \[fmx2004\]Trigonomic Shooting

**URL:** <https://forum.kirupa.com/t/fmx2004-trigonomic-shooting/42312>\
**Category:** programming\
**Created:** [February 7, 2004, 5:42pm UTC](https://forum.kirupa.com/t/fmx2004-trigonomic-shooting/42312 "2004-02-07T17:42:35Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![InsaneMonk](https://avatars.discourse-cdn.com/v4/letter/i/ce7236/32.png) [@InsaneMonk](https://forum.kirupa.com/u/InsaneMonk)\
**Post date:** [February 7, 2004, 5:42pm UTC](https://forum.kirupa.com/t/fmx2004-trigonomic-shooting/42312/1 "2004-02-07T17:42:35Z")

</div>

Hi, I am currently making a game in which you are in an overhead view. You can rotate, and I need to be able to shoot in w/e direction you are facing. I have a code, and when I shoot, it does shoot in the direction you are facing, but If you change direction, the bullet also changes direction. I know why, and I need desperate help. Here is the code:

In the frame:  
[AS]onLoad = function () {  
i = 1;  
bullet.\_visible = false;  
};  
onEnterFrame = function () {  
x = Math.cos(sqr.\_rotation\*Math.PI/180)_5;  
y = Math.sin(sqr.\_rotation_Math.PI/180)\*5;  
if (Key.isDown(Key.UP)) {  
sqr.\_rotation += 5;  
}  
if (Key.isDown(Key.DOWN)) {  
sqr.\_rotation -= 5;  
}  
if (Key.isDown(Key.SPACE)) {  
bullet.duplicateMovieClip(“obj”+i, i);  
this[“obj”+i].\_visible = true;  
i++;  
}  
};[/AS]

In the bullet:

[AS]onClipEvent (load) {  
this.\_x = sqr.\_x;  
this.\_y = sqr.\_y;  
}  
onClipEvent (enterFrame) {  
this.\_x += \_root.x;  
this.\_y += \_root.y;  
}[/AS]

here is the .fla:

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 7, 2004, 6:08pm UTC](https://forum.kirupa.com/t/fmx2004-trigonomic-shooting/42312/2 "2004-02-07T18:08:06Z")

</div>

Now I’m not the best with math at all… but it almost looks like you’re using the same variables for the moving bullet, as you are for the moving gun… which means that as the gun itself moves, so will the bullet…  
I would make a seperate set of variables that will be set, and then stay static when the bullet is fired to determine the Slope the bullet will move towards it’s target.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 7, 2004, 6:10pm UTC](https://forum.kirupa.com/t/fmx2004-trigonomic-shooting/42312/3 "2004-02-07T18:10:36Z")

</div>

And how would I make them stay static?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 7, 2004, 7:20pm UTC](https://forum.kirupa.com/t/fmx2004-trigonomic-shooting/42312/4 "2004-02-07T19:20:08Z")

</div>

[AS]  
if (Key.isDown(Key.SPACE)) {  
bullet.duplicateMovieClip(“obj”+i, i);  
this[“obj”+i].\_visible = true;  
“variablea”+i = value;  
“variableb”+i = value;  
i++;  
}

[/AS]

something similar to that, I’m at work… I’m not at a computer with Flash (or anything) installed right now to give you a better explanation than that… hopefully someone else can elaborate a little, and lend a hand to you… sorry.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 7, 2004, 8:59pm UTC](https://forum.kirupa.com/t/fmx2004-trigonomic-shooting/42312/5 "2004-02-07T20:59:31Z")

</div>

Ah! I see, but how can you call a variable with “i” attached to it? Can you really attach variables to variables?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 7, 2004, 9:06pm UTC](https://forum.kirupa.com/t/fmx2004-trigonomic-shooting/42312/6 "2004-02-07T21:06:08Z")

</div>

I don’t think the code would look exactly like my example, but i think it work similar to the way it would when you make duplicate movie clips…

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 7, 2004, 11:41pm UTC](https://forum.kirupa.com/t/fmx2004-trigonomic-shooting/42312/7 "2004-02-07T23:41:39Z")

</div>

\_root[“variablea”+1i] =

is the correct way to use dynamic variables in Flash buddy 😉

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 8, 2004, 3:58am UTC](https://forum.kirupa.com/t/fmx2004-trigonomic-shooting/42312/8 "2004-02-08T03:58:07Z")

</div>

Yeah, I figured it was something like that… man… I’ve been away from the coding for too long now… that’s what you get for having to type up a design doc before advancing to the actual coding… 😉

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 8, 2004, 5:32pm UTC](https://forum.kirupa.com/t/fmx2004-trigonomic-shooting/42312/9 "2004-02-08T17:32:58Z")

</div>

soooo… how do u create a “dynamic variable”?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 8, 2004, 5:45pm UTC](https://forum.kirupa.com/t/fmx2004-trigonomic-shooting/42312/10 "2004-02-08T17:45:00Z")

</div>

By “Dynamic” I mean that the variable changes. In your case, the variables that point the gun turret in the right direction are “Dynamic”, because they change everytime the player presses the button.  
By “Static”, I mean that once the variable is set, they don’t change any more.  
Which would result in the following: When the player hits [Space] the bullet gets told to move in the direction that the gun is pointing, but everytime the gun moves, the bullet is sent different information, and then it moves in the circular motion you mentioned. If you make it Static, then the bullet is told which way to go when [Space] is hit, but that information never changes, and the bullet will keep heading in the same direction, since they will be seperate variables.

Make sense?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 10, 2004, 10:25pm UTC](https://forum.kirupa.com/t/fmx2004-trigonomic-shooting/42312/11 "2004-02-10T22:25:45Z")

</div>

yes it does. ive actually tried that and the bullet doesnt move at all… i think its a duplication problem. u helped alot though
