Skip to main content
Participating Frequently
December 18, 2017
Answered

removeEventListener and bind

  • December 18, 2017
  • 1 reply
  • 2629 views

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

    This topic has been closed for replies.
    Correct answer kglad

    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);

    1 reply

    kglad
    Community Expert
    kgladCommunity ExpertCorrect answer
    Community Expert
    December 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);

    Gico62Author
    Participating Frequently
    December 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);

    }

    kglad
    Community Expert
    Community Expert
    December 18, 2017

    you're welcome.