explorers' club

explorations in dev, science, sci-fi, games, and other fun stuff!

EventDispatcherProxy & ProxyEvent

3 Comments

Preface

This is gonna be a ‘stream of conscientiousness’ type post. Hopefully it will be rather easy to follow and not long winded.

Might want to open this and follow along: example flex application

In the Beginning

One of my pet projects has been making an isometric (i.e. Diablo, Diablo II) game for Flash. Part of that effort was to create an Isometric framework where a developer could easily create and manipulate isometric primitive objects while not having to deal with complex math or all the logic behind Isometric game engines. Suffice it to say its still in development and in the umpteenth version. Thank you OCD.

Initially I had made the framework in AS2, extending MovieClip, blah blah blah. Then AS3 came out and resolved alot of issues I was having while creating new ones. IsoLib made the jump to AS3. Now the basic unit was extending Sprite. That was great in one respect but generated alot of funky API methods that seemed counterintuitive.

At a crossroads

Recently I discovered Sandy 3D (or for those who can read Chinese, 3D 3D). What a phenomenal framework. I am completely blown away by it. Aside from the crazy parameter names in the API, it works beautifully. I haven’t fully decided to abandon my IsoLib efforts but this discovery certainly makes the idea more enticing. But for now I will continue trudging onwards as I think Sandy 3D may be a bit overkill or what I am doing. But peering into the framework, I discovered some things I was doing wrongly.

Mainly my basic visual unit was extending Sprite rather than being a data-type class with a stored reference to its visual attribute. Why would one choose this route? Well firstly it would make the framework’s API quite a bit lighter. Example: Rather than setting an IsoCube’s isoX or isoHeight, one could simple say isoCube.x or isoCube.height. So it certainly would allow a more intuitive API without rewiring Sprite’s public attributes. But it creates a problem.

Now that my visual class is hidden behind the scenes, I could not make use of events triggered on the non-public container aka. the base unit’s visual attribute rendered to the screen. So if I wanted to assign a click event to my isoCube, normally I would have to say isoCube.container.addEventListener… and again folks, container is non-public. Plus that again is not intuitive. I’d rather have a simpler API where I could say, isoCube.addEventListener… and it would do the dirty work behind the scenes. The other reason is that I want to the event target that is handled to be isoCube, not isoCube’s container. Don’t ya love OOP?

Enter EventDispatcherProxy & ProxyEvent

Proxy means alot of different things to alot of different people. My browser proxy, my proxy settings, etc., etc. But looking at a dictionary definition it simply means something which acts on behalf of something else. So this is an excellent name. Basically I want a class that will act as an IEventDispatcher on behalf of a non-public attribute who is also an IEventDispatcher.

Disclaimer

Now keep in mind I made this little thing in a day, and it is completely plausible that I am overlooking some one-liner AS3 trick that does all this for you. If so, please let me know.

How it works

EventDispatcherProxy is an IEventDispatcher which contains a few other items in it. It has:

  • public var proxy:IEventDispatcher – this is normally the instance, but you could dispatch on behalf of someone else too
  • public var proxyTarget:IEventDispatcher – the target which you are acting on behalf of
  • a non-public hash table to keep track of all the things you are intercepting
  • a redispatcher method that intercepts proxy’d events and triggers the appropriate handler function with the event target being he EventDispatcherProxy not the proxyTarget

ProxyEvent extends a regular ol’ Event with two additional properties:

  • public var proxy:IEventDispatcher – this will reference the instance of the EventDispatcherProxy, in the case of the IsoCube example above, this would be the IsoCube itself
  • public var targetEvent:Event – this is a reference to the original event that got intercepted and proxy’d for.

Its all theory for now or is it…

I am posting a zipped project for you to check out. Please give me some feedback.

example flex application

Thanks,
J

Advertisements

3 thoughts on “EventDispatcherProxy & ProxyEvent

  1. hey J. if you like sandy try papervision i know there’s a big hype around it but it seems very good at this point and there’s a huge community out there to help you with PV3D (Papervision 3D)

  2. Hi Hugo,

    Thanks for the input. When I first started exploring the option of using a 3D framework I looked into Sandy 3D, Papervision and Away3D.

    I think I just kinda settled on Sandy because of the depth of detail some the examples went into and it just kinda stuck there.

    If I do decide to abandon the whole IsoLib development, I may revisit those libraries again to determine which is the best fit for me.

    J

  3. Interesting article. Thanks for the information.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s