Game Development Community

Stop music on level change?

by Johan Brodd · in Game Design and Creative Issues · 04/23/2012 (10:01 pm) · 4 replies

Forgive my noob questions, but this is the situation:

I have my level set up with an object that starts the music when the level is started. So far so good. I'm doing this game through behaviors. Now all I need to know is where to put alxStop to stop the music before the new one is started. Scripting ain't really my thing, so forgive my unstructured script. :)

/* AUDIO PROFILE BEHAVIOR 
       by Joe Rossi
    
   This is my workaround to the TGB editor not having audio support in the editor.
  
   Add this behavior to an invisible staticSprite, or one off screen.  
   I personally use a sprite with a music note texture ;)
      
   When the level is loaded, it creates a new audioprofile.
   
   This will look in your game/data/audio folder for the sound
   
   Modified by Johan Brodd.
 */


if (!isObject(AudioProfileBehavior))
{
   %template = new BehaviorTemplate( AudioProfileBehavior );
   
   %template.friendlyName = "Audio Profile";
   %template.behaviorType = "Audio";
   %template.description  = "Creates a new AudioProfile when this object is loaded.";
  
   %template.addBehaviorField( enable, "Is the object enabled", bool, true );
   %template.addBehaviorField( debug, "Activates debug mode", bool, true );
   %template.addBehaviorField( autoplay, "Play sound after the specified delay time.", bool, true );
   %template.addBehaviorField( name, "Name for this audio profile", string , "test"   );
   %template.addBehaviorField( filename, "File name for the audio file in your data/audio directory.", string , ""   );
   %template.addBehaviorField( volume, "The default volume", float ,  1.0  );
   %template.addBehaviorField( description, "The Audio Description to use", string , "test"  );
   %template.addBehaviorField( stopit, "Stop playback at the level end?", bool, true );
} 
 

function AudioProfileBehavior::onBehaviorAdd(%this){  
  if (!isobject( %this.name ) )
  { 
    if(%this.debug) echo("");
	if(%this.debug) echo("");
	if(%this.debug) echo("");
	if(%this.debug) echo("===========================");
	if(%this.debug) echo("Creating new audio profile:");
	if(%this.debug) echo("===========================");
	if(%this.debug) echo("");
	
	new AudioProfile(  %this.name  )
	{
      volume   =  %this.volume ;
      filename = "~/data/audio/" @ %this.filename  ;  
      description =  %this.description;
    };
	
	if(%this.debug) echo("Name: ", %this.name);
	if(%this.debug) echo("Volume: ", %this.volume);
	if(%this.debug) echo("Filename: ","~/data/audio/",%this.filename);
	if(%this.debug) echo("");
  }  
}
 
function AudioProfileBehavior::onLevelLoaded(%this, %scenegraph) 
{
	if(%this.debug)
	{
		if(%this.debug) echo("");
		if(%this.debug) echo(%this);
		if(%this.debug) echo("");
		if(%this.debug) echo("Level loaded");
		if(%this.debug) echo("------------");
		if (%this.enable) echo(%this.name, " is enabled");
		if (!%this.enable) echo(%this.name, " is disabled");
		if (%this.autoplay) echo("Playing ",%this.name,".");
		if (!%this.autoplay) echo("Waiting to play ",%this.name,".");
		echo("");
	}
	
	if(!%this.enable) return;
	if(!%this.autoplay) return;
	if(%this.autoplay) alxPlay(%this.name);
}

function AudioProfileBehavior::onRemoveBehavior(%this)
{
if(%this.debug)
	{
		echo("");
		if(%this.debug) echo("Level ended");
		if(%this.debug) echo("-----------");
		if(%this.enable) echo(%this.name, " is enabled");
		if(!%this.enable) echo(%this.name, " is disabled");
		if(%this.stopit) echo("Stopping ",%this.name,".");
		if(!%this.stopit) echo("Keeps playing ",%this.name," in the next level.");
	}
	
	if(!%this.enable) return;
	if(!%this.stopit) return;
	if(%this.stopit) alxStop(%this.name);
}

function AudioProfileBehavior::onLevelEnded(%this)
{
	if(%this.debug)
	{
		echo("");
		if(%this.debug) echo("Level ended");
		if(%this.debug) echo("-----------");
		if(%this.enable) echo(%this.name, " is enabled");
		if(!%this.enable) echo(%this.name, " is disabled");
		if(%this.stopit) echo("Stopping ",%this.name,".");
		if(!%this.stopit) echo("Keeps playing ",%this.name," in the next level.");
	}
	
	if(!%this.enable) return;
	if(!%this.stopit) return;
	if(%this.stopit) alxStop(%this.name);
}

#1
04/26/2012 (2:52 am)
Time for a bump.
#2
04/27/2012 (3:07 pm)
Call alxStopAll() in your onLevelEnded() callback - that way you don't have to cycle through everything yourself.

Also, if you want to stop a specific sound you need to save the return from the call to alxPlay() somewhere so you can reference it.
#3
04/28/2012 (9:01 am)
And I save this return by defining a variable?
#4
04/28/2012 (10:12 am)
Over in 3D land we would do something like this when we start a sound (level music):
$levelMusic = sfxPlay(Music_LevelX);
And then when it's time to stop the sound:
sfxStop($levelMusic);
This should work similarly in the 2D engines, just remember to use alxplay/alxstop instead of SFX.