【问题标题】:Corona SDK event phaseCorona SDK 事件阶段
【发布时间】:2013-12-02 06:03:22
【问题描述】:
function drag(event)
if event.phase == "began" then
    firstx, firsty = event.x, event.y
end

if event.phase == "ended" then
    if firsty<event.y then
        local otherx, othery = event.x, event.y
        local x=(otherx-firstx)*4
        local y=(othery-firsty)*4
        shoot(x,y)
    end
end
end

event.phase 有问题?

程序将 firsty 与 event.y 进行比较时出错。

错误:尝试将 nil 与数字进行比较

奇怪的是,如果我只让程序加载几秒钟,它就可以完美运行..

【问题讨论】:

  • firsty 在这种情况下进行比较之前没有被设置为nil 以外的任何值,或者event.y 有时是nil 并覆盖firsty
  • hmm.. firsty 好像是nil,但是如果在event.phase开始时(当我触摸屏幕时)声明它怎么可能是nil
  • firsty 声明在哪里?它不会在程序的其他任何地方使用,是吗?

标签: lua coronasdk


【解决方案1】:

如果没有看到更多代码,我不确定firsty 发生了什么,但是这个怎么样?我认为这会更有效地解决您的问题。

function drag(event)
    if event.phase == "ended" then
        if event.yStart < event.y then
            local y = (event.y - event.yStart) * 4
            local x = (event.x - event.xStart) * 4
            shoot(x, y)
        end
    end
end

您可以使用x & yStart 来获取事件的初始坐标,而不是尝试自己保存。

【讨论】:

  • 谢谢!我不知道 event.yStart 存在。
  • 不客气。如果这解决了您的问题,请勾选答案旁边的绿色复选标记,让其他人知道。
猜你喜欢
  • 2013-04-01
  • 2012-02-26
  • 1970-01-01
  • 1970-01-01
  • 2014-10-19
  • 2014-01-07
  • 2014-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多