Randomly Spawning Enemies?

Subscribe to Randomly Spawning Enemies? 3 posts

Sign in to reply


 
avatar for DNDProductions DNDProductions 42 posts
Flag Post

Okay, so I’m making a game in which you need to dodge enemies. I have this code on a movieclip:

onClipEvent (load) {
var t:MovieClip;
}
onClipEvent (enterFrame) {
if (_root.enetimer<0) {
_root.enetimer = random (200)
n = root.getNextHighestDepth();
t = attachMovie(“enemy”,"enemy"+n,n);
t._x = random (300) + 100
t.
y = random (500)
}
}

onClipEvent (enterFrame) {
t._y -= 10
_root.enetimer = _root.enetimer – 5
}

The code is supposed to make a new enemy appear whenever the variable “enetimer” reaches zero. Instead, there is only one enemy onscreen and it moves around whenever “enetimer” reaches zero. I’m sure I’m missing the part of the code that makes multiple enemies, but I don’t know what it is. Can someone help?

 
avatar for Moonkey Moonkey 1007 posts
Flag Post

The reason the enemy is jumping around is because each new enemy you spawn is overwriting the previous one. That happens when you try to put the new ones all in the same depth. You’re obviously trying to avoid that by using your n variable, but I think it’s not getting set properly because you’re missing a semicolon beforehand. Just slot one in after the random(200) and I think it’ll work.

_root.enetimer = random (200); n = _root.getNextHighestDepth();

 
avatar for pel6413 pel6413 72 posts
Flag Post

bq._root.enetimer = random (200); n = _root.getNextHighestDepth();bq.
that is not the problem.
the problem is that you are using _root.getNextHighestDepth(); but your not attaching the movieClip to _root.
this works:

onClipEvent (load) { 
	var t:MovieClip; 
	_root.enetimer = 0
} 
onClipEvent (enterFrame) { 
	if (_root.enetimer<0) { 
		_root.enetimer = random (200) 
		n = this.getNextHighestDepth(); 
		t = attachMovie("enemy","enemy"+n,n); 
		t._x = random (300) + 100 
		t._y = random (500)
		t.onEnterFrame = function() {
			this._y -= 10
			if(this._y<-this._width) {
				this.removeMovieClip();
			}
		}
	} 
	_root.enetimer -= 5
}


Sign in to reply