RSS

c) Effects

It’s time for sounds and particles!

Adding particle and sound effects

See file ‘2 - particles and sounds.bmx’

Particle effects for the framework are easily made using the Particle Editor tool and even easier to use in your game module.

Take a look again at the resource file the game module is using “Media/gameResources.txt”, we’ve now added sound effects and particle emitters to the list (as well as some comments);

‘
‘ Our sprites
‘
SPRITE player = Media/Sprites/player.png
SPRITE bullet = Media/Sprites/Bullet.png
SPRITE enemy = Media/Sprites/1/1.csv
‘
‘ Our sounds
‘
SOUND gameover = Media/sounds/gameover.wav
SOUND fire = Media/sounds/shoot.wav
SOUND collide = Media/sounds/collide.wav
‘
‘ Our particle emitters
‘
EMITTER playerfire = Media/Emitters/Fire.txt
EMITTER explosion = Media/emitters/Explosion.txt

Playing sounds

In our app module we can access sound effects by the name we gave them in the resource file.  We can add sounds for explosion and player firing events by adding one line of code for each sound:

For the player fire, we add this:

PlaySoundByName( “fire” )

For the collisions and enemy destruction, we add this:

PlaySoundByName( “collide” )

The PlaySoundByName function also takes an optional volume parameter.

You may be thinking that all this “ByName” retrieval is going to hurt performance and you’d rather not do it.  Well, you don’t have to.  You can always maintain your own fields or variables to store the objects and work with them directly.  In the case of sound effects you can use GetSound to retrieve a TSound object and use PlaySound to play it.  For the sake of the tutorials and even for most games, retrieving ByName isn’t going to hurt and it keeps the code down.

Particles

Particle emitters (like every resource in the framework) are also retrieved by name (with GetEmitter), and when we want to them to sprout more particles we call their DoBurst method.   We then simply call DrawParticleEmitters somewhere in our Draw method.  All the particles and emitters are automatically updated for us.

For the player fire, we burst out some yellow particles..

GetEmitter( “playerfire” ).DoBurst( t.GetX(), t.GetY(), True  ) 

For the collisions we use the explosion emitter..

GetEmitter( “explosion” ).DoBurst( t.GetX(), t.GetY(), False  )

Notice the last flag (fpsSync) is different between the two calls?

Particle emitters are limited to running at 50 frames per second; so their DoBurst method will ignore calls that come to soon since the last one.  This helps the emitters give out the same results across varying frame rates.  The problem with that is in the case of the “explosion” emitter above, we might detect several collisions in one loop and call DoBurst many times over.  In this case we don’t want the emitter limiting it’s output.

And that’s sounds and particles covered!

On to High Scores

Comments are closed.