transition of datas in a grid

I’m trying to make a chess game but i’m just starting to learn corona, and i’m having some difficulty to move the pieces on the board

here is how I do it:

to make the board:

First i make the full board without any special pieces:

local function MakeBoard() local boardIndex = 0 for i = 1, tilesAcross do if not boardImg[i] then boardImg[i] = {}; end for j = 1, tilesDown do boardIndex = boardIndex + 1 boardImg[i][j] = spawn({image = images[1].getFile , w = tileWidth, h = tileHeight, objTable = spawnTableBoard}) -- board image boardImg[i][j].x = (j - 1) \* (tileWidth + tileSpacing) + leftSpacing boardImg[i][j].y = (i - 1) \* (tileHeight + tileSpacing) + topSpacing boardImg[i][j].testx = i boardImg[i][j].testy = j boardImg[i][j]boardIndex = boardIndex end end 

Then I put the piece on the board:

local pieceIndex = 0 for i = 1, tilesAcross do if not pieceImg[i] then pieceImg[i] = {}; end for j = 1, tilesDown do pieceIndex = pieceIndex + 1 boardIndex = boardIndex + 1 local imagesId = levels[boardIndex] if imagesId ~= 1 then pieceImg[i][j] = spawn({image = images[imagesId].getFile , w = tileWidth, h = tileHeight, objTable = spawnTablePiece}) pieceImg[i][j].x = (j - 1) \* (tileWidth + tileSpacing) + leftSpacing pieceImg[i][j].y = (i - 1) \* (tileHeight + tileSpacing) + topSpacing pieceImg[i][j].testx = i pieceImg[i][j].testy = j pieceImg[i][j].pieceIndex = pieceIndex pieceImg[i][j]:addEventListener("tap", select) end end end

In my select event, I add an event on the board where the piece can move(there is a lot of useless thing and useless stuff because I went in so many direction but this is what the important part look like:

local function select(event) if selectedpiece == event.target then for i = 1, #spawnTableBoard do spawnTableBoard[i]:setFillColor( 255,255 ) end selectedpiece = nil -- if you click on the same piece as before, we cancel the selection else selectedpiece = nil for i = 1, #spawnTableBoard do spawnTableBoard[i]:setFillColor( 255,255 ) spawnTableBoard[i]:removeEventListener( "tap", move ) end selectedpiece = event.target selectedpiece.\_x = event.target.testx -- the x value according to the grid selectedpiece.\_y = event.target.testy -- the y value according to the grid if piece[selectedpiece.\_x][selectedpiece.\_y].testx == levelImg[selectedpiece.\_x][selectedpiece.\_y].testx then -- temporary test to check which piece we clicked levelImg[levelImg[selectedpiece.\_x][selectedpiece.\_y].testx][2]:setFillColor( 255,0,0 ) -- also for testing purpose levelImg[1][2]:addEventListener("tap", move) end end return true end

and then comes my move event, and I guess that the problem come from there, i’m doing a transition to move the piece where the player clicked and it works without problem but if I click on the piece that i just move, i have a bug in my select event where the piece object is nil (the piece object is a global : local piece = {} ) and I can’t click on it again, but the other piece are still working, so basically i can move every piece only one time.

I guess it’s because when I do the transition the image move but the array is not updated correclty or something like that.

local function move(event) if selectedpiece then transition.to( selectedpiece, {time = 100, x = spawnTableBoard[event.target.index].x, y = spawnTableBoard[event.target.index].y} ) selectedpiece.playerIndex = event.target.index piece[selectedpiece.\_x][selectedpiece.\_y] = selectedpiece piece[selectedpiece.\_x][selectedpiece.\_y].testx = event.target.testx piece[selectedpiece.\_x][selectedpiece.\_y].testy = event.target.testy for i = 1, #spawnTableBoard do spawnTableBoard[i]:setFillColor( 255,255 ) spawnTableBoard[i]:removeEventListener( "tap", move ) spawnTableBoard[i]:removeEventListener( "tap", select ) end selectedpiece = nil end end

Any help would be greatly appreciate!

and sorry for the wall of code.

I am finding it a bit diffult to interpret your code, I think maybe I am just missing something : 

For example, in your ‘move’ function you seem to reference an array called piece which isn’t mentioned anywhere else.

However, at a quick look I am wondering if maybe the problem is caused by the where you turn selectedpiece into nil at the end of your move function? I am wondering if maybe the script thinks that the object itself has been removed?

I am finding it a bit diffult to interpret your code, I think maybe I am just missing something : 

For example, in your ‘move’ function you seem to reference an array called piece which isn’t mentioned anywhere else.

However, at a quick look I am wondering if maybe the problem is caused by the where you turn selectedpiece into nil at the end of your move function? I am wondering if maybe the script thinks that the object itself has been removed?