Search code examples
drag-and-dropcoronasdkscene

How can i move to right by dragging the scene to the left in Corona?


Well, i want to make levels to my game like in Angry Birds. So how can i move from left to right by dragging the scene? What should i use for this in Corona? Thanks.


Solution

  • Insert all the images on the screen you want to drag (presumably everything besides any GUI objects) into a group.

    From there write a function with a touch listener assigned to the group itself. It would look something like this, supposing you're app is iPhone landscape mode.

    local function constrainMap ()
    if localGroup.x < -480 then
        localGroup.x = -480
    elseif localGroup.x > 0 then
        localGroup.x = 0
    end
    end
    Runtime:addEventListener("enterFrame", constrainMap)
    
    local function moveMap (event)
    if event.phase == "began" then
        localX = localGroup.x
    elseif event.phase == "moved" then
        localGroup.x = localX + (event.x - event.xStart)
    end
    end
    localGroup:addEventListener("touch", moveMap)
    

    In the above case the localGroup contains all visual elements and the constrainMap function is used to prevent the user from scrolling the map off the screen.