actionscript 3 - AS3 : EventListener won't be removed in an [IF] -



actionscript 3 - AS3 : EventListener won't be removed in an [IF] -

i have searched how pass arguments through eventlisteners, , used method without calling anonymous function remove eventlistener later.

the issue eventlistener removed if out if function, not if in if function. how ?

the code :

function dragship(m:mouseevent):void { var func:function = dispositionship(m.target); if (isdragging == false) { stage.addeventlistener(keyboardevent.key_down, func); m.target.startdrag(true); isdragging = true; } else { stage.removeeventlistener(keyboardevent.key_down, func); isdragging = false; placeship(m.target , mousex , mousey , m.target.rotation); } // if eventlistener set here, gets removed, not if set in else }

note : dispositionship()returns function.

edit : here next part of code :

function dispositionship(shiptarg):function { homecoming function(k:keyboardevent):void { rotateship(k,shiptarg); }; } function rotateship(k:keyboardevent,ship:object):void { if (k.keycode == 39) { ship.rotation += 90; } else if (k.keycode == 37) { ship.rotation -= 90; } }

moreover, if replace rotateship(k,shiptarg); simple trace, not work.

everytime call

function dispositionship(shiptarg):function { homecoming function(k:keyboardevent):void { rotateship(k,shiptarg); }; }

you're creating new anonymous object of type function calls rotateship(), when phone call stage.removeeventlistener(keyboardevent.key_down, func); func different object func passed addeventlistener(), doesn't match orginal listener , doesn't removed.

a improve way store current mouse target in fellow member var. ie:

var currentship:object; function dragship(m:mouseevent):void { if (isdragging == false) { stage.addeventlistener(keyboardevent.key_down, keypress); m.target.startdrag(true); isdragging = true; currentship = m.target; } else { stage.removeeventlistener(keyboardevent.key_down, keypress); isdragging = false; placeship(m.target , mousex , mousey , m.target.rotation); currentship = null; } } function keypress(k:keyboardevent):void { rotateship(k,currentship); }

actionscript-3 flash events actionscript listener

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -