trinoo Posted October 25, 2017 Share Posted October 25, 2017 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 Quote Link to comment Share on other sites More sharing options...
ivan.popelyshev Posted October 25, 2017 Share Posted October 25, 2017 You have to store that "onPointerMove.bind(this, a, b)" somewhere, and pass it to "off" later. Quote Link to comment Share on other sites More sharing options...
trinoo Posted October 25, 2017 Author Share Posted October 25, 2017 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... Quote Link to comment Share on other sites More sharing options...
ivan.popelyshev Posted October 25, 2017 Share Posted October 25, 2017 var stuff = onPointerMove.bind(this, a, b) obj.on('pointermove', stuff); obj.off('pointermove', stuff); Quote Link to comment Share on other sites More sharing options...
botmaster Posted October 25, 2017 Share Posted October 25, 2017 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. Quote Link to comment Share on other sites More sharing options...
trinoo Posted October 25, 2017 Author Share Posted October 25, 2017 Thanks guys, understand it now. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.