Reusing variable names for different MovieClips

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:


//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:

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.



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