# Need help with making global variable

**URL:** <https://forum.kirupa.com/t/need-help-with-making-global-variable/262430>\
**Category:** Uncategorized\
**Created:** [June 5, 2008, 8:59pm UTC](https://forum.kirupa.com/t/need-help-with-making-global-variable/262430 "2008-06-05T20:59:19Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![youngtrill](https://avatars.discourse-cdn.com/v4/letter/y/6bbea6/32.png) [@youngtrill](https://forum.kirupa.com/u/youngtrill)\
**Post date:** [June 5, 2008, 8:59pm UTC](https://forum.kirupa.com/t/need-help-with-making-global-variable/262430/1 "2008-06-05T20:59:19Z")

</div>

1. i have my enemies just spawning in same spot forever  
id like to set up a global variable that will increase when i press right  
and then when that variable reaches certain numbers… like 300 and 600  
i want an enemy to be attached
2. if u test this youll see enemies coming from one side  
how would i make them come from both sides  
[HERE IS A DEMO](http://spamtheweb.com/ul/upload/050608/8449_game.php?lightbox)  
if you know how to solve either of these please by all means assist

all im doing is attachin a movie using  
a copied code…  
_dont get me wrong i can completely remake this whole code its pretty much the only enemy ai code i ever used and ive tweaked it from bottom to top plenty of times_  
_what it does is attaches an enemy and the enemy follows the AIzombie function  
which tells it to walk or die and if i hit any of the monstersinscene they die and are removed from the stage  
which will then make another respawn_  
this isnt the code that i will use if someone can help me with the global variable thing im asking for  
i want it so that when my character is moving right the global variable called MainX is increasing by increments of 10  
and once it reaches 200 i want to have a movie attached at at the coordinates \_x:600, \_y:305 and i want the movieclip to follow the same function that is listed below but ill take out the part that makes it spawn again

[SIZE=5] **my character movement is basic** [/SIZE]

\*\*hero.step=15;  
hero.attack = false;  
hero.stance= 1;  
hero.crouch=false;  
\*\*  
\*\*this.onEnterFrame = function()  
{\*\*\*\*if (Key.isDown (Key.RIGHT) )  
{  
hero.\_xscale = 100;  
hero.stance=1;  
hero.\_x+=hero.step;  
hero.gotoAndStop(“walk”);  
}

[SIZE=6][COLOR=Navy]  
[/COLOR][/SIZE]**[SIZE=6][COLOR=Navy]this is the enemy spawn code [/COLOR][/SIZE]**[SIZE=6][COLOR=Navy]

[/COLOR][/SIZE]\*\*\*\* SpawnEnemy = function ()  
{  
//INITIALISE VARS

```
    attachedObj = _root.attachMovie("zombie", "brownzombie"+tag, 9, {_x:600, _y:305});
    
    
    attachedObj.health = 100;
    attachedObj.speed=5;
    attachedObj.hit=false;
    attachedObj.alive = true;
    attachedObj.gotoFunction = AIZombie;
           
    MonstersInScene.push(attachedObj);
    
    
    //MonstersInScene
    tag++;

```

}

function AIZombie()  
{  
if(this.alive)  
{  
this.gotoAndStop(“walk”);  
//YOUR FIRST AI

```
    if(tukelay._x&lt;=this._x-80)
    {
        this._xscale = 100;
        this._x-=this.speed;
    }
    else if(tukelay._x&gt;=this._x+80)
    {
        this._x+=this.speed;
        this._xscale = -100;
    }
    
        
    //We have to add a small offset of + and - 20
            //to stop the zombie 
    //from 'flickering' when he's close. Remove the numbers- you'll see what I mean
    
    
    
}
else
{
    
    this.gotoAndStop("dead");
    //Activates the dying animation, which automatically removes this movie clip/sprite
    //when it reaches the final frame
}

```

}  
function RenderEnemy()  
{

```
for (counter=0;counter &lt; MonstersInScene.length;counter++)    
{
        MonstersInScene[counter].gotoFunction();
        
        if(tukelay.attack)
        {    
            if(tukelay.hitTest(MonstersInScene[counter]))
                MonstersInScene[counter].alive=false;
        }
        
        if(MonstersInScene[counter]._x==null)
        {
            MonstersInScene.splice(counter,1);
            SpawnEnemy();
                        
        } ** sorry thats alot but all i need is someone to help me out or guide me in making

```

a global x variable that increases as my character moves right and when the global x reaches  
200 attach an enemy

---

<div class="post-metadata">

**Author:** ![youngtrill](https://avatars.discourse-cdn.com/v4/letter/y/6bbea6/32.png) [@youngtrill](https://forum.kirupa.com/u/youngtrill)\
**Post date:** [June 6, 2008, 12:43pm UTC](https://forum.kirupa.com/t/need-help-with-making-global-variable/262430/2 "2008-06-06T12:43:29Z")

</div>

i solved the spawn in 2 places problem

now for the other one
