when moving an image around the screen, how can i prevent it so it cannot be moved entirely off screen ?
i know how to track it in the ‘moved’ phase, but not how to limit the movement.
when moving an image around the screen, how can i prevent it so it cannot be moved entirely off screen ?
i know how to track it in the ‘moved’ phase, but not how to limit the movement.
Can you provide more information? What is moving? How are you moving it? Some code could also be useful?
local screenLeft = display.screenOriginX local screenRight = display.contentWidth - display.screenOriginX local screenTop = display.screenOriginY local screenBottom = display.contentHeight - display.screenOriginY local rect = display.newRect(0, 0, 100, 75) rect.x = display.contentCenterX rect.y = display.contentCenterY local function dragRect( event ) if event.phase == "began" then rect.xStart = rect.x - event.xStart rect.yStart = rect.y - event.yStart elseif event.phase == "moved" then rect.x = event.x + rect.xStart rect.y = event.y + rect.yStart if rect.x \> screenRight - rect.width\*0.5 then rect.x = screenRight - rect.width\*0.5 elseif rect.x \< screenLeft + rect.width\*0.5 then rect.x = screenLeft + rect.width\*0.5 end if rect.y \> screenBottom - rect.height\*0.5 then rect.y = screenBottom - rect.height\*0.5 elseif rect.y \< screenTop + rect.height\*0.5 then rect.y = screenTop + rect.height\*0.5 end end end rect:addEventListener("touch", dragRect)
Just try to drag rect around to see that it just won’t move outside screen.
thanks to all for the response.
Jedi, elegant coding.
Can you provide more information? What is moving? How are you moving it? Some code could also be useful?
local screenLeft = display.screenOriginX local screenRight = display.contentWidth - display.screenOriginX local screenTop = display.screenOriginY local screenBottom = display.contentHeight - display.screenOriginY local rect = display.newRect(0, 0, 100, 75) rect.x = display.contentCenterX rect.y = display.contentCenterY local function dragRect( event ) if event.phase == "began" then rect.xStart = rect.x - event.xStart rect.yStart = rect.y - event.yStart elseif event.phase == "moved" then rect.x = event.x + rect.xStart rect.y = event.y + rect.yStart if rect.x \> screenRight - rect.width\*0.5 then rect.x = screenRight - rect.width\*0.5 elseif rect.x \< screenLeft + rect.width\*0.5 then rect.x = screenLeft + rect.width\*0.5 end if rect.y \> screenBottom - rect.height\*0.5 then rect.y = screenBottom - rect.height\*0.5 elseif rect.y \< screenTop + rect.height\*0.5 then rect.y = screenTop + rect.height\*0.5 end end end rect:addEventListener("touch", dragRect)
Just try to drag rect around to see that it just won’t move outside screen.
thanks to all for the response.
Jedi, elegant coding.