Hi,
Just a quick one on dragging objects. I’m having a problem where by the user can touch just below the object and move it without actually being directly on the object.
I want them to be only able to touch the object on screen in order to move it.
Any help, greatly appreciated.
[lua] local background = display.newImage( “images/page63.png”, true )
local chessboard = display.newImageRect( “images/chessboard.jpg”, 500, 500 )
chessboard:setReferencePoint(display.TopLeftReferencePoint)
chessboard.x = 100
chessboard.y = 50
local chess1 = display.newImageRect( “images/chess1.png”, 300, 300 )
chess1:setReferencePoint(display.TopLeftReferencePoint)
chess1.x = 100
chess1.y = 50
local chess2 = display.newImageRect( “images/chess2.png”, 300, 300 )
chess2:setReferencePoint(display.TopLeftReferencePoint)
chess2.x = 150
chess2.y = 150
local chess3 = display.newImageRect( “images/chess3.png”, 300, 300 )
chess3:setReferencePoint(display.TopLeftReferencePoint)
chess3.x = 250
chess3.y = 250
---- functions
local function wrapit (pieces)
local obj = chess1 or chess2 or chess3
if obj.x < 10 then
obj.x = 10
elseif obj.x > 738 then
obj.x = 738
elseif obj.y < 10 then
obj.y = 10
elseif obj.y > 994 then
obj.y = 994
end
end
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
– Store initial position
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
wrapit(t)
elseif “ended” == phase or “cancelled” == phase then
display.getCurrentStage():setFocus( nil )
t.isFocus = false
–change(t)
end
end
return true
end
chess1:addEventListener( “touch”, onTouch )
chess2:addEventListener( “touch”, onTouch )
chess3:addEventListener( “touch”, onTouch )[/lua] [import]uid: 97656 topic_id: 17465 reply_id: 317465[/import]
[import]uid: 52491 topic_id: 17465 reply_id: 66393[/import]