TGB/ScriptTutorials/MusicTutorial
From TDN
|
[edit] IntroductionWhat a player hears in game can be just as important as what he sees! This tutorial will show you how to add ambient (background) music with looping. Also covered is how to integrate non-looping sound effects to your game. The tutorial is aimed at beginner level users of TGB. [edit] Getting Started
[edit] The AudioDatablock
new AudioDescription(AudioLooping)
{
volume = 1.0;
isLooping= true;
is3D = false;
type = $GuiAudioType;
};
new AudioDescription(AudioNonLooping)
{
volume = 1.0;
isLooping= false;
is3D = false;
type = $GuiAudioType;
};
Very similar to our first AudioDescription, except that isLooping is now false.
new AudioProfile(ambient1Audio)
{
filename = "~/data/audio/oceanwhales.wav";
description = "AudioLooping";
preload = true;
};
Your entire audioDatablocks.cs file should now look like this:
new AudioDescription(AudioLooping)
{
volume = 1.0;
isLooping= true;
is3D = false;
type = $GuiAudioType;
};
new AudioDescription(AudioNonLooping)
{
volume = 1.0;
isLooping= false;
is3D = false;
type = $GuiAudioType;
};
new AudioProfile(ambient1Audio)
{
filename = "~/data/audio/oceanwhales.wav";
description = "AudioLooping";
preload = true;
};
Save your audioDatablocks.cs file and open up main.cs, found in the games\Fish folder. Find this line:
// Exec game scripts.
exec("./gameScripts/game.cs");
and add this just below it:
exec("./gameScripts/audioDatablocks.cs");
Save and close the main.cs file. [edit] Singing Fish
function FishClass::onLevelLoaded(%this, %sceneGraph)
{
%this.setLinearVelocityX(-20);
%worldLimit = %this.getWorldLimit();
%limit0 = getWord(%worldLimit, 1);
%limit1 = getWord(%worldLimit, 2);
%limit2 = getWord(%worldLimit, 3);
%limit3 = getWord(%worldLimit, 4);
%this.setWorldLimit("NULL", %limit0, %limit1, %limit2, %limit3 , true);
}
Now, change it to look like this:
function FishClass::onLevelLoaded(%this, %sceneGraph)
{
// play ambient music
$fishMusic = alxPlay(ambient1Audio);
%this.setLinearVelocityX(-20);
%worldLimit = %this.getWorldLimit();
%limit0 = getWord(%worldLimit, 1);
%limit1 = getWord(%worldLimit, 2);
%limit2 = getWord(%worldLimit, 3);
%limit3 = getWord(%worldLimit, 4);
%this.setWorldLimit("NULL", %limit0, %limit1, %limit2, %limit3 , true);
}
To start a sound file, we use the alxPlay() function with a reference in the quotes to our only AudioProfile. [edit] Turn it off!
function endGame()
{
sceneWindow2D.endLevel();
moveMap.pop();
}
Change it to this:
function endGame()
{
alxStop($fishMusic);
sceneWindow2D.endLevel();
moveMap.pop();
}
Easy? You bet. Non looping sounds work the same way, use alxPlay() inside your game specific functions, remembering that timing is key. To test it out you can simply change our existing AudioProfile for the ocean whales to:
new AudioProfile(ambient1Audio)
{
filename = "~/data/audio/oceanwhales.wav";
description = "AudioNonLooping";
preload = true;
};
or create a new AudioProfile with a much shorter duration sound effect. Examples in this area can be seen in the scrollerDemo. The audioDatablocks.cs file can be found in games\scrollerDemo\data\audio\. Check it out and make your games sound great! |
Categories: TGB | Audio | Tutorial








