Copy link to clipboard
Copied
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
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
...Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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);
Copy link to clipboard
Copied
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 });
}
Copy link to clipboard
Copied
The advantage of creating another variable is that you can run only one check for multiple statements.
Copy link to clipboard
Copied
...and checking for the presence of an event listener can be that one check.