Is it possible to prevent you from dragging an object - like say in the Drag Platform example - only to say the top half of the screen, but not the bottom half.
Example: I have a picture of a sun and moon. I only want the sun and moon images to be able to be dragged and dropped at the top half of the screen.
function new()
local localGroup = display.newGroup()
local rect = display.newRect(0,0,50,50)
local function moveRect(event)
local target = event.target
local phase = event.phase
if “began” == phase then
display.getCurrentStage():setFocus( target )
target.isFocus = true
– Store initial position
target.x0 = event.x - target.x
target.y0 = event.y - target.y
elseif “moved” == phase then
if event.y + target.height * 0.5 < display.contentHeight * 0.5 then
target.y = event.y - target.y0
end
target.x = event.x - target.x0
elseif “ended” == phase then
display.getCurrentStage():setFocus( nil )
target.isFocus = false
end
end
rect:addEventListener(“touch”,moveRect)
return localGroup
end[/lua] [import]uid: 12482 topic_id: 9363 reply_id: 34223[/import]
@hgvyas123,
I guess you want to use the target.isFocus in the moved phase. The reason why a boolean flag is set on begin is to avoid the rogue touches. With the code you have, touch outside the rect and then move your touch over the rect, you will find the rogue touches.
To avoid this,
use
[lua]if “began” == phase then
–set isFocus = true
else
if target.isFocus == true then
if “moved” == phase then
–Do whatever for moved
elseif “ended” == phase or “cancelled” == phase then
–Do whatever for Ended/Cancelled
end
end
end[/lua] [import]uid: 3826 topic_id: 9363 reply_id: 35624[/import]
yup exactly that’s the reason why i am using target.isFocus but just forgot to take that in moved and ended phase
updated code :–
[lua]module(…, package.seeall)
function new()
local localGroup = display.newGroup()
local rect = display.newRect(0,0,50,50)
local function moveRect(event)
local target = event.target
local phase = event.phase
if “began” == phase then
display.getCurrentStage():setFocus( target )
target.isFocus = true
– Store initial position
target.x0 = event.x - target.x
target.y0 = event.y - target.y
elseif “moved” == phase and target.isFocus == true then
if event.y + target.height * 0.5 < display.contentHeight * 0.5 then
target.y = event.y - target.y0
end
target.x = event.x - target.x0
elseif “ended” == phase and target.isFocus == true then
display.getCurrentStage():setFocus( nil )
target.isFocus = false
end
end
rect:addEventListener(“touch”,moveRect)
return localGroup
end[/lua]
I just added 1 if statement to the “drag platforms” project
if "moved" == phase then
t.x = event.x - t.x0
t.y = event.y - t.y0
if t.y \> 200 then --added these lines and it limited the drag to 200 pixels on Y axis
t.y = 200
end
end
[import]uid: 31262 topic_id: 9363 reply_id: 35911[/import]