电晕停止物体被拖出屏幕

Goo*_*ner 3 lua coronasdk

我有多个可拖动的对象,可以在屏幕上移动.我想设置一个边界,以便它们不能被拖出屏幕.我真的找不到我想要做的事情.

The*_*gAl 5

有几种方法可以做到这一点.

您可以将一些静态物理实体设置为墙(位于屏幕边缘之外),并将动态物理实体附加到可拖动对象.如果您不希望多个可拖动对象相互碰撞,则需要设置自定义碰撞过滤器.

最简单的方法(假设您的对象不是物理对象)是将所有可拖动项放入表中.然后在运行时侦听器中,不断检查对象的x和y位置.例如

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)
Run Code Online (Sandbox Code Playgroud)

该循环假定图像的参考点位于中心,如果不是,则需要调整它.