I needed a setTile function in Lime in order to open and close doors on my map, and thought I’d put the function up here for anyone who wants it.
The function doesn’t take into account physics properties, and doesn’t throw property notifications if the new tile contains them. It wouldn’t be difficult to add either to the function though. I’m just not using them right now, so I didn’t.
To use it just put this function in your main.lua, or wherever, and the call it with the layer to modify, the gid to set the tile to and the coordinates of the tile to change.
Here’s an example that will set the tile at 5,10 on layer “Overlay” to the second tile in your tile sheet:
[lua]local layer = map:getTileLayer(“Overlay”)
tile = layer:getTileAt({row=tile.row-1, column=tile.column})
setTile(tile.layer, 2, 5, 10)[/lua]
And, here’s the function:
[lua]function setTile(layer, gid, tx, ty)
local index = layer.map.width*(ty-1)+tx
local oldTile = layer.tiles[index]
if oldTile and oldTile.sprite then layer.group:remove(oldTile.sprite) end
local tile = Tile:newFromGid(gid, layer.map, layer)
local data = {}
data[“Attributes”] = {}
data[“Attributes”].gid = gid
data[“Attributes”].gid = tonumber(data[“Attributes”].gid)
tile = Tile:new(data, layer.map, layer)
layer.tiles[index] = tile
tile:create(index)
if(tile) then
if(tile.gid ~= 0) then
if(not layer.tileGrid) then
layer.tileGrid = {}
end
if(not layer.tileGrid[tile.column]) then
layer.tileGrid[tile.column] = {}
end
if(not layer.tileGrid[tile.column][tile.row]) then
layer.tileGrid[tile.column][tile.row] = {}
end
layer.tileGrid[tile.column][tile.row] = tile
end
end
if(tile.sprite) then
if(tile.tileSet) then
local sprite = tile.sprite
sprite.alpha = tile.alpha or 1
sprite.isVisible = utils.stringToBool(tile.isVisible) or true
sprite.isHitTestable = utils.stringToBool(tile.isHitTestable) or true
sprite.x = (tile.x or sprite.x) + (tile.xOffset or 0)
sprite.y = tile.y or sprite.y + (tile.yOffset or 0)
sprite.xOrigin = tile.xOrigin or sprite.xOrigin
sprite.yOrigin = tile.yOrigin or sprite.yOrigin
sprite.xReference = tile.xReference or sprite.xReference
sprite.yReference = tile.yReference or sprite.yReference
sprite.xScale = tile.xScale or 1
sprite.yScale = tile.yScale or 1
sprite.rotation = tile.rotation or 0
if tile.xTileOffset then
sprite.x = sprite.x + (tile.xTileOffset * sprite.width)
end
if tile.yTileOffset then
sprite.y = sprite.y + (tile.yTileOffset * sprite.height)
end
layer.group:insert(tile.sprite)
end
end
end[/lua]
Enjoy! [import]uid: 3401 topic_id: 5904 reply_id: 305904[/import]