Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Want to move a piece on a click but don't know how to designate the piece to be moved.
Started by Rodayan Dec 27 2018 09:32 PM

3 replies to this topic
move checkers name piece position
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

Rodayan

[GLOBAL: userInfoPane.html]
Rodayan
  • Observer

  • 9 posts
  • Corona SDK

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.


Edited by Rodayan, 28 December 2018 - 12:32 PM.


[TOPIC: post.html]
#2

XeduR @Spyric

[GLOBAL: userInfoPane.html]
XeduR @Spyric
  • Contributor

  • 619 posts
  • Corona SDK

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.



[TOPIC: post.html]
#3

davebollinger

[GLOBAL: userInfoPane.html]
davebollinger
  • Corona Geek

  • 1,309 posts
  • Enterprise

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



[TOPIC: post.html]
#4

Rodayan

[GLOBAL: userInfoPane.html]
Rodayan
  • Observer

  • 9 posts
  • Corona SDK

local function spawnPiece( xPos, yPos, pieceType )
    if pieceType ~= "red" and pieceType ~= "white" then
        print( "Invalid piece type", pieceType )
        return nil
    end
    if xPos < 1 or xPos > GRID_WIDTH or yPos < 1 or yPos > GRID_HEIGHT then
        print( "Position out of range:", xPos, yPos )
        return nil
    end

    local piece = display.newImageRect( mainGroup, "token_" .. pieceType .. ".png", CELL_WIDTH-5, CELL_HEIGHT-5 )
    -- record the piece's logical position on the board
    piece.xPos = xPos
    piece.yPos = yPos
    -- Position the piece
    piece.x = (xPos - 1) * CELL_WIDTH + (CELL_WIDTH * 0.5) + gbOffsetX
    piece.y = (yPos - 1) * CELL_HEIGHT + (CELL_HEIGHT * 0.5) + gbOffsetY

    return piece
end
--
local function startPieces()
    local countr = 1
    for i = 1, 8 do
        local xPos = countr
        local yPos = 1
        local color = "red"
        pieceName = "r"..countr
        grid[yPos][xPos] = spawnPiece(xPos, yPos, color, checkerBoard)
        print( pieceName )
        countr = countr + 1
        if i == 1 then
          redPiece1 = grid[yPos][xPos]
          print("redPiece1")
          print(redPiece1)
        end
    end        
    local countw = 1
    for i = 1, 8 do
        local xPos = countw
        local yPos = 8
        local color = "white"
        pieceName = "w"..countw
        print( pieceName )
        grid[yPos][xPos] = spawnPiece(xPos, yPos, color, checkerBoard)
        countw = countw + 1
        end
    return true
end


local function movePiece(piece, xPos, yPos )
    -- check to see if the position is occupied.  You can do either:
    -- 1. "Capture the piece".  This would involve removing the piece that is there before moving to the spot or
    -- 2. "Reject the move" because the spot is occupied.  For the purpose of this tutorial we will reject the move.
    --
    if xPos < 1 or xPos > GRID_WIDTH or yPos < 1 or yPos > GRID_HEIGHT then
        return false
    end
    if grid[yPos][xPos] == nil then -- got an empty spot, move into it
        -- get the screen x, y for where we are moving to
        local x = (xPos - 1) * CELL_WIDTH + (CELL_WIDTH * 0.5) + gbOffsetX
        local y = (yPos - 1) * CELL_HEIGHT + (CELL_HEIGHT * 0.5) + gbOffsetY
        -- save the old grid x, y
        local oldXPos = piece.xPos
        local oldYPos = piece.yPos
        -- Move the object in the table
        grid[yPos][xPos] = piece
        grid[yPos][xPos].xPos = xPos
        grid[yPos][xPos].yPos = yPos
        grid[oldYPos][oldXPos] = nil
        -- Now move the physical graphic
        transition.to(grid[yPos][xPos], { time = 500, x = x, y = y})
        return true
    
    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




[topic_controls]
[/topic_controls]

Also tagged with one or more of these keywords: move, checkers, name, piece, position