3

画面上で移動できるドラッグ可能なオブジェクトが複数あります。画面外にドラッグできないように境界を設定したい。これを行うために探しているものが本当に見つかりません。

4

2 に答える 2

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)

そのループは、画像の基準点が中央にあると想定しています。そうでない場合は調整する必要があります。

于 2013-02-11T13:05:02.387 に答える