Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • EspaƱol
      • FranƧais
      • PortuguĆŖs
  • ę—„ęœ¬čŖžć‚³ćƒŸćƒ„ćƒ‹ćƒ†ć‚£
  • ķ•œźµ­ ģ»¤ė®¤ė‹ˆķ‹°
0

[canvas] Using removeEventListener remove button states

Community Beginner ,
Aug 20, 2020 Aug 20, 2020

I've a button in a clip timeline, the button actions are  in the clip timelin. Since this clip is load several time, the click in the button is cumulative (if a load the clip 5 times, one single click in the button make 5 calls). So before binding the click i remove previous events attached to the button with 'removeEventListener' and then i declare the mouse event for the button.
In this way the button (with have graphical different status) loose the rollover status.
There's another way to do this without losing button status?

Tnx in advace

622
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, 2020 Aug 20, 2020

Hi.

 

How are you trying to remove the events?

 

Anyway, there are two approaches that you can use to check if an instance already has an event listener:

- Using the hasEventListener method;

- Or by creating a boolean flag in the frame you are adding the events and testing this flag everytime the frame is visited. For example:

if (!this.frame0Visited)
{
    this.yourButton.on("click", function(e){ // do something });
    this.frame0Visited = true;
}

 

In this way the click listener won't be adde

...
Translate
Community Expert ,
Aug 20, 2020 Aug 20, 2020

Hi.

 

How are you trying to remove the events?

 

Anyway, there are two approaches that you can use to check if an instance already has an event listener:

- Using the hasEventListener method;

- Or by creating a boolean flag in the frame you are adding the events and testing this flag everytime the frame is visited. For example:

if (!this.frame0Visited)
{
    this.yourButton.on("click", function(e){ // do something });
    this.frame0Visited = true;
}

 

In this way the click listener won't be added again.

 

Please let us know if this works for you.

 

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
Community Beginner ,
Aug 20, 2020 Aug 20, 2020

Yeah!
the variable script works like a charme!
Indeed a smart way to check the only-once.


I'm used to Flash and in its language there is no persistence of variables even when objects disappear from the Stage.
Again, thanks!
My best
Diz

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

there's no need to change what's working for you now but, in the future, and for others when trying to remove a canvas listener this is how to do it:

 

// this will fail:

this.btn.addEventListener("click",f.bind(this));

this.btn.removeEventListener("click", f.bind(this));

 

// this remove will work:

var fbind = f.bind(this);

this.btn.addEventListener("click",fbind);

this.btn.removeEventListener("click", fbind);

 

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
LEGEND ,
Aug 20, 2020 Aug 20, 2020

There's no reason to create another variable. This works just as well:

if (!this.yourButton.hasEventListener("click")) {
	this.yourButton.on("click", function(e){ // do something });
}
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
Community Expert ,
Aug 20, 2020 Aug 20, 2020

The advantage of creating another variable is that you can run only one check for multiple statements.

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
LEGEND ,
Aug 20, 2020 Aug 20, 2020
LATEST

...and checking for the presence of an event listener can be that one check.

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