Problem switching audio?

@samme : thank you! this helped alot! Although, it helps me to understand the code better when i use this.__. So I went ahead & did that, but for some reason when I call more than 1 __switchAudio ( ) it does not switch to the new sound. It just plays both at the same time.

All the background music / sound effects data is located in 'vars.js'

Here’s a direct link to the 'demo' : https://thundros.github.io/soundtest/

Here’s a direct link to vars.js : https://thundros.github.io/res/game/js/vars.js

Here’s a direct link to sfx.js : https://thundros.github.io/res/game/js/sfx.js

Here’s a direct link to 'main.js' : https://thundros.github.io/res/game/js/main.js

The code I’m looking at is in sfx.js

Line{s} : '75-96'

The code is being called in 'main.js'

Line{s} : '27-39'

Here’s the code I’m looking at in 'sfx.js' :

this.__switchAudio = function ( __objData ) {

	this.__objData = __objData;

	this.__scene = this.__objData.scene;
	this.__sound = this.__objData.sound;
	this.__record = this.__objData.record;
	this.__currentSound = this.__objData.currentSound;

	// `this` is the scene

	console.log ( this.__currentSound );

	if ( this.__currentSound ) {
		this.__currentSound.stop ( );
		this.__sound.remove ( this.__currentSound );
	}

	this.__currentSound = this.__sound.add ( this.__record.key, this.__record.config );
	this.__currentSound.play ( );

}

Here is the code I’m looking at in 'main.js' :

this.__create = function ( ) {

	this.sfx = new sfx ( );

	this.__currentSound = null;

	this.sfx.__switchAudio({ 
		scene : this, 
		sound : this.sound, 
		record : __music [ 0 ], 
		currentSound : this.__currentSound, 
	});

	this.sfx.__switchAudio({ 
		scene : this, 
		sound : this.sound, 
		record : __music [ 1 ], 
		currentSound : this.__currentSound, 
	});

}

Any help as always is GREATLY appreciated!

That is a working example, in 75 lines. Use it.

Don’t do that, you’ll just add bugs.

@samme : I don’t understand how that would add bugs.

You introduced a bug when you rewrote the function. In your version of the switchAudio function, you assign the passed in audio to this.__currentSound before you stop the value that this.__currentSound held from the previous call. So your code never stops the sound value that was passed in the first time the function was called. Look again at @samme’s example which stops the current sound first, then assigns the passed in sound to it so it can be referenced next time.

1 Like

so how can i get 'PlayAudio ( )' to still play the background music, but also play say a jump sound when jump is triggered, but only play it once per jump instead of playing 1 sound then overlapping another sound & overlapping another sound?

This is the 'AddAudio ( )' function & the 'PlayAudio ( )' function :

this.AddAudio = function ( __objData )  

{

	this.__objData = __objData;

	this.__snd = this.__objData.sound;
	this.__key = this.__objData.key;
	this.__config = this.__objData.config;

	if ( typeof ( this.__objData ) !== 'object' ) { return console.error ( 'ERROR :: { Please ensure you are using an `object` for `objectData` & try again } !' ); }

	this.__currentSound = this.__snd.add ( this.__key, this.__config );

	return this.__currentSound;

}

this.PlayAudio = function ( __objData )

{

	this.__objData = __objData;

	this.__sound = this.__objData.sound;
	this.__record = this.__objData.record;

	if ( typeof ( this.__objData ) !== 'object' ) { return console.error ( 'ERROR :: { Please ensure you are using an `object` for `objectData` & try again } !' ); }

	__currentSound = this.AddAudio ({
		sound : this.__sound, 
		key : this.__record.key, 
		config : this.__record.config, 
	});

	__currentSound.play ( );

}

This is how they’re called :


// *** INSIDE the 'PlayAudio ( )' function : 

__currentSound = this.AddAudio ({
	sound : this.__sound, 
	key : this.__record.key, 
	config : this.__record.config, 
});

// *** OUTSIDE the 'PlayAudio ( )' function : 

__m.PlayAudio ({
	sound : this.__sound, 
	record : __soundfx [ 0 ], 
});

Any help as always is most appreciated!

so i refactored my ENTIRE game to NOT include 'this.__' after having figured out where to put __isPlayerJumping & having it return correctly, I cannot get it to still play the background music, but when activating any sound effect also playing that ONCE. and then when jumping again, resetting the old jump sound so it doesn’t overlap with the old sound effect. NOTE : that the only reason the functions have 'this.' are because they are all contained within a central function that gets called as 'mylib = new functionNameHere ( );'

then the calls look like this :

'mylib.PlayAudio ({'
'currentSound : music [ 0 ]'
'});'

Here is the code I’m using :

this.AddAudio = function ( __objData )  

{

    __objData = __objData;

    __snd = __objData.sound;
    __key = __objData.key;
    __config = __objData.config;

    if ( typeof ( __objData ) !== 'object' ) { return console.error ( 'ERROR :: { Please ensure you are using an `object` for `objectData` & try again } !' ); }

    __currentSound = __snd.add ( __key, __config );

    return __currentSound;

}

this.PlayAudio = function ( __objData )

{

    __objData = __objData;

    __sound = __objData.sound;
    __record = __objData.record;

    if ( typeof ( __objData ) !== 'object' ) { return console.error ( 'ERROR :: { Please ensure you are using an `object` for `objectData` & try again } !' ); }

    __oldSound = __currentSound;

    if ( __oldSound.isPlaying === true ) {

        this.StopAudio ({
            currentSound : __currentSound, 
        });

        /*
        this.DestroyAudio ({
            sound : __sound, 
            currentSound : __currentSound, 
        });
        */

        __currentSound = this.AddAudio ({
            sound : __sound, 
            key : __record.key, 
            config : __record.config, 
        });

        __currentSound.play ( );

    }

}

Any help as always is GREATLY appreciated!

Thank You!

Here’s a video of what it is doing :