tags: factory title: Spawn enemies with script properties brief: This example shows how to spawn enemy game objects using a factory component with different properties. author: Defold Foundation scripts: ship.script, enemy.script, spawner.script
This example shows how to dynamically spawn enemy game objects using a factory component with different properties. 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 factory to create enemies with specific properties:
local properties = ENEMY_TYPES[enemy_type]
factory.create("#enemyfactory", position, nil, properties)
Random Enemy (Key 1):
Diagonal Enemy (Key 2):
Straight Enemy (Key 3):
Properties defined in enemy.script control enemy behavior:
sprite - Which sprite to displayhealth_points - How many hits before destructionspeed - Movement velocity vectoris_random - Whether to use random movement changesWhen enemies have go.property defined in their script, these properties are visible in the Properties pane and can be customized per enemy type.
Combine this example with other movement and physics examples to create a complete shoot'em up game!