Copy link to clipboard
Copied
Hello guys. I got some troubled here. I just had a project with adobe animated. I just want to make some button for music. I got several bugs here. When I clicked into the music button it works as normal but when after i go to other menu and go back to menu when i clicked the music. The music played several music in the same time
Copy link to clipboard
Copied
My action scripts :
this.musicBtn.visible = false; // hilang untuk musicBtn
this.musicStop.visible = true;
this.musicBtn.addEventListener('click', playSound.bind(this));
this.musicStop.addEventListener('click', playSound.bind(this));
createjs.Sound.registerSound("Assets/Sound/MainMenu.mp3", "menu");
var isPlayed = false;
//var isPaused = false;
function playSound(){
if(!isPlayed){
window.sounds = createjs.Sound.play("menu");
this.musicBtn.visible = true;
this.musicStop.visible = false;
isPlayed = true;
}else if(isPlayed){
window.sounds.stop();
this.musicBtn.visible = false;
this.musicStop.visible = true;
isPlayed = false;
}
}
Copy link to clipboard
Copied
First, that isn't ActionScript, it's JavaScript.
Second, yes, because event listeners don't replace each other, they're cumulative. Every time you return to the frame with that code you're adding more and more of the same event listener.
Something like this should work, though not tested.
var _this = this;
this.musicBtn.visible = false; // hilang untuk musicBtn
this.musicStop.visible = true;
if (!this.musicBtn.hasEventListener("click")) {
this.musicBtn.addEventListener('click', playSound);
this.musicStop.addEventListener('click', playSound);
}
createjs.Sound.registerSound("Assets/Sound/MainMenu.mp3", "menu");
var isPlayed = false;
function playSound() {
isPlayed = !isPlayed;
_this.musicBtn.visible = isPlayed;
_this.musicStop.visible = !isPlayed;
if (isPlayed) {
window.sounds.stop();
}
else {
window.sounds = createjs.Sound.play("menu");
}
}
Bear in mind that you might get an error if the sound tries to play before it's loaded, since you aren't doing any load state checking.