【问题标题】:Manually ending or canceling touch phase in corona sdk?在电晕sdk中手动结束或取消触摸阶段?
【发布时间】:2013-11-05 00:45:49
【问题描述】:

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

【问题讨论】:

  • 你可以通过添加标志以简单的方式做到这一点。
  • 我该怎么做呢?对不起,关于我的问题。我是个菜鸟。

标签: lua touch coronasdk


【解决方案1】:
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-05-04
  • 1970-01-01
  • 1970-01-01
  • 2013-03-17
  • 2014-12-04
  • 1970-01-01
  • 1970-01-01
  • 2022-08-18
  • 1970-01-01
相关资源
最近更新 更多