local arr = {} local myGroup = display.newGroup() local function onTouch( event ) local t = event.target local phase = event.phase if "began" == phase then local parent = t.parent parent:insert( t ) display.getCurrentStage():setFocus( t ) t.isFocus = true t.x0 = event.x - t.x t.y0 = event.y - t.y elseif t.isFocus then if "moved" == phase then t.x = event.x - t.x0 t.y = event.y - t.y0 print("X = "..arr[3][3].x) print("Y = "..arr[3][3].y.."\n") elseif "ended" == phase or "cancelled" == phase then display.getCurrentStage():setFocus( nil ) t.isFocus = false end end end for i = 1 , 5 do arr[i] = {} for j = 1 ,5 do arr[i][j] = display.newRect(0,0,50,50) arr[i][j].x = i\*55 arr[i][j].y = j\*55 myGroup:insert(arr[i][j]) end end myGroup:addEventListener("touch",onTouch)
How to get the new x and new y when dragging myGroup ?