Configure/auto-install eventHandlers in Tiled thru Lime...cool

Realize I asked a lot of Qs about some issues with Lime, so let me share a cool success.

You can configure/auto-install event-handlers by setting tile properties and using some naming conventions. For example:

For a tile that is a dynamic body and will collide, you can add a tile property in Tiled with a name of “eventHandler” and a value of “collision:boomHandler”.

Then in your main.lua you should add:

[lua]local eventHandlerT = {}
eventHandlerT[“boomHandler”] = function(e) if ( e.phase == “began” ) then print("!!!BOOM!!!", e.target) end end

local onEventHandlerProperty = function(property, type, object)
local k = string.find(property.value,":")
local ename,hname = string.sub(property.value,1,k-1),string.sub(property.value,k+1)
object.sprite:addEventListener( ename, eventHandlerT[hname] )
end

map:addPropertyListener(“eventHandler”, onEventHandlerProperty)[/lua]

What this does is that it will register the eventhandler at eventHandlerT[“boomHandler”] with the tile’s display object for “collision” events, for all tiles that have a property with a name of eventHandler and value of “collision:boomHandler”.

It relies on a naming conventions for the property name/value and on a mapping table that gives you the handler for a name.

The end result is that thru this recipe, you could register much of your event-handlers thru the editing of Tiled-properties - define much of your game behavior while you design the map and avatars and such.

That is way cool.

Enjoy, Frank.

[import]uid: 8093 topic_id: 6641 reply_id: 306641[/import]

Nice, thanks for sharing [import]uid: 8782 topic_id: 6641 reply_id: 23135[/import]

Hey Frank, that is really cool. If you ever feel like writing that up into a little tutorial I will gladly host it on the Lime site. A very nice find indeed! [import]uid: 5833 topic_id: 6641 reply_id: 23208[/import]