I am trying to swap two tiles on one tile layer.
Expected result is - I do an operation on two map positions, and have them swapped - literally - they both exchange their sprites and properties.
I have tried the usual LUA way:
function TileLayer:swapTiles(pos1, pos2)
if (self.tileGrid) then
self.tileGrid[pos1.column][pos1.row], self.tileGrid[pos2.column][pos2.row] = self.tileGrid[pos2.column][pos2.row], self.tileGrid[pos1.column][pos1.row];
end
end
and then the way “of the fist” 
[code]function TileLayer:swapTiles(pos1, pos2)
if (self.tileGrid) then
tempTile1 = self.tileGrid[pos1.column][pos1.row];
tempTile2 = self.tileGrid[pos2.column][pos2.row];
self.tileGrid[pos1.column][pos1.row] = tempTile2;
self.tileGrid[pos2.column][pos2.row] = tempTile1;
end
end[/code]
Both ways failed ;(
Then I have thought of using the fresh and cool functions setTileAt and getTileAt… but it makes me to remember the internal GID of a tile in a tileset (or there is a fast and easy way, to get a GID of a tile?).
Graham - sorry I am fiddling with library interface, and place methods in lime-tileLayer.lua, but that was the fastest way to get to the internals (tileGrid) to “check, how I can get this done”
[import]uid: 10881 topic_id: 6483 reply_id: 306483[/import]
Hey,
The reason that it isn’t working is because the row and column values are actually only used in the initial setup to position things properly, from then on you would want to position the items via the tile.sprite.x and tile.sprite.y methods. If you bear with me a little bit I will write a function for you (or you can carry on with the new insight) and then post it for you and also add it to 2.8
Also, please feel free to mess with the Lime code as much as you want! That’s one of the reasons that I have left it completely open and unprotected because I want people to look at it, either to modify it for their own needs or to understand how it works and maybe learn something. Or more likely teach me something 
The swap tiles function sounds like a great addition to Lime so if you beat me to it feel free to post it here and I will add it in to the main code. [import]uid: 5833 topic_id: 6483 reply_id: 22426[/import]
Took a little longer than hoped but here are the new functions:
--- Swaps two tiles around.
-- @param tile1 The first tile.
-- @param tile2 The second tile.
-- @usage Originally created by Micha? Ko?odziejski - http://developer.anscamobile.com/forum/2011/02/12/how-swap-two-tiles-layer
function TileLayer:swapTiles(tile1, tile2)
-- Make sure we actually have tiles
if tile1 and tile2 then
-- Make sure the tiles have sprites attached
if tile1.sprite and tile2.sprite then
-- Swap the tile world positions
tile1.sprite.x, tile2.sprite.x = tile2.sprite.x, tile1.sprite.x
tile1.sprite.y, tile2.sprite.y = tile2.sprite.y, tile1.sprite.y
-- Swap the tiles in the tileGrid
self.tileGrid[tile1.column][tile1.row], self.tileGrid[tile2.column][tile2.row] = self.tileGrid[tile2.column][tile2.row], self.tileGrid[tile1.column][tile1.row]
-- Swap the tile grid positions
tile1.column, tile2.column = tile2.column, tile1.column
tile1.row, tile2.row = tile2.row, tile1.row
end
end
end
--- Swaps two tiles around based on their positions.
-- @param position1 The position of the first tile.
-- @param position2 The position of the second tile.
-- @usage Originally created by Micha? Ko?odziejski - http://developer.anscamobile.com/forum/2011/02/12/how-swap-two-tiles-layer
function TileLayer:swapTilesAtPositions(position1, position2)
-- Get both tiles
local tile1 = self:getTileAt(position1)
local tile2 = self:getTileAt(position2)
-- Swap the tiles
self:swapTiles(tile1, tile2)
end
You can then use them like this:
local onTap = function(event)
local screenPosition1 = { x = event.x, y = event.y }
local screenPosition2 = { x = event.x + 32, y = event.y }
local tileLayer = map1:getTileLayer("TileLayerName")
tileLayer:swapTilesAtPositions(screenPosition1, screenPosition2)
end
Hope that helps! [import]uid: 5833 topic_id: 6483 reply_id: 22444[/import]
You’re a great person, the code You have posted here works like a charm
Thanks a lot. [import]uid: 10881 topic_id: 6483 reply_id: 22602[/import]
You’re too kind
Glad you like it and it works! [import]uid: 5833 topic_id: 6483 reply_id: 22942[/import]