Drag object within bounds

I’m trying to constrain the drag movement between the screen boundaries but If you move fast enough it goes past the 50 and 200 limits

If you move fast enough it goes past the 50 and 200 limits local r = display.newRect(100,100,30,30) r:addEventListener("touch",function(event) if event.phase == "began" then display.getCurrentStage():setFocus(event.target) event.target.ox = event.target.x end if event.phase == "moved" and ((event.xStart \< event.x and event.target.x \< 200) or (event.xStart \> event.x and event.target.x \> 50)) then event.target.x = event.x - event.xStart + event.target.ox + 1 end if event.phase == "ended" or event.phase == "cancelled" then display.getCurrentStage():setFocus(nil) end end)

Anyone had this problem before? Is there a solution for this problem? 

Thank you

I would personally check the x, y at the end of the touch handler on the actual object and if it was out of bounds, move it back.  You will need to save the event.target.x and .y at the top before you start the IF and then at the bottom check to see if you’re out of bounds and if so, set the x, y back to what you saved at the top.

I actually just added this in a sample app i am doing to show Corona at a meetup.

Do the following

[lua]

xMin = 40; xMax = 280;

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, event.id )
       
        t.isFocus = true
        
        – Store initial touch position on the actual object - prevents jumping when touched
        t.xStart = event.x - t.x
        t.yStart = event.y - t.y
    elseif t.isFocus then
        if “moved” == phase then
            print(t.xStart)

                t.x = event.x - t.xStart
                if (t.x < xMin) then t.x = xMin end
                if (t.x > xMax) then t.x = xMax end                

            --t.y = event.y - t.yStart

        elseif “ended” == phase or “cancelled” == phase then
            display.getCurrentStage():setFocus( t, nil )
            t.isFocus = false
                 
        end
    end
    
    --This tells the system that the event
    – should not be propagated to listeners of any objects underneath.
    return true
end

[/lua]

good luck

Larry

Thank you guys, I will try this and let you know if solves the problem.

Regards,

Andrei

I would personally check the x, y at the end of the touch handler on the actual object and if it was out of bounds, move it back.  You will need to save the event.target.x and .y at the top before you start the IF and then at the bottom check to see if you’re out of bounds and if so, set the x, y back to what you saved at the top.

I actually just added this in a sample app i am doing to show Corona at a meetup.

Do the following

[lua]

xMin = 40; xMax = 280;

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, event.id )
       
        t.isFocus = true
        
        – Store initial touch position on the actual object - prevents jumping when touched
        t.xStart = event.x - t.x
        t.yStart = event.y - t.y
    elseif t.isFocus then
        if “moved” == phase then
            print(t.xStart)

                t.x = event.x - t.xStart
                if (t.x < xMin) then t.x = xMin end
                if (t.x > xMax) then t.x = xMax end                

            --t.y = event.y - t.yStart

        elseif “ended” == phase or “cancelled” == phase then
            display.getCurrentStage():setFocus( t, nil )
            t.isFocus = false
                 
        end
    end
    
    --This tells the system that the event
    – should not be propagated to listeners of any objects underneath.
    return true
end

[/lua]

good luck

Larry

Thank you guys, I will try this and let you know if solves the problem.

Regards,

Andrei

Did it solve the issue? Interested myself :slight_smile:

Did it solve the issue? Interested myself :slight_smile: