Lesson 10 - Adding Sound
Compared to working with graphics, sound is relatively straightforward. However, there are some important things to keep in mind.
Aside from all this, it is very straightforward to get Director to play a sound. The most obvious way is to drop a sound cast member into a sound channel in the score. By doing this, you attach a sound to a frame. The sound will start and continue playing while the playback head remains in that frame, unless the sound itself is not set to loop or a lingo command takes over the sound channel.
I usually advise against using the score sound channels because they lack flexibility and control. A better way to play sounds is to trigger them at the appropriate place in your lingo code. The basic sound effect instruction is
puppetsound channel, soundCastMember
Something about puppets...
The word 'puppet' suggests external or remote control. There is also a puppetsprite command which you may see in books and elsewhere. External control violates one of the central principles of object oriented design: Objects should be responsible for themselves. A pure OO design has no 'managers' and no 'controllers'. This lends enormous flexibility to the system. Objects can be freely added and removed without the whole system falling apart.
Earlier versions of Director relied on puppetsprite whenever Lingo was going to do anything visually interesting. This command switched the properties of sprite objects in the score to external control by lingo. The resulting mess may be remembered by some readers as counter-intuitive at best, misery-inducing at worst.
Director 6 introduced 'auto puppeting' and (more importantly) the ability to extend the sprite object with attached behaviors. Some would argue that this is just a conceptual trick and that there is actually no essential difference between puppetting a sprite channel with an object birthed from a parent script (technical stuff) and having a behavior sitting 'inside' the sprite, controlling it as a self contained object.
I would say that this conceptual trick makes all the difference in the world, that it allows more intuitive thinking about the way that sprites should behave. On top of this there is the fact that working with parent scripts requires sophisticated abstract knowledge whereas dragging a behavior over a sprite is easy enough for absolute beginners to do.
Using puppetsprite in Director 6 or later is no longer necessary. Some die-hards have their own reasons for using it, some good, some bad. Beginners and those without set habits should steer well clear of puppetsprite. It is confusing and practically obsolete.
Unfortunately, the effects channels (sound, transitions, tempo) are still controlled 'from the outside'. You can not drag a behavior into the sound channel. Fortunately, there are far fewer properties and commands to consider so this is acceptable for now.
So if you want to play the sound cast member "explosion" in channel 1, you would type
puppetsound 1, "explosion"
It is possible to use puppetsound without specifying a sound channel. Don't do this. I have found it to be very unreliable.
The sound will be played at the next screen update. Usually this will synchronise the sound with whatever else is going on. On rare occasions you may wish to issue an updatestage command to trigger the sound more immediately. Be careful, because this command will also update the screen and cause extra frame messages to be generated.
Find, create or download some sounds. You may need to convert sounds to a different format before you can import them into Director. Director imports AIFF and WAV format sounds without any problem. Make sure your sounds are named appropriately. I am going to assume we have a sound called "explosion" and one called "blast". We are also going to use sound channels 1 and 2.
NOTE! If a sound is playing and you issue a puppetsound command to the same channel, the new sound will override the one that is playing.
The first thing to do is to add a sound to the bullet when it gets fired.
on shoot me
if shooting then
return
end if
set shooting to true
set the loc of sprite mysprite to the loc of sprite 1
puppetsound 1, "blast"
end
Notice that we place this sound after the test for
shooting = true
The other place we want a sound is in the hit handler of the invader.
on hit me set alive to false set the locH of sprite mySprite to -999 puppetsound 1, "explosion" end
That was simple enough. Feel free to add more sounds and experiment with adding puppetsound instructions at different places in your script. Keep bandwidth in mind and don't forget that sound channels can only play one sound at a time. The most recent puppetsound command will always dominate. If you have the luxury of making a Mac-only CDROM, go all out and add as many channels of CD quality sound as you wish!
I think you will agree that the game has much more presence with a little sound.