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

removeEventListener and bind

Community Beginner ,
Dec 18, 2017 Dec 18, 2017

Hi.

I have objects  on an animation, they are clips, but are used as button with  4 steps in the timeline ( in, over, rollout and selected).

the principle of the script is this simple one

this.couverture.addEventListener("mouseover", rollover.bind(this,'couverture'));

this.couverture.addEventListener("mouseout", rollout.bind(this, 'couverture'));

this.couverture.addEventListener("click", obj_bon.bind(this,'couverture'));

function rollover(obj) {

this[obj].gotoAndPlay('over');

}

function rollout(obj) {

this[obj].gotoAndPlay('rollout');

}

function obj_bon(obj) {

this[obj].removeEventListener("mouseout",true);

this[obj].gotoAndPlay('selected');

}

the remove EventListener don't work, when i click on the object, the clip read the timeline correctly from the "selected" label, but as the rollover is still active, moving the mouse out of the object send back the timeline to the "rollout" label, as i want  the object to become inactive. i have seen some solution involving the bounds, but did'nt find a way to adapt them to my script (  functions with parameters)

thanks

2.5K
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 , Dec 18, 2017 Dec 18, 2017

when using bind:

var f1=whateverF.bind(this);

this.whateverobj.addEventListener(event1,f1);

you can then use:

this.whateverobj.removeEventListener(event1,f1);

or if needed in another frame of the same timeline:

this.f1=whateverF.bind(this);

this...addEventListener(e1,this.f1);

.

.

.

this...removeEventListener(e1,this.f1);

Translate
Community Expert ,
Dec 18, 2017 Dec 18, 2017

when using bind:

var f1=whateverF.bind(this);

this.whateverobj.addEventListener(event1,f1);

you can then use:

this.whateverobj.removeEventListener(event1,f1);

or if needed in another frame of the same timeline:

this.f1=whateverF.bind(this);

this...addEventListener(e1,this.f1);

.

.

.

this...removeEventListener(e1,this.f1);

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 ,
Dec 18, 2017 Dec 18, 2017

Thanks kglad, it's perfect, I ended with this code :

var couv_hover=rollover.bind(this,'couverture');

this.couverture.addEventListener("mouseover",couv_hover);

var couv_out=rollout.bind(this,'couverture');

this.couverture.addEventListener("mouseout",couv_out);

this.couverture.addEventListener("click", obj_bon.bind(this,'couverture'));

function rollover(obj) {

this[obj].gotoAndPlay('over');

}

function rollout(obj) {

this[obj].gotoAndPlay('out');

}

function obj_bon(obj) {

    score++;

    this.score.score_dyn.text = score;   

    this[obj].gotoAndPlay('selected');

    this[obj].removeEventListener("mouseout",couv_out);

    this[obj].removeEventListener("mouseover",couv_hover);

}

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 ,
Dec 18, 2017 Dec 18, 2017

you're welcome.

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 ,
Dec 18, 2017 Dec 18, 2017

Maybe i could use your help a little more, as i have several objects who will use the same functions, i need to have consistancy in naming , but i don't succeed to concatenate a function name

function obj_bon(obj) {

var function_hover = [obj] +'_out';  // ex : couverture_out

this[obj].removeEventListener("mouseout", function_hover );

}

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 ,
Dec 18, 2017 Dec 18, 2017
LATEST

i don't know what that is, but if you're trying to generalize that code:

function addListenerF(obj,functionF,eventE,this_var){

obj.functionF=functionF.bind(this_var);

obj.addEventListener(eventE,obj.functionF);

}

function removeListenerF(obj,functionF,eventE,this_var){

obj.removeEventListener(eventE,obj[functionF]);

}

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