Gooner
Gooner

Reputation: 387

Corona stop object being dragged off screen

I have multiple draggable objects that can be moved around the screen. I want to set a boundary so they can not be dragged off screen. I can't really find what I'm looking for to do this.

Upvotes: 3

Views: 2482

Answers (2)

Robbie Armstrong
Robbie Armstrong

Reputation: 11

I'd also like to add for those that need their object to be more or much further off-screen you need to make the following adjustments to the code (And please keep in mind Gooner's switching the " > < " around comment) I also renamed some vars (minimumX/maximumX to tBandStartX/tBandEndX) so keep that in mind.

-- Create boundry for tBand

local tBandStartX = 529
local tBandEndX = -204

    local function tBandBoundry()
            --check if the left edge of the image has passed the left side of the screen
            if tBand.x  > tBandStartX then
                tBand.x = tBandStartX

            --check if the right edge of the image has passed the right side of the screen
            elseif tBand.x < tBandEndX then
                tBand.x = tBandEndX
            end
    end

    Runtime:addEventListener("enterFrame", tBandBoundry)

Thank you, TheBestBigAl, for helping me get to where I needed to be with this function!

-Robbie

Upvotes: 1

TheBestBigAl
TheBestBigAl

Reputation: 1230

There are a few ways to do this.

You could set up some static physics bodies as walls (just outside the edges of the screen), and the attach dynamic physics bodies to your draggable objects. You would need to set custom collision filters if you didn't want multiple draggable objects to collide with each other though.

The easiest way (assuming your objects are not already physics objects) is to put all of your draggable items into a table. Then in a Runtime listener, constantly check the x and y positions of your objects. E.g

object1 = display.newimage.....

local myObjects = {object1, object2, object3}

local minimumX = 0
local maximumX = display.contentWidth
local minimumY = 0
local maximumY = display.contentHeight

local function Update()

    for i = 1, #myObjects do

        --check if the left edge of the image has passed the left side of the screen
        if myObjects[i].x - (myObjects[i].width * 0.5) < minimumX then
            myObjects[i].x = minimumX

        --check if the right edge of the image has passed the right side of the screen
        elseif myObjects[i].y + (myObjects[i].width * 0.5) > maximumX then
            myObjects[i].x = maximumX

        --check if the top edge of the image has passed the top of the screen
        elseif myObjects[i].y - (myObjects[i].height * 0.5) < minimumY then
            myObjects[i].y = minimumY

        --check if the bottom edge of the image has passed the bottom of the screen
        elseif myObjects[i].x + (myObjects[i].height * 0.5) > maximumY then
            myObjects[i].y = maximumY
        end

    end
end

Runtime:addEventListener("enterFrame", Update)

That loop presumes that the reference point of your images is in the center, you would need to adjust it if they are not.

Upvotes: 5

Related Questions