Table of Contents

Shooting Bullets with a Spawner

This tutorial will show you how to set up a spawner to shoot bullets that can be turned on and off from code. Normally if you attach a spawner to an object, you can only turn it off using orxObject_Enable. But that will make the parent object invisible as well. We use a child object in the case.

Assets

You can use the following assets in this tutorial.

Config

In this config, we create a CannonObject. We give it an empty child object called: CannonSwitchingObject. The spawner CannonSpawner is attached to CannonSwitchingObject.

CannonSwitchingObject is the one we will turn on or off to stop the spawner from shooting bullets.

[CannonObject]
Graphic   = CannonGraphic
Position  = (100, 100, 0)
Scale     = 5
ChildList = CannonSwitchingObject
 
[CannonGraphic]
Texture = spawner-gun.png
 
[CannonSwitchingObject]
Spawner = CannonSpawner
 
[CannonSpawner]
Object          = BulletObject
WaveSize        = 1
WaveDelay       = 0.2
ActiveObject    = 10
Position        = (10, -1, 0)

Next, the bullet object to be fired by the CannonSpawner.

[BulletObject]
Graphic  = BulletGraphic
Speed    = (300, -15, 0) ~ (300, 15, 0)
LifeTime = 2.0
 
[BulletGraphic]
Texture = spawner-bullet.png

Code

Then in the code, use the cannonObject to get the switchObject by calling orxObject_GetChild on the cannonObject.

Finally, use orxObject_Enable(switchObject, orxFALSE) to stop the spawner shooting bullets, or use orxTRUE to switch it back on.

orxOBJECT *cannonObject;
orxOBJECT *switchObject;
cannonObject = orxObject_CreateFromConfig("CannonObject");
switchObject = (orxOBJECT*)orxObject_GetChild(cannonObject );
 
orxObject_Enable(switchObject, orxFALSE); //turn off the firing
 
orxObject_Enable(switchObject, orxFALSE); //turn on the firing