I am writing a simple drag and drop game and have no issues with the drag and drop. There is also no issue with grabbing the start location since it is event.target in the listener. However I cannot identify the end point of the drag action, i.e. drop point.
I have two arrays, one for the image to be moved and one for the underlying player board. Both arrays contain their respective identifiers (position in the array) and the corresponding x and y.
How can I get the target position (x,y). It will always be the cell next to the origin as you can only move one cell at a time in either direction. Up, down, right, left or diagonally from the starting point moving one cell.
Sorry, code looks like it did not format correctly.
--------------------------------------------------------------- -- dragDrop() -- listener to control the drag and drop functionality --------------------------------------------------------------- -- outOf Bounds: flag to show the player has moved past the -- board boundries, snaps image back to origin. -- cellNum: position of player board array matched to event.target -- zX, zY: moving x,y for drag -- minX, maxX, minY, maxY: boundry limits from playing piece build -- gridButtons: array form playing board --------------------------------------------------------------- function dragDrop( self,event ) outOfBounds = false cellNum = event.target.id if event.phase == "began" then display.getCurrentStage():setFocus( event.target ) --- -- store starting x,y --- self.markX = self.x self.markY = self.y elseif event.phase == "moved" then --- -- determine moved location --- print("id's: ", global.gridButtons[cellNum].num, global.gridButtons[cellNum].x,global.gridButtons[cellNum].y,event.target.id) local x = (event.x - event.xStart) + self.markX local y = (event.y - event.yStart) + self.markY zX = x zY = y elseif event.phase == "ended" or event.phase == "cancelled" then display.getCurrentStage():setFocus(nil) print("ended x, y: ",zX, zY, " markX, markY: ",math.floor(self.markX,1),math.floor(self.markY,1)) --- -- max/minX and max/minY are from the 1st and last listener cells -- if ((zX \< minX) or (zX \> maxX)) then --- -- out of X bounds, snap to origin --- outOfBounds = true --print("out of bound: ") x = self.markX y = self.markY elseif ((zY \< minY) or (zY \> maxY)) then --- -- out of Y bounds, snap to origin --- outOfBounds = true --print("out of bound: ") x = self.markX y = self.markY end --- -- validate bounds check for boundry snap to cell -- lock x/y for target cell for snap to cell --- print("bounds check val: ", outOfBounds,math.floor(zX,1), math.floor(zY,1) ) if (not outOfBounds) then self.x, self.y = zX, zY end elseif event.phase == "moved" then end return true end