Object Spawning

As in the projectile example, sometimes an object needs to spawn another object. For example, an object could explode into several pieces, resulting in several spawns traveling in different directions; a bot could drop its weapon; or "sparks" could fly off a bot when it gets hit. It's probably a good idea to let any game object spawn another, so you'll add code in the GameObject class to add and retrieve spawns, shown here in Listing 13.17.

Listing 13.17 Spawning in GameObject
private List spawns;
...
/**
 Spawns an object, so that the GameManager can
 retrieve later using getSpawns().
*/
protected void addSpawn(GameObject object) {
 if (spawns == null) {
 spawns = new ArrayList();
 }
 spawns.add(object);
}
/**
 Returns a list of "spawned" objects (projectiles,
 exploding parts, etc) or null if no objects
 were spawned.
*/
public List getSpawns() {
 List returnList = spawns;
 spawns = null;
 return returnList;
}


After an update, the game object manager should call getSpawns() and add any spawns to the list of objects it manages.



   
Comments