Design Talks - Custom Runtime Events

@sporkfin I do this all the time… I allow players to drag a building from an overlay onto the main scene.

Now the asset in the overlay will, obviously, not exist when the overlay is removed so I create a new asset in the main scene and dispatch a fake touch event to it so the drag is continuous between overlay and the main scene.

I admit this was way too tricky to get working but it is rather slick and fluid now it is.

So in my main scene I do this

    if inDrag then       --start a drag event manually       local event = { name="touch", phase="began", target=myTarget, x=x, y=y}       buildingOnTouch( event )     end

I then broadcast the event and the corresponding building will respond.  x and y are the touch points from the overlay which are passed to the addNewBuilding() function in my main scene.

Play DC2 to see this in action (as there is far too much code to post here to make this seamless - and it requires major hacking to storyboard. Basically, I defer overlay unloading until after the drag event has completed).

All this discussion about custom events is really about the Observer design pattern that is used in MVC and major frameworks.

Corona’s original implementation of event *source* is limited to the Runtime and DisplayObject. With the new system.newEventDispatcher(), it is possible to dispatch events from any object although it is a bit limiting in terms of supported properties and methods (e.g. no event.source, event.target, and helper methods) when compared to:

https://github.com/daveyang/EventDispatcher

I wrote EventDispatcher about 4 years ago because there wasn’t a way to dispatch custom events from any object. If you think in terms of object-oriented design, dispatching custom events from the Runtime or DisplayObject doesn’t seem right if only your custom objects are supposed to be dealing with these event messaging.

Anyway, I don’t use custom events (or even OOP) in all my Corona apps. Keeping things simple if there is a better way is what I’d choose.

Dave

Very nice Dave!  :)  Corona’s method perhaps seems weird because it’s (effectively) a Publish-Subscribe pattern (where Runtime is the broker) – so it’s similar, but not quite same as observer.  Some pub-subs are “hybrid”, allowing you to target a specific recipient for example (or other features - categorical topics, etc), and thus can more closely mimic observer, but as it stands, there’s still a void for something more “direct” like your approach.  But I (too) rarely go to that much trouble when simpler approaches will do.

Thanks Dave, nice name!  :smiley:

I suppose you could consider Corona’s messaging mechanism as Pub/Sub if you implement an intermediate message filtering broker using Runtime:dispatchEvent() or other methods.

As for hybrid approaches, my EventDispatcher is also not a pure Observer implementation because it handles dispatching different methods and not just update().

Btw, I enjoyed watching you and your son programming together!

Cheers,

Dave

@sporkfin Yep, I use a nifty little passthrough to send mouse movements to the touch even on objects to do simple mouseOvers… The hard part with touches is getting the event to the right object. Usually that means a separate event listener for each object.