by Fuzzy » Tue May 08, 2007 7:37 pm
Make an array.
in global code do this:
float Fade[16] = {0.9375, 0.875, 0.8125, 0.75, 0.6875, 0.625, 0.5625, 0.5, 0.4375, 0.375, 0.3125, 0.25, 0.1875, 0.25, 0.125, 0.0625};
Initialize an integer to zero and use that to refer to the Fade[16]. with each draw, add 1 to the int.
if (count < 16)
{
setVolume(channel, Fade[count]);
count ++;
}
You can do some nifty tricks with this. Take the distance of the player from the sound actor. change the distance from a float to an int with a type cast...
SoundDist = (int)distance(player.x, player.y, enemy.x, enemy.y);
and then force it to a positive value....
SoundDist = abs(SoundDist);
so now its 0 to whatever. we need to change it to the same range as the number of elements in the array. Its called normalizing. We do it with an AND operator.
SoundDist = SoundDist & 16;
So now GE considers out distance to be from 0 to 16. We use this to look up Fade...
setVolume(channel, Fade[SoundDist]);
A third way.... No array required.... use an int called SoundDist, same as before, and a float var called Vol.
switch(SoundDist)
{
case 0 :
Vol = 1.0;
break;
case 1 :
Vol = 0.9;
break;
case 2 :
Vol = 0.8;
break;
}
and so on...
A fourth way. This is the smoothest result. Determine the max distance you want to hear the sound at. Store it in a float var. Lets use MaxDist for a name. use a float called Vol to store our final result.
Get the distance from the player to the enemy.
Dist = distance(player.x, player.y, enemy.x, enemy.y);
which gives a nice float value from 0.0 to whatever. Add a tiny amount to it because we are going to divide, and want to avoid divide by zero errors.
Dist = Dist + 0.001;
Now we divide the two variables.
Vol = MaxDist / Dist;
But wait! at full distance, we get a vol of 1.0! thats no good! we need to reverse it.. easily done.
Vol = 1.0 - Vol;
and we set the volume as before..
setVolume(channel, Vol);
Done! Not checked for errors though!
Mortal Enemy of IF....THEN(and Inspector Gadget)
Still ThreeFingerPete to tekdino