PDA

View Full Version : Custom Events??


JLM
10-20-2008, 02:02 PM
Can't seem to get a handle on how I should be creating custom events.

Looking through various sources, I am wondering if something like this appropriate for flash 9.



package net.justinfront.events;

import flash.events.Event;


class CustomEvent extends Event
{

public static inline var CUSTOM_EVENT:String = "customevent";
public var data( default, null ):Dynamic;

public function new( type:String, ?bubbles:Null<Bool>, ?cancelable:Null<Bool>, ?data:Null<Dynamic> )
{
if (bubbles == null) bubbles = false;
if (cancelable == null) cancelable = false;
super(type, bubbles, cancelable);
this.data = data;
}

override public function clone():Event
{

return new CustomEvent(type, bubbles, cancelable, data);

}

override public function toString():String
{
//???
return formatToString( 'CustomEvent',
'type',
'bubbles',
'cancelable',
'eventPhase',
'data'
)

}

}

grumpytoad
10-21-2008, 07:44 PM
Heya Justin,

Your CustomEvent can be used on any EventDispatcher, either as a subscriber or a publisher, in order to trigger code in an abstracted way. Sprites, Bitmaps and all Displayobjects (even the stage) inherit from EventDispatcher, so you can attach your CustomEvent to make display objects more reactive.

For example:

import net.justinfront.events.CustomEvent;

class A extends flash.display.Sprite {
function trigger ( event : CustomEvent ) {
// ... triggered when CustomEvent is dispatched
}
static function main () {
var sprite = new A();

// subscribe a custom event to a callback function
sprite.addEventListener( CustomEvent.CUSTOM_EVENT, sprite.trigger );

// somewhere in your code, dispatch the event
sprite.dispatchEvent( new CustomEvent(CustomEvent.CUSTOM_EVENT) );
}
}



HTH.. javascript target is similar, but I'll leave that for someone else...

JLM
10-21-2008, 09:25 PM
I have been reluctant to try the code and

override public function toString():String
{
//???
return formatToString( 'CustomEvent',
'type',
'bubbles',
'cancelable',
'eventPhase',
'data'
)

}

was thrown in without real research so not sure its haxe, but you think the approach is valid, cool I will start trying it, its partly based on some of your code, it would be nice to keep stuff javascript, especially given what your doing!

I saw a lot of stuff on not using strings I guess it related to

public static inline var CUSTOM_EVENT:String = "customevent";

and enums?
and there was some interesting but rather deep threads around...
http://xinf.org/pipermail/xinf/2006-July/000157.html

slightly beyond me, but I should ignore that as I have and almost just reproduce the adobe approach?

Anyway at least I can move forward with my code thanks.

Cheers

;j

grumpytoad
10-22-2008, 07:00 AM
I have been reluctant to try the code and
was thrown in without real research so not sure its haxe, but you think the approach is valid, cool I will start trying it, its partly based on some of your code, it would be nice to keep stuff javascript, especially given what your doing!


It shouldn't be a problem to use the code - 'toString' is used in functions such as 'trace' or when casting the object as a string, so it is not entirely necessary.

Getting the code to work in javascript is still a little tricky, mainly because IE does not implement addEventListener.. but instead has attachEvent. It's usually easier to use a javascript library such as dojo's subscribe and publish API...


I saw a lot of stuff on not using strings I guess it related to

public static inline var CUSTOM_EVENT:String = "customevent";

and enums?
and there was some interesting but rather deep threads around...
http://xinf.org/pipermail/xinf/2006-July/000157.html

slightly beyond me, but I should ignore that as I have and almost just reproduce the adobe approach?


It's quite interesting, thanks for the link - they've implemented they're own pattern-based subscriber/publisher model, so you don't subscribe the wrong event type to the model. You wouldn't need to use it to just get something finished in flash, but it's probably worth looking at if you're doing a larger application, and/or you want to create an event model that works on more than one platform.