While working on The Shine of a Star, we made a sound emitter to fade sound in and out from a place in the scene. It works by measuring the distance between the actor and a point in the scene, the closer to the point the louder the sound to a maximum of 100 percent.
We put the script as a function in base.script and calls it from wherever we want to use it.
The arguments are sound (the pathway to the song/sound in a string), x, y, a radius from where the sound should start fade in, if it should loop and which channel it should play the sound through. If no loop is chosen, it loops automatically and if no channel is set, it uses the playSound method.
So here is the script:
function soundEmitter(var sound, var positionX, var positionY, var maxRadius, var loop, var channel)
{
if(loop == null)
{
loop = true;
}
if (channel == null)
{
Game.PlaySound(sound,loop);
}
else
{
Game.PlayMusicChannel(channel, sound, loop);
}
var update = true;
var distance = distanceCalc(positionX, positionY);
var p1;
var precent;
while(update)
{
distance = distanceCalc(positionX, positionY);
p1 = 1 - distance/maxRadius;
precent = p1 * 100;
if(precent >100)
{
precent = 100;
}
if (precent < 0)
{
precent = 0;
}
if (channel == null)
{
Game.SetSoundVolume(precent);
}
else
{
Game.SetMusicChannelVolume(channel, precent);
}
Sleep(10);
}
}
And here is a simple example of how to call for the function. In this particular example, the function is called from a sprite event for a bird to play a random bird cry every time it opens it's mouth:
on "Speak"
{
r = Random(1,3);
soundEmitter("Musik\Den första scenen\Mourning Bird "+ r +".ogg",this.X,this.Y,1600,false);
}
Hope it is usefull for someone, Cheers!
EDIT: Worth mentioning is that the script measure distance (i.e., a circle), and will work on both the X-axis and the Y-axis.