Created
June 11, 2020 09:07
-
-
Save netgfx/fcdfbc38be20521e3526452eb541e135 to your computer and use it in GitHub Desktop.
An Audio Scene for Phaser 3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var AudioScene = new Phaser.Class({ | |
Extends: Phaser.Scene, | |
initialize: | |
function AudioScene() { | |
Phaser.Scene.call(this, { key: 'AudioScene' }); | |
}, | |
preload: function() { | |
//this.load.image('logo', 'assets/logo.png'); | |
}, | |
create: function() { | |
// init music // | |
var config = { | |
mute: false, | |
volume: 0.2, | |
rate: 1, | |
detune: 0, | |
seek: 0, | |
loop: true, | |
delay: 0 | |
}; | |
this.music = this.sound.add("mainTrack", config); | |
this.music.play(); | |
console.log("audio scene started!", this.sound.locked); | |
reg.sound = true; | |
let texture = this.sound.locked === true ? "soundOffButton" : "soundOnButton"; | |
this.soundBtn = this.add.sprite(0, 0, texture); | |
this.soundBtn.setScale(0.55); | |
this.soundBtn.setInteractive(); | |
console.log(this.sys.game.config.width, window.innerWidth, this.soundBtn.displayWidth, this.soundBtn.width); | |
this.soundBtn.x = this.sys.game.config.width - this.soundBtn.displayWidth - 10; | |
this.soundBtn.y = this.sys.game.config.height - this.soundBtn.displayHeight - 10; | |
this.soundBtn.on('pointerdown', function(pointer) { | |
console.log("clicked! ", this.sound.locked); | |
this.muteToggle(); | |
}, this); | |
this.sound.on('unlocked', function(soundManager) { | |
console.log("unlocked"); | |
let texture = this.sound.locked === true ? "soundOffButton" : "soundOnButton"; | |
this.soundBtn.setTexture(texture); | |
this.music.setMute(false); | |
this.music.play(); | |
}, this); | |
this.scene.bringToTop(); | |
}, | |
muteToggle: function() { | |
if (this.sound.locked === true) { | |
return false; | |
} | |
var mute = this.music.mute; | |
this.music.setMute(!mute); | |
let texture = mute === false ? "soundOffButton" : "soundOnButton"; | |
this.soundBtn.setTexture(texture); | |
console.log("mute is: ", mute); | |
if (mute === true) { | |
this.music.play(); | |
} else { | |
this.music.stop(); | |
} | |
}, | |
stopMusic: function() { | |
this.music.stop(); | |
}, | |
resumeMusic: function() { | |
this.music.resume(); | |
}, | |
update: function(time, delta) { | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment