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:

1
2
3
4
5
6
7
8
9
10
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)

Nice, thanks for sharing

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!

views:1433 update:2011/10/13 16:39:51
corona forums © 2003-2011