# Reusing variable names for different MovieClips

**URL:** <https://forum.kirupa.com/t/reusing-variable-names-for-different-movieclips/323376>\
**Category:** flash\
**Created:** [June 28, 2011, 9:34am UTC](https://forum.kirupa.com/t/reusing-variable-names-for-different-movieclips/323376 "2011-06-28T09:34:34Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![BadBoyMcCoy](https://avatars.discourse-cdn.com/v4/letter/b/bbe5ce/32.png) [@BadBoyMcCoy](https://forum.kirupa.com/u/BadBoyMcCoy)\
**Post date:** [June 28, 2011, 9:34am UTC](https://forum.kirupa.com/t/reusing-variable-names-for-different-movieclips/323376/1 "2011-06-28T09:34:34Z")

</div>

Hi All,  
I’m creating a pacman game and basically I’m having trouble coding the enemies (ghosts), the code works perfectly with just one ghost, as soon as I add another it starts to go wrong.

Firstly I’ve got this function which places the enemies:

```auto

//places the enemies
function placeEnemies() {
		
	_root.attachMovie("enemy1", "enemy1", _root.getNextHighestDepth());
	
	enemy1._x = 268;
	enemy1._y = 205;
	
	_root.attachMovie("enemy2", "enemy2", _root.getNextHighestDepth());
	
	enemy2._x = 407;
	enemy2._y = 474;
	
}

```

I think this is fine. Next I have this code which basically tells each enemy to run the “enemyMovement” function every frame, the function tell the enemies how to move depending on a bunch of different factors:

```auto
enemy1.onEnterFrame = enemyMovement;
enemy2.onEnterFrame = enemyMovement;

```

Next (and I think this is where the problem is) I have the actual “enemyMovement” function. Now I think the problem is that the variables I’m using in this function aren’t local to each specific enemy. For example I think enemy1 and enemy2 are using the same variables (like eLeft, eCanGoRight etc…), and that this is messing up the way the enemies move around.

```auto

function enemyMovement() {
	
	//check which directions it's possible to go
	if(pmPath.hitTest(this._x-eSpeed,this._y,true)) {
		//if not going right
		if(!eRight) {
			eCanGoLeft = true;
		}
	}
	else {
		eCanGoLeft = false;
	}
	if(pmPath.hitTest(this._x+eSpeed,this._y,true)) {
		if(!eLeft) {
			eCanGoRight = true;
		}
	}
	else {
		eCanGoRight = false;
	}
	if(pmPath.hitTest(this._x,this._y-eSpeed,true)) {
		if(!eDown) {
			eCanGoUp = true;
		}
	}

//loads more movement code below

```

So my question is this, is it possible to keep the variables unique to each enemy, for example could I do something like “this.eLeft” or somehow do something like “eLeftEnemy1, eLeftEnemy2” without repeating loads of code.

TBH I’ve been stumped so long I’m not far off of just repeating the function with a slightly different name for each of the enemies.

TIA  
McCoy
