2

是否可以手动取消或结束对象的触摸阶段?我基本上想让用户无法拖动对象,除非他们将手指从屏幕上移开并再次开始拖动它。这可能吗?

4

1 回答 1

2
local isDragAllowed = 0  -- create a flag or a variable

local bg = display.newRect(0,0,display.contentWidth,display.contentHeight) -- background

local myObject = display.newImageRect("Icon.png", 50, 50); -- your object
myObject.x = 160
myObject.y = 240

local function touchHandler(event)
    if(event.phase=="began")then
        isDragAllowed = 1
    elseif(event.phase=="moved" and isDragAllowed==1)then
        -- object will be moved only if the flag is true or 1
        myObject.x = event.x
        myObject.y = event.y
    else
        isDragAllowed = 0 -- resetting the flag on touch end
    end
    return true;
end
myObject:addEventListener("touch",touchHandler)

local function bgTouchHandler(event)
    print(event.phase)
    isDragAllowed = 0 -- resetting the flag if drag/touch happens on background
    return true;
end
bg:addEventListener("touch",bgTouchHandler)

继续编码…………

于 2013-11-05T04:54:22.810 回答