How to swap two tiles on a layer?

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:

1
2
3
4
5
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

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.

Took a little longer than hoped but here are the new functions:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
--- 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're a great person, the code You have posted here works like a charm :) Thanks a lot.

You're too kind :) Glad you like it and it works!

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