Hi,
I’m trying to develop a game based on a 2D grid layout and got inspired from this demo project but I’m having issues retrieving the grid position (e.g. 3, 4) of the click object to move it.
I’ve tried event.x and event.y but that returns the coordinates in pixel instead (e.g. 135.120093, 96.700010).
Any help would be appreciated. My code is (see at the end) :
local GRID\_WIDTH = 8 -- Number of cells in width (background image needs to cover right space) local GRID\_HEIGHT = 8 -- Number of cells in height (background image needs to cover right space) local CELL\_WIDTH = 40 -- Pixels per cell local CELL\_HEIGHT = 40 -- Pixels per cell -- -- Create a 2D array to hold our objects. local grid = {} for i = 1, GRID\_HEIGHT do grid[i] = {} end -- -- load the background image: local backgroundImage = display.newImageRect("table.png", 320, 320) -- Number of pixes occupied by image (needs to be GRID\_WIDTH \* CELL\_WIDTH) backgroundImage.x = display.contentCenterX -- Center image on x axis backgroundImage.y = display.contentCenterY -- Center image on y axis -- -- Calculate some values -- local gbOffsetX = backgroundImage.x - ( backgroundImage.width \* backgroundImage.anchorX ) local gbOffsetY = backgroundImage.y - ( backgroundImage.height \* backgroundImage.anchorY ) -- Create a group containing background image and objects local backgroundObjectsGroup = display.newGroup() -- Insert background into backgroundObjectsGroup backgroundObjectsGroup:insert( backgroundImage ) -- make the whole background and object group dragable with ponywolf plugin (backgroundObjectsGroup) local dragable = require "com.ponywolf.plugins.dragable" -- Create a dragable object --backgroundObjectsGroup = dragable.new(backgroundObjectsGroup) -- Associate the dragable object to backgroundObjectsGroup -- Using positions 1, 8 for X and Y, draw the object at the right place in the grid -- 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( "token\_" .. pieceType .. ".png", CELL\_WIDTH, CELL\_HEIGHT ) backgroundObjectsGroup:insert( piece ) -- Insert the object into backgroundObjectsGroup (to be dragable) -- -- record the pieces 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 movePiece(piece, xPos, yPos ) -- check to see if the position is occupied. You can do either: -- 1. "Capture the piece". This would involve removeing 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 -- -- 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 -- -- Generate a few objects -- -- -- a holding piece for moving the object -- local lastObject for i = 1, 10 do local xPos = math.random( 1, GRID\_WIDTH ) local yPos = math.random( 1, GRID\_HEIGHT ) local color = "red" if math.random(2) == 2 then color = "white" end grid[yPos][xPos] = spawnPiece(xPos, yPos, color, backgroundImage) lastObject = grid[yPos][xPos] end -- -- -- -- I'm trying to get the touch position here and call the MovePiece function -- So far I have 1,1 but that should be replaced by the position where the user ended the touch -- local function onObjectTouch( event ) print(event.x) print(event.y) if ( event.phase == "began" ) then display.getCurrentStage():setFocus( event.target ) event.target.isFocus = true elseif ( event.target.isFocus ) then if ( event.phase == "moved" ) then print( "moved phase" ) elseif ( event.phase == "ended" or event.phase == "cancelled" ) then movePiece( lastObject, 1, 1) display.getCurrentStage():setFocus( nil ) event.target.isFocus = false print (event.y) end end return true end backgroundImage:addEventListener( "touch", onObjectTouch ) --timer.performWithDelay(200, function() movePiece( lastObject, 1, 1); end, 1)
The movePiece functions works fine, my problem is passing the x and y coordinate to it (I can’t retrieve them from the user touch action.