• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Sound with 1 button action script

New Here ,
Dec 04, 2019 Dec 04, 2019

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

TOPICS
ActionScript , Code , Error

Views

111

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 04, 2019 Dec 04, 2019

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;
}

}

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Dec 05, 2019 Dec 05, 2019

Copy link to clipboard

Copied

LATEST

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines