Jump to content

how to remove listener


trinoo
 Share

Recommended Posts

Hello guys, a noob question here:

if I set up listeners like this, all works fine for me:

myobject.on("pointerdown", onPointerDown);

function onPointerDown(ev){
     	myobject.on("pointermove", onPointerMove);
     	myobject.on("pointerup", onPointerUp);     	
}

function onPointerMove(ev){
	//do some things
}

function onPointerUp(ev){
	myobject.off("pointermove", onPointerMove);
}

but if I set up things like this:

myobject.on("pointerdown", onPointerDown);

function onPointerDown(ev){
	var a = somevalue;
        var b = somevalue;
     	myobject.on("pointermove", onPointerMove.bind(this, a, b));//closure here to pass aditional parameters to callback
     	myobject.on("pointerup", onPointerUp);     	
}

function onPointerMove(param_a,param_b,ev){
	//do stuff with param_a,param_b and ev here
}

function onPointerUp(ev){
	myobject.off("pointermove", onPointerMove);//does not work
	myobject.off("pointermove", onPointerMove.bind(this, a, b));//also does not work
	myobject.removeAllListeners();//works, but this is not, what I need
}

If I make a closure callback, I'm not able to remove onPointerMove listener then. Can anybody explain me, what is going on there or what am I missing?

(new to Pixi and JavaScript in overal, so sorry if too stupid question...)

thanks

Link to comment
Share on other sites

Thanks for answer, but Im not sure, if I understand you right.

Its like a kind of dragging:

on-pointer-down (start drag) --> sets listeners for on-pointer-move (moving objects) and on-pointer-up (stop moving)--> when on-pointer-up, then  stop dragging/moving  - so I need to remove on-pointer-move listener right when on-pointer-up event occurs. How can I store it and use later?

And also, how its possible, that removeAllListeners() works? I was trying to find this method in Pixi sources to study it, but cannot find its definition anywhere...

Seems like I have to use standard approach without closure and pass those two needed arguments differently...but just interested in...

 

Link to comment
Share on other sites

using 'bind' creates a copy of the method (it's a totally different object!) so when you remove that listener after using the original method it can't work since you registered a copy of that method instead. So if you use 'bind' you need to keep a reference of that copy in order to remove it later.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...