Want to move a piece on a click but don't know how to designate the piece to be moved.

I am having a circular reference problem.

I’m trying to play with this tutorial https://coronalabs.com/blog/2015/04/07/tutorial-working-with-a-grid-layout/ which of course the source file has been moved or deleted, so have cut and pasted the code and am trying to make it into a game.

I’ve managed to make it place red pieces along the top and white pieces along the bottom instead of randomly spawning a number of pieces.

Now I want to make it move a piece when I click on the piece.

Trouble is I can’t work out how to designate a clicked on piece as something that should be moved. Maybe it’s a problem with my event listener code (which I can’t get anything to work AT ALL). It’s like I could move it if I knew what its “name” was, but I need the name to be able to run a function to get the name!

Any suggestions?

Code is below.

The section I’m having trouble with is about lines 99 (where I’m trying to get the name of a touched object), line 106 (where I’m trying to touch an object) and the function from line 49 (where I’m trying to move it).

I also need some way of adding or subtracting 1 square to the ‘y’ so it moves down.

Edited to do what the two commenters have asked : cleaned up code and used <>  and added line numbers (using excel and Word because I didn’t know how else to do it).

Edited again because my attempts to use <> and line numbers made the post exceedingly longer. Instead, here’s a screenshot. I suppose I’m going to put that in wrong as well.

Apparently I’m not allowed to put in a PNG or JPG image.  This is getting stupid. What image types does the forum allow?  I guess you don’t get to see the code.

When you post code to the forums, you’ll want to use the code format tool <> to make your code easier to read and to include those line numbers so that others will have easier time trying to help you. Most people, myself included, will be scared off when we encounter long unformatted posts.

as per XeduR,… and get rid of all dead commented code, isolate the problem, simplest possible example, help us help you

you MAYBE just need to understand how to use event.target, but i won’t slog through unformatted code

local function spawnPiece( xPos, yPos, pieceType ) &nbsp;&nbsp; &nbsp;if pieceType ~= "red" and pieceType ~= "white" then &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;print( "Invalid piece type", pieceType ) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;return nil &nbsp;&nbsp; &nbsp;end &nbsp;&nbsp; &nbsp;if xPos \< 1 or xPos \> GRID\_WIDTH or yPos \< 1 or yPos \> GRID\_HEIGHT then &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;print( "Position out of range:", xPos, yPos ) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;return nil &nbsp;&nbsp; &nbsp;end &nbsp;&nbsp; &nbsp;local piece = display.newImageRect( mainGroup, "token\_" .. pieceType .. ".png", CELL\_WIDTH-5, CELL\_HEIGHT-5 ) &nbsp;&nbsp; &nbsp;-- record the piece's logical position on the board &nbsp;&nbsp; &nbsp;piece.xPos = xPos &nbsp;&nbsp; &nbsp;piece.yPos = yPos &nbsp;&nbsp; &nbsp;-- Position the piece &nbsp;&nbsp; &nbsp;piece.x = (xPos - 1) \* CELL\_WIDTH + (CELL\_WIDTH \* 0.5) + gbOffsetX &nbsp;&nbsp; &nbsp;piece.y = (yPos - 1) \* CELL\_HEIGHT + (CELL\_HEIGHT \* 0.5) + gbOffsetY &nbsp;&nbsp; &nbsp;return piece end -- local function startPieces() &nbsp;&nbsp; &nbsp;local countr = 1 &nbsp;&nbsp; &nbsp;for i = 1, 8 do &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;local xPos = countr &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;local yPos = 1 &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;local color = "red" &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;pieceName = "r"..countr &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;grid[yPos][xPos] = spawnPiece(xPos, yPos, color, checkerBoard) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;print( pieceName ) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;countr = countr + 1 &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if i == 1 then &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; redPiece1 = grid[yPos][xPos] &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; print("redPiece1") &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; print(redPiece1) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;end &nbsp;&nbsp; &nbsp;end&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;local countw = 1 &nbsp;&nbsp; &nbsp;for i = 1, 8 do &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;local xPos = countw &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;local yPos = 8 &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;local color = "white" &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;pieceName = "w"..countw &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;print( pieceName ) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;grid[yPos][xPos] = spawnPiece(xPos, yPos, color, checkerBoard) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;countw = countw + 1 &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;end &nbsp;&nbsp; &nbsp;return true end local function movePiece(piece, xPos, yPos ) &nbsp;&nbsp; &nbsp;-- check to see if the position is occupied.&nbsp; You can do either: &nbsp;&nbsp; &nbsp;-- 1. "Capture the piece".&nbsp; This would involve removing the piece that is there before moving to the spot or &nbsp;&nbsp; &nbsp;-- 2. "Reject the move" because the spot is occupied.&nbsp; For the purpose of this tutorial we will reject the move. &nbsp;&nbsp; &nbsp;-- &nbsp;&nbsp; &nbsp;if xPos \< 1 or xPos \> GRID\_WIDTH or yPos \< 1 or yPos \> GRID\_HEIGHT then &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;return false &nbsp;&nbsp; &nbsp;end &nbsp;&nbsp; &nbsp;if grid[yPos][xPos] == nil then -- got an empty spot, move into it &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;-- get the screen x, y for where we are moving to &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;local x = (xPos - 1) \* CELL\_WIDTH + (CELL\_WIDTH \* 0.5) + gbOffsetX &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;local y = (yPos - 1) \* CELL\_HEIGHT + (CELL\_HEIGHT \* 0.5) + gbOffsetY &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;-- save the old grid x, y &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;local oldXPos = piece.xPos &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;local oldYPos = piece.yPos &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;-- Move the object in the table &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;grid[yPos][xPos] = piece &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;grid[yPos][xPos].xPos = xPos &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;grid[yPos][xPos].yPos = yPos &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;grid[oldYPos][oldXPos] = nil &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;-- Now move the physical graphic &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;transition.to(grid[yPos][xPos], { time = 500, x = x, y = y}) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;return true &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;end end

Trying again to submit my code. Here’s the functions part of my code (Above).

Here’s the ways I’ve tried to call the piece to move, in the ‘did’ phase of ‘show event’. None of them have worked.

  Runtime:addEventListener( “touch”, movePiece )
  r1:addEventListener( “touch”, movePiece )
  piece:addEventListener( “tap”, movePiece )
  pieceName:addEventListener( “touch”, movePiece )
  pieceName.addEventListener(“touch”,movePiece(pieceName,2,2))

Is anyone able to shed some light on my problem? How do I tell a piece that I don’t know the name of, that I want it to move when I click it? Even if I know it’s called r1 I still don’t know how to tell it to move.

Thanks,

Rodayan

When you post code to the forums, you’ll want to use the code format tool <> to make your code easier to read and to include those line numbers so that others will have easier time trying to help you. Most people, myself included, will be scared off when we encounter long unformatted posts.

as per XeduR,… and get rid of all dead commented code, isolate the problem, simplest possible example, help us help you

you MAYBE just need to understand how to use event.target, but i won’t slog through unformatted code

local function spawnPiece( xPos, yPos, pieceType ) &nbsp;&nbsp; &nbsp;if pieceType ~= "red" and pieceType ~= "white" then &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;print( "Invalid piece type", pieceType ) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;return nil &nbsp;&nbsp; &nbsp;end &nbsp;&nbsp; &nbsp;if xPos \< 1 or xPos \> GRID\_WIDTH or yPos \< 1 or yPos \> GRID\_HEIGHT then &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;print( "Position out of range:", xPos, yPos ) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;return nil &nbsp;&nbsp; &nbsp;end &nbsp;&nbsp; &nbsp;local piece = display.newImageRect( mainGroup, "token\_" .. pieceType .. ".png", CELL\_WIDTH-5, CELL\_HEIGHT-5 ) &nbsp;&nbsp; &nbsp;-- record the piece's logical position on the board &nbsp;&nbsp; &nbsp;piece.xPos = xPos &nbsp;&nbsp; &nbsp;piece.yPos = yPos &nbsp;&nbsp; &nbsp;-- Position the piece &nbsp;&nbsp; &nbsp;piece.x = (xPos - 1) \* CELL\_WIDTH + (CELL\_WIDTH \* 0.5) + gbOffsetX &nbsp;&nbsp; &nbsp;piece.y = (yPos - 1) \* CELL\_HEIGHT + (CELL\_HEIGHT \* 0.5) + gbOffsetY &nbsp;&nbsp; &nbsp;return piece end -- local function startPieces() &nbsp;&nbsp; &nbsp;local countr = 1 &nbsp;&nbsp; &nbsp;for i = 1, 8 do &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;local xPos = countr &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;local yPos = 1 &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;local color = "red" &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;pieceName = "r"..countr &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;grid[yPos][xPos] = spawnPiece(xPos, yPos, color, checkerBoard) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;print( pieceName ) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;countr = countr + 1 &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if i == 1 then &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; redPiece1 = grid[yPos][xPos] &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; print("redPiece1") &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; print(redPiece1) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;end &nbsp;&nbsp; &nbsp;end&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;local countw = 1 &nbsp;&nbsp; &nbsp;for i = 1, 8 do &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;local xPos = countw &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;local yPos = 8 &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;local color = "white" &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;pieceName = "w"..countw &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;print( pieceName ) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;grid[yPos][xPos] = spawnPiece(xPos, yPos, color, checkerBoard) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;countw = countw + 1 &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;end &nbsp;&nbsp; &nbsp;return true end local function movePiece(piece, xPos, yPos ) &nbsp;&nbsp; &nbsp;-- check to see if the position is occupied.&nbsp; You can do either: &nbsp;&nbsp; &nbsp;-- 1. "Capture the piece".&nbsp; This would involve removing the piece that is there before moving to the spot or &nbsp;&nbsp; &nbsp;-- 2. "Reject the move" because the spot is occupied.&nbsp; For the purpose of this tutorial we will reject the move. &nbsp;&nbsp; &nbsp;-- &nbsp;&nbsp; &nbsp;if xPos \< 1 or xPos \> GRID\_WIDTH or yPos \< 1 or yPos \> GRID\_HEIGHT then &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;return false &nbsp;&nbsp; &nbsp;end &nbsp;&nbsp; &nbsp;if grid[yPos][xPos] == nil then -- got an empty spot, move into it &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;-- get the screen x, y for where we are moving to &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;local x = (xPos - 1) \* CELL\_WIDTH + (CELL\_WIDTH \* 0.5) + gbOffsetX &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;local y = (yPos - 1) \* CELL\_HEIGHT + (CELL\_HEIGHT \* 0.5) + gbOffsetY &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;-- save the old grid x, y &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;local oldXPos = piece.xPos &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;local oldYPos = piece.yPos &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;-- Move the object in the table &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;grid[yPos][xPos] = piece &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;grid[yPos][xPos].xPos = xPos &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;grid[yPos][xPos].yPos = yPos &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;grid[oldYPos][oldXPos] = nil &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;-- Now move the physical graphic &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;transition.to(grid[yPos][xPos], { time = 500, x = x, y = y}) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;return true &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;end end

Trying again to submit my code. Here’s the functions part of my code (Above).

Here’s the ways I’ve tried to call the piece to move, in the ‘did’ phase of ‘show event’. None of them have worked.

  Runtime:addEventListener( “touch”, movePiece )
  r1:addEventListener( “touch”, movePiece )
  piece:addEventListener( “tap”, movePiece )
  pieceName:addEventListener( “touch”, movePiece )
  pieceName.addEventListener(“touch”,movePiece(pieceName,2,2))

Is anyone able to shed some light on my problem? How do I tell a piece that I don’t know the name of, that I want it to move when I click it? Even if I know it’s called r1 I still don’t know how to tell it to move.

Thanks,

Rodayan