[SOLVED] How can I change the music when a player entity is above a y-position?

I have no idea how to write specific code for changing anything, let alone just music. There’s a part in my project where a boss placeholder is and it has specific music I need to use. I need it to where if the player entity is above y=300, the current music stops and the new song plays. Any :thought_balloon:s?

https://playcanvas.com/project/1035819/overview/expungeds-unfair-platformer

Here’s kind of an example of what I want the code to do. If it needs anything else, let me know.

MusicSwap.prototype.initialize = function() {
    if (this.entity > (0, 300, 0));
        this.entity.sound.stop('song1');
        this.entity.sound.play('song2');
    if (this.entity < (0, 300, 0));
        this.entity.sound.play('song1');
        this.entity.sound.stop('song2');
};
1 Like

Hi @butternyke!

Thanks for sharing your code. Below how I think it need to be.

MusicSwap.prototype.update = function(dt) {
    if (this.entity.getPosition().y > 300) {
        if (!this.entity.sound.slot('song2').isPlaying) {
            this.entity.sound.stop('song1');
            this.entity.sound.play('song2');
        }
    }
    else {
        if (!this.entity.sound.slot('song1').isPlaying) {
            this.entity.sound.stop('song2');
            this.entity.sound.play('song1');
        }
    }
};
3 Likes

Thank you!

1 Like