Skip to main content
Known Participant
August 20, 2021
Answered

How to remove event listener of a play audio button.

  • August 20, 2021
  • 1 reply
  • 570 views

Hey guys!

I have a button on screen that I want to be able to tap and play an audio as many times as I want when on the screen. But after moving to the next frame I want to remove that previous audio buttons event listener. But I'm unable to do this.

This is my code:

var root = this;
 
root.stop();
 
root.btnPlay.addEventListener("click", playSound);
root.btnBack.addEventListener("click", goBack);
root.btnNext.addEventListener("click", goNext);
 
function playSound() {
root.btnPlay.removeEventListener("click", playSound);
createjs.Sound.play("letterA")
}
 
function goBack() {
root.btnBack.removeEventListener("click", goBack);
root.gotoAndStop();
}
 
function goNext() {
root.btnNext.removeEventListener("click", goNext);
root.gotoAndStop(1);
}


The code the way it is, it only lets me play the audio once. To be able to play it as much as I want I need to delete the removeEventListener but then the event will persist.

Is there a way to remove ALL event listeners when moving to the next frame? I tried doing so on the goNext function but then I couldn't navigate or play sound anymore.
This topic has been closed for replies.
Correct answer JoãoCésar17023019

Hi.

 

I've edited my answer because it was not what you want.

 

But now going to the point... The event listener for the sound is only re-added when the user revisits the frame in which the code is located. If you want to play the sound as many time as you want, don't remove the listener.

 

To remove the listener when the frame is left, place the remove event listener for the audio in the goBack and/or goNext functions.

 

Also, please keep in mind that Animate automatically creates a global playSound method when at least one sound in the Library has a linkage name assigned. So you should give your function another name. One more thing is that the gotoAndStop call in you goBack function doesn't have an argument.

 

I hope it helps.

 

Regards,

JC

1 reply

JoãoCésar17023019
Community Expert
JoãoCésar17023019Community ExpertCorrect answer
Community Expert
August 20, 2021

Hi.

 

I've edited my answer because it was not what you want.

 

But now going to the point... The event listener for the sound is only re-added when the user revisits the frame in which the code is located. If you want to play the sound as many time as you want, don't remove the listener.

 

To remove the listener when the frame is left, place the remove event listener for the audio in the goBack and/or goNext functions.

 

Also, please keep in mind that Animate automatically creates a global playSound method when at least one sound in the Library has a linkage name assigned. So you should give your function another name. One more thing is that the gotoAndStop call in you goBack function doesn't have an argument.

 

I hope it helps.

 

Regards,

JC