Game Development Reference
In-Depth Information
function PlayBackgroundMusic () {
if(audio.isPlaying == true) {
audio.Stop();
}
audio.Play();
}
This breaks down as follows:
(1) PlayBackgroundMusic();
1.
Keep the Start() function uncluttered by putting the code block for playing
the background music in its own function, PlayBackgroundMusic() , which is
called from the Start() function.
(2) if(audio.isPlaying == true)
2.
Check to see if the audio clip is already playing. It's good practice to add this
test rather than simply using audio.Play() by itself, to avoid strange audio
behavior.
(3) audio.Stop();
3.
If the audio clip is already playing, stop it.
(4) audio.Play();
4.
Start playing the audio clip from the beginning.
In the Inspector, uncheck Play On Awake and Loop in the Audio Source component. Attach the
BackgroundMusic script to the Audio game object. Save the scene and playtest. At this point you
have the script equivalent of Play On Awake.
Next change the audio clip by dragging the “Loop 16 - 145 bpm - intro” intro clip from the Project
panel into the Audio Clip property field of the Audio Source component in the Inspector. Save and
playtest, and now the intro clip plays immediately and only once.
In MonoDevelop, edit the BackgroundMusic script to:
#pragma strict
public var musicLoop : AudioClip;
private var introMusicIsPlaying : boolean = true;
private var looping : boolean = false;
function Start () {
PlayBackgroundMusic();
}
 
Search WWH ::




Custom Search