在corona sdk中手动结束或取消触摸阶段?

时间:2013-11-05 00:45:49

标签: lua touch corona

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

1 个答案:

答案 0 :(得分: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)

继续编码............