Basic event handling question

With Flash, I frequently do things like this to clean up block event handlers: someInteractiveObject.addEventListener(MouseEvent.CLICK, function(event:Event):void { event.target.removeEventListener(event.target, arguments.callee); // other s…

Viewing 1 to 5 (5 Total)
Basic event handling question

crayfellow

crayfellow
Total Posts: 204
Joined: November 28, 2011

With Flash, I frequently do things like this to clean up block event handlers:

someInteractiveObject.addEventListener(MouseEvent.CLICK, function(event:Event):void {
event.target.removeEventListener(event.target, arguments.callee);
// other stuff...
}

Now, I know there is __arguments__.callee in HaXe but it is only available when targeting Flash.

In cross-platform HaXe, how would I go about doing same while maintaining the convenience of the code block/closure? Meaning, I hope not to define the function separately to pass into the addEventListener just so I can reference it in removeEventListener. However, if this is the only way to be sure the listener gets removed, that is what we will do.

Any other tips for managing event handlers in cross-platform HaXe are welcome!

Tags:
Posted on December 06, 2011 at 11:14 AM

crayfellow

crayfellow
Total Posts: 204
Joined: November 28, 2011

Re: Basic event handling question

typo, of course the remove line is "event.target.removeEventListener(event.type, arguments.callee);"

Posted on December 06, 2011 at 11:15 AM

crayfellow

crayfellow
Total Posts: 204
Joined: November 28, 2011

Re: Basic event handling question

I'm finding along with this, a more specific issue with jeash even when the function can be specified by name.


private function handleSomething(event:Event):Void {
event.target.removeEventListener(event.type, handleSomething);
...
}

... does not appear to be removing the event listener. Even if I know the target is, say, an InteractiveObject, casting event.target to a new variable to animate the resulting object or something does not work.

Is there a more universal technique for cleaning up events that works in jeash as well as for cpp/neko/flash targets?

Posted on December 06, 2011 at 3:22 PM

singmajesty

singmajesty
Total Posts: 2147
Joined: August 25, 2011

Re: Basic event handling question

Hey, great picture! I saw your pictures earlier only by email, so I didn't spot it smiling

The way it works may depend a lot on your application. Sometimes I will call "event.target.mouseEnabled" and set it to false, so it will stop dispatching events. In general, however, I have something like this:

private function addObject (object:GameObject):Void {

objects.push (object);
object.addEventListener (MouseEvent.MOUSE_DOWN, GameObject_onMouseDown);
object.addEventListener (MouseEvent.MOUSE_UP, GameObject_onMouseUp);

}

private function removeObject (object:GameObject):Void {

objects.remove (object);
object.removeEventListener (MouseEvent.MOUSE_DOWN, GameObject_onMouseDown);
object.removeEventListener (MouseEvent.MOUSE_UP, GameObject_onMouseUp);

}


This is a simple example, but the basic idea is when I need to add an object with listeners, I push it through a function that gives me explicit calls to add them. When I am ready to remove the object, I pass it through a similar function which explicitly removes the listeners I had on the object.

In this way, instead of trying to remove listeners in the event handler, I can pass the object to something like the "removeObject" method above, which will handle it for me.

That might not be what will create the behavior you want in your application, but its the way I've always done it in Haxe and AS3, and it tends to work fine on all the targets I've ever tested

Posted on December 06, 2011 at 6:52 PM

crayfellow

crayfellow
Total Posts: 204
Joined: November 28, 2011

Re: Basic event handling question

That will work.

I was playing around with a design pattern like what I have in another larger application I want to eventually port to HaXe. In this case, the listener is in a controller which does not have a direct reference to the InteractiveObject and probably shouldn't. In this case, it is kinda nice to just kill the listener using event.target. The view it's talking to also does not retain a reference to the thing, but it could (and maybe should to make stuff like this work).

With that said, you make a good point that doing a "removeListeners" method where everything is cleaned out at once tends to be more certain. I guess I was just testing out a situation where (for instance) I only wanted the click listener cleared out, but other listeners might remain for a bit until no longer useful.

For what it's worth, jeash is the only target that seems to not like event.target.

Cheers,
Patrick

Posted on December 06, 2011 at 7:13 PM