Generic button-type object-listener handler that transforms a tile-object into a ui.newButton

Any tile can be easily used as a button thru the definition of properties in Tiled and by adding the attached snippet to your Lime-enabled code.

In Tiled, you have to define your tile-object of the type “UIButton”.
Furthermore, you have to specify the tile to use for the over-image thru the property “overButtonTile”, which accepts values of “tileSetName:localTileIndex”, like “arrow-buttons:4”, which points at the 5th tile of the arrow-buttons.png spritesheet file. If you discard the tileSetName and only specify a number, it will look for the tile with that index in de same spritesheet file as the tile-object’s tile.
Furthermore, button handlers are installed if they are specified by name thru the “buttonHandler” property.
Lastly, the “overButtonTile” and “buttonHandler” can either be specified as an object property with the tile-object, or as a tile property with the associated tile. The object property is looked at first, if nothing found then the tile property is looked for.

Enjoy, Frank.

[lua]-------------------------------------------------------------------------------

— Generic button-type object-listener handler that transforms a tile-object into a ui.newButton.
– Registered with map:addObjectListener(“UIButton”, onUIButtonType), it will transform any tile-object of a “UIButton” type into a ui:newButton. The handler will look for the (optional) property “overButtonTile” to find the tile to use for press-animation. Also, button handlers can be registered by name thru the “buttonHandler” property.
local onUIButtonType = function(object)
local map = object.map
local gid = tonumber(object.gid)
local oversprite
– find the over-button tile to use for the press animation
local tileNameID = object:getObjectTilePropertyValue(“overButtonTile”)
if(tileNameID)then – create sprite for over-button
local overgid, ltid = map:getGIDForTileNameID(tileNameID)
if(not overgid)then – assume overtile is local to default tile
local tileS,tileID = map:getTileSetFromGID( gid )
overgid = tileS.firstgid + ltid
end
oversprite = map:createSprite(overgid)
end
– find the button event handler to register
local buttonHandlerS = object:getObjectTilePropertyValue(“buttonHandler”)
local buttonHandler = buttonHandlerS and eventHandlerT[buttonHandlerS]
– create the button and register it as the object’s sprite.
object.sprite = ui.newButton{
default = object.sprite,
over = oversprite,
x = object.x,
y = object.y,
onPress = buttonHandler
}
– copy some other properties ?
end

map:addObjectListener(“UIButton”, onUIButtonType)[/lua]
[import]uid: 8093 topic_id: 6828 reply_id: 306828[/import]