Spawning enemies when they are at a specific distance from player(a more efficient way)


(billy2000) #1

I want in my game to make enemies be removed when they are at a specific distance form player and then to be added again if player gets close to their spawning points(exact same thing like in kirby games with spawning enemies).Also im using ogmo for the map.In this case i made this code :

private function respawnEnemies(mapXML:XML):void
	{
		var node:XML;
		
		if (whereTele == "UpDown") {//if map is wide
			if (blueNR != 0) {//if this type of enemy exist in this map
				var blueE:EnemyBlue;
				var enemyX:Number = 0;
				for each (node in mapXML.Entities.Blue)
				{
					//getting enemy's x
					enemyX = int(node.@x);
					//checking if he is in the spawning position
					if ((enemyX > camera.x + 650 && enemyX < camera.x + 690) || (enemyX < camera.x - 50 && enemyX > camera.x - 100)) {
						//checking if the enemy exist allready
						blueE = getInstance(String(node.@myname));
						if (blueE == null) {
							//adding it
							add(new EnemyBlue(int(node.@x), int(node.@y), String(node.@myname)));
						}
					}
				}
			}
			
		}
		else {//if map is from not wide(obviously)
			//the enemyes will be spawned using y coordinate not x to decide when
		}
	}

My question is: Is there any other more efficient way of doing this? thx :smiley:


(Jonathan Stoler) #2

I don’t have time right now to thoroughly look through your code, but using recycle() instead of remove() (this part isn’t in the code you posted) will make it more efficient if you’re seeing memory or lag problems.


(Zachary Lewis) #3

You could create a spawner class that keeps a reference to it’s spawned enemy and a distance. Then, on each update, check the distance from the spawner to the player.

Spawner::update()

// Get the distance between the spawner and the player.
var distance:Number = FP.distance(player.x, player.y, x, y);

// If the player is closer to the spawner than a set spawn distance, spawn an enemy
// if the enemy doesn't exist.
if (distance < spawnDistance && spawnedEnemy == null)
{
  spawnedEnemy =   Enemy(FP.world.create(Enemy), false);

  // Set initial conditions before adding to the world.
  spawnedEnemy.x = x;
  spawnedEnemy.y = y;

  FP.world.add(spawnedEnemy);

}

// If the player is further from the spawner than a set despawn distance,
// remove the enemy if it isn't already removed.
else if (distance > despawnDistance && spawnedEnemy != null)
{
  FP.world.recycle(spawnedEnemy);
  spawnedEnemy = null;
}

Use this code as an idea. There are unsolved problems in it, such as, if the player kills an enemy, it might respawn instantly or never respawn depending on how your kill function works. Additionally, if the enemy chases the player, it will vanish once the player gets far enough away from its spawn point.


(billy2000) #4

ty :smiley:

@zachwlewis That means to make a entity for each enemy i have on the map?


(Zachary Lewis) #5

You could write a manager class that stores a list of spawner objects and updates them each tick, but that’s a premature optimization. Make sure it works the way you want it first, then improve from there.


(billy2000) #6

ok ty very much :smile: PS: by making a entity/object for each of my enemies wouldn’t increase lag?


(Ultima2876) #7

You can generally have thousands of entities before it’ll make any appreciate dent in performance, so it shouldn’t make a big difference. Using entities is the preferred way to work with FlashPunk.


(billy2000) #8

oh k ty :smile: idk why thought if there are many entities it begins to lag XD