Game Development Reference
In-Depth Information
There's more...
Now that we have the muzzle flash and added it to a weapon, we can create a control in or-
der to use it within the game by performing the following steps:
1. Create a new class called
WeaponControl
extending
AbstractControl
.
2. Add a
ParticleEmitter
field called
muzzleFlash
.
3. In the
setSpatial
method, check whether the supplied spatial has a suitable
child, either by type or name (requires that the muzzle flash has a fixed name), and
set the
muzzleFlash
field:
muzzleFlash = (ParticleEmitter)
((Node)spatial).getChild("MuzzleFlash");
4. Now, we create a publicly available
onFire
method and add the following:
if(muzzleFlash != null){
muzzleFlash.emitAllParticles();
}
This control should then be added to the weapon spatial inside the game and
onFire
should be called whenever the weapon fires. The class is suitable to play sounds and keeps
track of ammunition as well.