# Spawning Enemies

**URL:** https://forum.kirupa.com/t/spawning-enemies/242590
**Category:** programming
**Created:** [October 28, 2007, 3:11am UTC](https://forum.kirupa.com/t/spawning-enemies/242590 "2007-10-28T03:11:28Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![habes](https://avatars.discourse-cdn.com/v4/letter/h/e5b9ba/32.png) [@habes](https://forum.kirupa.com/u/habes)
#### Post date: [October 28, 2007, 3:11am UTC](https://forum.kirupa.com/t/spawning-enemies/242590/1 "2007-10-28T03:11:28Z")

</div>

Ok, so heres my enemy code, on the main frame:  
ActionScript Code:  
[FONT=Courier New][LEFT][COLOR=#000000] **var** [/COLOR] enemy = [COLOR=#0000ff]attachMovie[/COLOR][COLOR=#000000]([/COLOR][COLOR=#ff0000]“zombie”[/COLOR], [COLOR=#ff0000]“zombie”[/COLOR]+zombienum, [COLOR=#000080]1000[/COLOR][COLOR=#000000])[/COLOR];  
enemy.[COLOR=#0000ff]\_x[/COLOR] = [COLOR=#0000ff]random[/COLOR]COLOR=#000000[/COLOR];  
enemy.[COLOR=#0000ff]\_y[/COLOR] = [COLOR=#0000ff]random[/COLOR]COLOR=#000000[/COLOR]+[COLOR=#0000ff]Stage[/COLOR].[COLOR=#0000ff]height[/COLOR];  
enemy.[COLOR=#000080]health[/COLOR] = [COLOR=#000080]100[/COLOR];  
enemy.[COLOR=#000080]dead[/COLOR] = [COLOR=#000000] **false** [/COLOR];  
enemy.[COLOR=#0000ff]onEnterFrame[/COLOR] = [COLOR=#000000] **function** [/COLOR]COLOR=#000000[/COLOR] [COLOR=#000000]{[/COLOR]  
[COLOR=#0000ff]if[/COLOR] COLOR=#000000[/COLOR] [COLOR=#000000]{[/COLOR]  
[COLOR=#0000ff]if[/COLOR] COLOR=#000000[/COLOR] [COLOR=#000000]{[/COLOR]  
enemy.[COLOR=#000080]body[/COLOR].[COLOR=#000080]fist[/COLOR].[COLOR=#0000ff]play[/COLOR]COLOR=#000000[/COLOR];  
myRadians = [COLOR=#0000ff]Math[/COLOR].[COLOR=#0000ff]atan2[/COLOR][COLOR=#000000]([/COLOR]player.\_y-enemy.[COLOR=#0000ff]\_y[/COLOR], player.\_x-enemy.[COLOR=#0000ff]\_x[/COLOR][COLOR=#000000])[/COLOR];  
myDegrees = [COLOR=#0000ff]Math[/COLOR].[COLOR=#0000ff]round[/COLOR]COLOR=#000000[/COLOR];  
[COLOR=#000000]}[/COLOR] [COLOR=#0000ff]else[/COLOR] [COLOR=#0000ff]if[/COLOR] COLOR=#000000[/COLOR] [COLOR=#000000]{[/COLOR]  
myRadians = [COLOR=#0000ff]Math[/COLOR].[COLOR=#0000ff]atan2[/COLOR][COLOR=#000000]([/COLOR]player.\_y-enemy.[COLOR=#0000ff]\_y[/COLOR], player.\_x-enemy.[COLOR=#0000ff]\_x[/COLOR][COLOR=#000000])[/COLOR];  
myDegrees = [COLOR=#0000ff]Math[/COLOR].[COLOR=#0000ff]round[/COLOR]COLOR=#000000[/COLOR];  
[COLOR=#0000ff]this[/COLOR].[COLOR=#0000ff]\_rotation[/COLOR] = myDegrees;  
enemy.[COLOR=#0000ff]\_x[/COLOR] += [COLOR=#0000ff]Math[/COLOR].[COLOR=#0000ff]cos[/COLOR]COLOR=#000000[/COLOR]\*zombiespeed;  
enemy.[COLOR=#0000ff]\_y[/COLOR] += [COLOR=#0000ff]Math[/COLOR].[COLOR=#0000ff]sin[/COLOR]COLOR=#000000[/COLOR]\*zombiespeed;  
[COLOR=#000000]}[/COLOR] [COLOR=#0000ff]else[/COLOR] [COLOR=#000000]{[/COLOR]  
myRadians = [COLOR=#0000ff]Math[/COLOR].[COLOR=#0000ff]atan2[/COLOR][COLOR=#000000]([/COLOR]castle.\_y-enemy.[COLOR=#0000ff]\_y[/COLOR], castle.\_x-enemy.[COLOR=#0000ff]\_x[/COLOR][COLOR=#000000])[/COLOR];  
myDegrees = [COLOR=#0000ff]Math[/COLOR].[COLOR=#0000ff]round[/COLOR]COLOR=#000000[/COLOR];  
[COLOR=#0000ff]this[/COLOR].[COLOR=#0000ff]\_rotation[/COLOR] = myDegrees;  
enemy.[COLOR=#0000ff]\_x[/COLOR] += [COLOR=#0000ff]Math[/COLOR].[COLOR=#0000ff]cos[/COLOR]COLOR=#000000[/COLOR]\*zombiespeed;  
enemy.[COLOR=#0000ff]\_y[/COLOR] += [COLOR=#0000ff]Math[/COLOR].[COLOR=#0000ff]sin[/COLOR]COLOR=#000000[/COLOR]\*zombiespeed;  
[COLOR=#000000]}[/COLOR]  
[COLOR=#0000ff]if[/COLOR] COLOR=#000000[/COLOR] [COLOR=#000000]{[/COLOR]  
enemy.[COLOR=#000080]health[/COLOR] -= gundamage;  
[COLOR=#000000] **var** [/COLOR] recoilX = enemy.[COLOR=#0000ff]\_x[/COLOR] += [COLOR=#0000ff]Math[/COLOR].[COLOR=#0000ff]cos[/COLOR]COLOR=#000000[/COLOR]\*recoil;  
[COLOR=#000000] **var** [/COLOR] recoilY = enemy.[COLOR=#0000ff]\_y[/COLOR] += [COLOR=#0000ff]Math[/COLOR].[COLOR=#0000ff]sin[/COLOR]COLOR=#000000[/COLOR]\*recoil;  
[COLOR=#000000] **var** [/COLOR] blood = [COLOR=#0000ff]\_root[/COLOR].[COLOR=#0000ff]attachMovie[/COLOR][COLOR=#000000]([/COLOR][COLOR=#ff0000]“blood”[/COLOR], [COLOR=#ff0000]“blood”[/COLOR]+depth, depth++[COLOR=#000000])[/COLOR];  
blood.[COLOR=#0000ff]\_rotation[/COLOR] = player.[COLOR=#0000ff]\_rotation[/COLOR]+[COLOR=#000080]180[/COLOR];  
blood.[COLOR=#0000ff]\_x[/COLOR] = recoilX;  
blood.[COLOR=#0000ff]\_y[/COLOR] = recoilY;  
enemy.[COLOR=#0000ff]play[/COLOR]COLOR=#000000[/COLOR];  
[COLOR=#000000]}[/COLOR]  
[COLOR=#0000ff]if[/COLOR] COLOR=#000000[/COLOR] [COLOR=#000000]{[/COLOR]  
enemy.[COLOR=#0000ff]gotoAndPlay[/COLOR]COLOR=#000000[/COLOR];  
enemy.[COLOR=#000080]dead[/COLOR] = [COLOR=#000000] **true** [/COLOR];  
[COLOR=#000000]}[/COLOR]  
[COLOR=#000000]}[/COLOR]  
[COLOR=#000000]}[/COLOR];  
[/LEFT]  
[/FONT]

What I need is a way to be able to create a variable with # of enemies and be able to generate them all with AS. I know that I should be using a for loop and a variable with number of enemies, I just cant seem to wrap my head around how to do it.

what i want is:  
ActionScript Code:  
[FONT=Courier New][LEFT]numenemies=[COLOR=#000080]0[/COLOR];  
nummaxenemies=[COLOR=#000080]5[/COLOR];  
[COLOR=#808080]_//attach random enemies here_[/COLOR]  
[COLOR=#808080]\*//code above to control all the enemies \*[/COLOR]

[/LEFT]  
[/FONT]

any help would be great 🙂
