tags: factory title: Spawn enemies with central management brief: This example shows how to spawn enemy game objects using a factory component and use a central manager to control movement and behavior of all enemies. author: Defold Foundation scripts: ship.script, spawner.script
This example shows how to dynamically spawn enemy game objects using a factory component and use a central manager to control movement and behavior of all enemies. The setup consists of three main components: a player ship, enemy spawner, and different enemy types with customizable properties.
Press keys 1, 2, or 3 to spawn different enemy types.
Example collection consists of 2 game objects:
The red ship at the bottom that automatically moves and shoots. Consists of:
bulletfactory to spawn bullet game objectsship that handles automatic movement (ping-pong animation) and bullet spawning every 0.25 secondsBullets are simply animated upward and automatically deleted when they reach the top.
Controls enemy spawning with keyboard input. Consists of:
enemyfactory to spawn enemies with different propertiesexample_description with instructions text displayed on topspawner that spawns enemies.The spawner script defines three different enemy types: random, diagonal, and straight.
Uses a factory to create enemies and store their ids and properties in a look-up table:
local properties = ENEMY_TYPES[enemy_type]
local id factory.create("#enemyfactory", position)
self.enemies[id] = { ... }
The spawner script handles enemy movement by looping over the list of currently spawned enemies in the update() lifecycle function. It also uses a physics event listener to detect any enemy to bullet collisions. The spawner script also deletes enemy objects and removes them from the look-up table.
Random Enemy (Key 1):
Diagonal Enemy (Key 2):
Straight Enemy (Key 3):
Combine this example with other movement and physics examples to create a complete shoot'em up game!