Please find attached a number of convenience functions that I have used so far to make my life with Lime even more enjoyable.
I do have tile-objects as ui-buttons working… and the code uses these functions.
It seemed better to separate the submission - so here is the first part and the button code comes later this weekend if I can find time to clean it up.
[lua]-------------------------------------------------------------------------------
– Additional convenience functions to navigate the Lime-world tree
— Returns a list of all the objects in “map” that have name equal “aName”.
function Map:findObjectsByName(aName)
local mobjects = {}
for i,objectLayer in ipairs(self.objectLayers)do
for j,o in ipairs(objectLayer:getObjects(aName))do
table.insert(mobjects,o)
end
end
return mobjects
end
— Returns a list of all different tile-property values in the “map” for a property-name “aName”
function Map:findValuesByTilePropertyName(aName)
local values = {}
for i,layer in ipairs(self.tileLayers)do
for j,tile in ipairs(layer:getTilesWithProperty(aName))do
table.insert(values,tile:getPropertyValue(aName))
end
end
return values
end
— For a tileset-tile in the “map” and identified by its “gid”, instantiate an associated sprite and return its display-object.
function Map:createSprite(gid)
gid = tonumber(gid)
local tileS = self:getTileSetFromGID( gid )
return tileS and tileS:createSprite(gid)
end
— For a tileset-tile in the “map” and identified by its “gid”, return its property value for “name”.
function Map:getTilePropertyValueForGID(gid,name)
gid = tonumber(gid)
local tileS = self:getTileSetFromGID(gid)
local props = tileS:getPropertiesForTile(gid)
for i,p in ipairs(props)do
if(p:getName() == name)then return p:getValue()end
end
return nil
end
— For a tileset-tile in the “map” and identified by its “gid”, return its tileset name and local tile ID/index.
function Map:getTileNameIDForGID(gid)
gid = tonumber(gid)
local tileS = self:getTileSetFromGID(gid)
if tileS then return tileS.name, (gid - tileS.firstgid)
else return nil end
end
— Returns the GID and localTileID for a tile from a tileset “tileSetName” in this “map” with a local index of “localTileID” - alternatively, the tileSetName and localTileID can be specified in a single string “tileSetName:localTileID”.
function Map:getGIDForTileNameID(tileSetName,localTileID)
– see if only localTileID specified in first arg
if(type(tileSetName)==“number” or tonumber(tileSetName))then
return nil, tonumber(tileSetName)
end
– (maybe) only localTileID specified in first arg string
local name,lid = unpack(split(tileSetName,":"))
if(type(name) ~= “string”)then
return nil, tonumber(name)
end
– see if we can truly return gid, localTileID
lid = tonumber(lid) or tonumber(localTileID) or 0
local tileS = self:getTileSet(name)
if tileS then
return (tonumber(tileS.firstgid) + lid), lid
end
– fall-through…give up
return nil
end
— Returns the property value for a tile-object from either the object’s property, or if undefined, from the associated tile property.
function Object:getObjectTilePropertyValue(name)
if(name==nil)then return nil end
if(self[name]) then – local object property
return self[name]
else – return the associated tile property value (if it exists)
return self.map:getTilePropertyValueForGID(self.gid, name)
end
end
-------------------------------------------------------------------------------[/lua]
Enjoy, Frank.
[import]uid: 8093 topic_id: 6826 reply_id: 306826[/import]