[SOLVED] How can I replace all music in a game when a key is pressed?

I want to add an Easter egg where if the 7 key is pressed, any song that is playing is replaced with another song for the rest of the game. This is an example of what I have for the script:

var Song6 = pc.createScript('song6');

Song6.prototype.update = function(dt) {
    if (this.app.keyboard.wasPressed(pc.KEY_7)) {
        if (!this.entity.sound.slot('song6').isPlaying) {
            this.entity.sound.stop('song1');
            this.entity.sound.stop('song2');
            this.entity.sound.stop('song3');
            this.entity.sound.stop('song4');
            this.entity.sound.stop('song5');
            this.entity.sound.play('song6');
        }
    }
};

Here’s the project, too:
https://playcanvas.com/project/1035819/overview/expungeds-unfair-platformer

All it does is overlap “song6” with whatever is playing, and I don’t have Overlap checked in the sound component.

With the code you shared I expect song 1 t/m 5 are stopped and song 6 starts to play. Is that not the case?

1 Like

None of the other songs stop, but song 6 plays anyway.

Okay, I will check your project when I’m home.

1 Like

Thank you.

Your MusicSwap script is conflicting with the code of the script you shared here.

Also I think the code of your MusicSwap script is in the wrong order.

Right now only the first statement can be reached.

1 Like

So I essentially have to reverse the order? And how are the codes conflicting?

Yes, I think so.

Because you start a sound in that script too.

1 Like

I tested reversing the order, but the last song played first instead of Cheating.
And should I put the secret song into the musicSwap script?

Sorry I was wrong, it’s correct this way.

I’m not sure what you try to achieve, but I think it’s easier to add it to your other statements indeed.

1 Like

I tried that, but…

What about this?

var MusicSwap = pc.createScript('musicSwap');

MusicSwap.prototype.update = function(dt) {
    if (this.app.keyboard.wasPressed(pc.KEY_7)) {
        if (!this.entity.sound.slot('kabunga').isPlaying) {
            this.entity.sound.stop('cheating');
            this.entity.sound.stop('unfairness');
            this.entity.sound.stop('opposition');
            this.entity.sound.stop('exploitation');
            this.entity.sound.stop('hellbreaker');
            this.entity.sound.play('kabunga');
        }
    }

    else if (this.entity.getPosition().y < 88) {
        if (!this.entity.sound.slot('cheating').isPlaying) {
            this.entity.sound.play('cheating');
        }
    }

    else if (this.entity.getPosition().y < 168){
        if (!this.entity.sound.slot('unfairness').isPlaying) {
            this.entity.sound.stop('cheating');
            this.entity.sound.play('unfairness');
        }
    }

    else if (this.entity.getPosition().y < 248)  {
        if (!this.entity.sound.slot('opposition').isPlaying) {
            this.entity.sound.stop('cheating');
            this.entity.sound.stop('unfairness');
            this.entity.sound.play('opposition');
        }
    }

    else if (this.entity.getPosition().y < 328) {
        if (!this.entity.sound.slot('exploitation').isPlaying) {
            this.entity.sound.stop('cheating');
            this.entity.sound.stop('unfairness');
            this.entity.sound.stop('opposition');
            this.entity.sound.play('exploitation');
        }
    }

    else if (this.entity.getPosition().y < 69420) {
        if (!this.entity.sound.slot('hellbreaker').isPlaying) {
            this.entity.sound.stop('cheating');
            this.entity.sound.stop('unfairness');
            this.entity.sound.stop('opposition');
            this.entity.sound.stop('exploitation');
            this.entity.sound.play('hellbreaker');
        }
    }
};
1 Like

It does the exact same thing.

Again, I don’t know what you try to achieve.

1 Like

I’m trying to make it so that when the 7 key is pressed, all other songs stop permanently and Kabunga is played until you refresh the game.

var MusicSwap = pc.createScript('musicSwap');

MusicSwap.prototype.update = function(dt) {
    if (this.app.keyboard.wasPressed(pc.KEY_7)) {
        if (!this.entity.sound.slot('kabunga').isPlaying) {
            this.entity.sound.stop('cheating');
            this.entity.sound.stop('unfairness');
            this.entity.sound.stop('opposition');
            this.entity.sound.stop('exploitation');
            this.entity.sound.stop('hellbreaker');
            this.entity.sound.play('kabunga');
        }
    }

    if (!this.entity.sound.slot('kabunga').isPlaying) {
        if (this.entity.getPosition().y < 88) {
            if (!this.entity.sound.slot('cheating').isPlaying) {
                this.entity.sound.play('cheating');
            }
        }
    
        else if (this.entity.getPosition().y < 168){
            if (!this.entity.sound.slot('unfairness').isPlaying) {
                this.entity.sound.stop('cheating');
                this.entity.sound.play('unfairness');
            }
        }
    
        else if (this.entity.getPosition().y < 248)  {
            if (!this.entity.sound.slot('opposition').isPlaying) {
                this.entity.sound.stop('cheating');
                this.entity.sound.stop('unfairness');
                this.entity.sound.play('opposition');
            }
        }
    
        else if (this.entity.getPosition().y < 328) {
            if (!this.entity.sound.slot('exploitation').isPlaying) {
                this.entity.sound.stop('cheating');
                this.entity.sound.stop('unfairness');
                this.entity.sound.stop('opposition');
                this.entity.sound.play('exploitation');
            }
        }
    
        else if (this.entity.getPosition().y < 69420) {
            if (!this.entity.sound.slot('hellbreaker').isPlaying) {
                this.entity.sound.stop('cheating');
                this.entity.sound.stop('unfairness');
                this.entity.sound.stop('opposition');
                this.entity.sound.stop('exploitation');
                this.entity.sound.play('hellbreaker');
            }
        }
    }
};
1 Like

With the way below you can stop all playing sounds at once.

this.entity.sound.stop();
1 Like

Once again, thank you for helping me!

1 Like