In my game, the user has to solve a jumbled word. The jumbled word is on the top row, where each letter is a separate tile object, which is draggable and has to be placed in a space on the bottom row. I want the user to be able to tap, which will cause the tile to jump to the first space on the bottom row. The user can also tap and hold to drag a tile if they have misplaced a letter.
Here is the code I am using at the moment:
local function onTouch( event )
local t = event.target
local phase = event.phase
if "began" == phase then
-- Make target the top-most object
local parent = t.parent
parent:insert( t )
display.getCurrentStage():setFocus( t )
t.isFocus = true
-- Store initial position
t.x0 = event.x - t.x
t.y0 = event.y - t.y
t.x, t.y = posX[index] + (sizeX/2), posY + (sizeY/2);
index = index + 1
elseif t.isFocus then
if "moved" == phase then
-- Make object move (we subtract t.x0,t.y0 so that moves are
-- relative to initial grab point, rather than object "snapping").
t.xScale = 1.7
t.yScale = 1.7
t.x = event.x - t.x0
t.y = event.y - t.y0
--t1 = t1..t.value
elseif "ended" == phase or "cancelled" == phase then
if t.container then
containers[t.container].isOccupied = false
answer[t.container] = " "
t.container = nil
--answer[t.container] = " "
end
for i = 1, #posX do
if not containers[i].isOccupied then
if (//correct condition) then
t.x, t.y = posX[i] + (sizeX/2), posY + (sizeY/2);
containers[i].isOccupied = true
t.container = i
--check to see if answer is correct
--get string from answer array
--print ( 'table: '..table.concat( answer))
s3 = table.concat(answer)
print(s3)
elseif (//another condition) then
-- t.x, t.y = posX2 + (sizeX/2), posY2 + (sizeY/2);
end
end
end
t.xScale = 1
t.yScale = 1
display.getCurrentStage():setFocus( nil )
t.isFocus = false
end
end
-- Important to return true. This tells the system that the event
-- should not be propagated to listeners of any objects underneath.
return true
end
I am trying to use the began phase as the tap and the rest acts as the dragging. It works to a certain degree on the simulator on Windows and on a Samsung s2, but testing on my Samsung s3 if the user taps the tile for more than a split second, the tile wont jump to the right position and becomes draggable in stead. Is there a better way to do this? Can I use two different listeners on the same object? [import]uid: 208811 topic_id: 36109 reply_id: 336109[/import]