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

How to remove event listener of a play audio button.

Community Beginner ,
Aug 20, 2021 Aug 20, 2021

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.
TOPICS
Code , Performance
582
Translate
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

correct answers 1 Correct answer

Community Expert , Aug 20, 2021 Aug 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 play

...
Translate
Community Expert ,
Aug 20, 2021 Aug 20, 2021
LATEST

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

Translate
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