Game Development Reference
In-Depth Information
function Update() {
if (!audio.isPlaying) {
introMusicIsPlaying = false;
}
if (!introMusicIsPlaying && !looping) {
looping = true;
LoopBackgroundMusic();
}
}
function PlayBackgroundMusic () {
if(audio.isPlaying == true) {
audio.Stop();
}
audio.Play();
}
function LoopBackgroundMusic () {
if(audio.isPlaying == true) {
audio.Stop();
}
audio.clip = musicLoop;
audio.loop = enabled;
audio.Play();
}
Breaking this down:
(1) public var musicLoop : AudioClip;
1.
Declare a reference variable of type AudioClip to hold the second audio clip
to be played, and name it musicLoop .
(2) private var introMusicIsPlaying : boolean = true;
2.
Declare a private boolean variable named introMusicIsPlaying and set it to
true , since PlayBackgroundMusic() is called from the Start() function and
will immediately begin playing the intro clip.
(3) private var looping : boolean = false;
3.
Declare a private boolean variable named looping and set it to false .
(4) if (!audio.isPlaying)
4.
In the Update() function, check to see when the intro clip is finished playing.
Remember that ! means NOT, so this line of code can be read as “If the audio
clip is not playing,” which will be true when the intro clip is over.
(5) introMusicIsPlaying = false;
Search WWH ::




Custom Search