# Setting vector for bullet instances

**URL:** <https://forum.kirupa.com/t/setting-vector-for-bullet-instances/216775>\
**Category:** Uncategorized\
**Created:** [February 20, 2007, 1:56pm UTC](https://forum.kirupa.com/t/setting-vector-for-bullet-instances/216775 "2007-02-20T13:56:12Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![graylensman](https://avatars.discourse-cdn.com/v4/letter/g/5f9b8f/32.png) [@graylensman](https://forum.kirupa.com/u/graylensman)\
**Post date:** [February 20, 2007, 1:56pm UTC](https://forum.kirupa.com/t/setting-vector-for-bullet-instances/216775/1 "2007-02-20T13:56:12Z")

</div>

I’m working on a zombie shooter, with a top-down view. Think asteroids, except with zombies instead of rocks. So, I’m coding weapons fire. I can generate bullets all day and all of the night, and can get them going in the direction the hero’s gun is pointing - except that once you change the rotation of the hero, the bullet vectors change too. Any ideas on how to keep the bullet on an unchanging vector? I’ve tried all sorts of stuff, but nothing works.

Here’s the code so far:  
ActionScript Code:  
[FONT=Courier New][LEFT][COLOR=#808080]_[//2.20.07](https://2.20.07) [/COLOR]  
[COLOR=#808080]//motion of the hero. This works fine._[/COLOR]  
[COLOR=#000000] **function** [/COLOR] moveHero[COLOR=#000000]([/COLOR]speed, swivel[COLOR=#000000])[/COLOR] [COLOR=#000000]{[/COLOR]  
[COLOR=#808080]_//left and right arrow keys rotate our plucky hero; up key moves him forward relative to his rotation_[/COLOR]  
[COLOR=#0000FF]if[/COLOR] COLOR=#000000[/COLOR] [COLOR=#000000]{[/COLOR]  
hero.[COLOR=#0000FF]\_rotation[/COLOR] -= swivel;  
[COLOR=#000000]}[/COLOR] [COLOR=#0000FF]else[/COLOR] [COLOR=#0000FF]if[/COLOR] COLOR=#000000[/COLOR] [COLOR=#000000]{[/COLOR]  
hero.[COLOR=#0000FF]\_rotation[/COLOR] += swivel;  
[COLOR=#000000]}[/COLOR] [COLOR=#0000FF]else[/COLOR] [COLOR=#0000FF]if[/COLOR] COLOR=#000000[/COLOR] [COLOR=#000000]{[/COLOR]  
heroAngle = [COLOR=#000080]90[/COLOR]-hero.[COLOR=#0000FF]\_rotation[/COLOR];  
xchange = [COLOR=#0000FF]Math[/COLOR].[COLOR=#0000FF]cos[/COLOR]COLOR=#000000[/COLOR];  
ychange = -[COLOR=#0000FF]Math[/COLOR].[COLOR=#0000FF]sin[/COLOR]COLOR=#000000[/COLOR];  
hero.[COLOR=#0000FF]\_x[/COLOR] += xchange_speed;  
hero.[COLOR=#0000FF]\_y[/COLOR] += ychange_speed;  
[COLOR=#000000]}[/COLOR]  
[COLOR=#808080]_//call the function that makes him fire his weapon_[/COLOR]  
[COLOR=#0000FF]if[/COLOR] COLOR=#000000[/COLOR] [COLOR=#000000]{[/COLOR]  
shootItCOLOR=#000000[/COLOR];  
[COLOR=#000000]}[/COLOR]  
[COLOR=#000000]}[/COLOR]  
[COLOR=#808080]_///_[/COLOR]  
[COLOR=#808080]_/the function to shoot the bullets. Attach movie clips of bullets, then impart vector and velocity  
using the sine and consine functions, too. But currently the vector changes when the rotation of  
the hero changes. /[/COLOR]  
[COLOR=#808080]//_[/COLOR]  
[COLOR=#808080]_//variables for bullet generation, and the speed of the bullets._[/COLOR]  
[COLOR=#000000] **var** [/COLOR] b = [COLOR=#000080]1[/COLOR];  
[COLOR=#000000] **var** [/COLOR] bSpeed=[COLOR=#000080]3[/COLOR];  
[COLOR=#000000] **function** [/COLOR] shootItCOLOR=#000000[/COLOR] [COLOR=#000000]{[/COLOR]  
[COLOR=#808080]_//incrementing our bullet variable by one_[/COLOR]  
b++;  
[COLOR=#808080]_//creating a variable to reference generated bullet instances_[/COLOR]  
[COLOR=#000000] **var** [/COLOR] [COLOR=#0000FF]bullet[/COLOR] = [COLOR=#FF0000]“bullet”[/COLOR]+b;  
[COLOR=#808080]_//putting bullets into action!_[/COLOR]  
[COLOR=#0000FF]\_root[/COLOR].[COLOR=#0000FF]attachMovie[/COLOR][COLOR=#000000]([/COLOR][COLOR=#FF0000]“bullet”[/COLOR], [COLOR=#0000FF]bullet[/COLOR], [COLOR=#0000FF]getNextHighestDepth[/COLOR]COLOR=#000000[/COLOR][COLOR=#000000])[/COLOR];  
[COLOR=#808080]_//setting bullet instance x & y to hero x & y_[/COLOR]  
[COLOR=#0000FF]\_root[/COLOR][COLOR=#000000][[/COLOR][COLOR=#0000FF]bullet[/COLOR][COLOR=#000000]][/COLOR].[COLOR=#0000FF]\_x[/COLOR] = hero.[COLOR=#0000FF]\_x[/COLOR];  
[COLOR=#0000FF]\_root[/COLOR][COLOR=#000000][[/COLOR][COLOR=#0000FF]bullet[/COLOR][COLOR=#000000]][/COLOR].[COLOR=#0000FF]\_y[/COLOR] = hero.[COLOR=#0000FF]\_y[/COLOR];  
[COLOR=#808080]_//onEnterFrame to put the bullets in motion -_[/COLOR]  
[COLOR=#808080]_//here’s where the weirdness starts. When the hero rotates, the vector of the bullets changes to match._[/COLOR]  
[COLOR=#0000FF]\_root[/COLOR][COLOR=#000000][[/COLOR][COLOR=#0000FF]bullet[/COLOR][COLOR=#000000]][/COLOR].[COLOR=#0000FF]onEnterFrame[/COLOR] = [COLOR=#000000] **function** [/COLOR]COLOR=#000000[/COLOR] [COLOR=#000000]{[/COLOR]  
bulletAngle = [COLOR=#000080]90[/COLOR]-hero.[COLOR=#0000FF]\_rotation[/COLOR];  
bXchange =[COLOR=#0000FF]Math[/COLOR].[COLOR=#0000FF]cos[/COLOR]COLOR=#000000[/COLOR]_[COLOR=#000080]5[/COLOR];  
bYchange = -[COLOR=#0000FF]Math[/COLOR].[COLOR=#0000FF]sin[/COLOR]COLOR=#000000[/COLOR]_[COLOR=#000080]5[/COLOR];  
[COLOR=#0000FF]this[/COLOR].[COLOR=#0000FF]\_x[/COLOR]+=bXchange_bSpeed;  
[COLOR=#0000FF]this[/COLOR].[COLOR=#0000FF]\_y[/COLOR]+=bYchange_bSpeed;  
[COLOR=#808080]_//remove movie clip instances once they leave the stage_[/COLOR]  
[COLOR=#0000FF]if[/COLOR] [COLOR=#000000]([/COLOR][COLOR=#0000FF]this[/COLOR].\_x\>Stage.[COLOR=#0000FF]width[/COLOR] || [COLOR=#0000FF]this[/COLOR].\_x\<[COLOR=#000080]0[/COLOR] || [COLOR=#0000FF]this[/COLOR].\_y\>Stage.[COLOR=#0000FF]height[/COLOR] || [COLOR=#0000FF]this[/COLOR].\_y\<[COLOR=#000080]0[/COLOR][COLOR=#000000])[/COLOR] [COLOR=#000000]{[/COLOR]  
[COLOR=#0000FF]this[/COLOR].[COLOR=#0000FF]removeMovieClip[/COLOR]COLOR=#000000[/COLOR];  
[COLOR=#000000]}[/COLOR]  
[COLOR=#000000]}[/COLOR];  
[COLOR=#000000]}[/COLOR]  
[COLOR=#808080]_//actually get it all going!_[/COLOR]  
[COLOR=#0000FF]this[/COLOR].[COLOR=#0000FF]onEnterFrame[/COLOR] = [COLOR=#000000] **function** [/COLOR]COLOR=#000000[/COLOR] [COLOR=#000000]{[/COLOR]  
moveHero[COLOR=#000000]([/COLOR][COLOR=#000080]5[/COLOR], [COLOR=#000080]10[/COLOR][COLOR=#000000])[/COLOR];  
[COLOR=#000000]}[/COLOR];  
[/LEFT]  
[/FONT]

thanks in advance!
