我有一个逐渐向下移动的对象,我希望每当该对象经过某个 Y 时就调用触摸函数。
所以我有一个 if 条件来检查 square.y 是否大于 150。如果该条件为真,我希望能够调用触摸函数。
我怎样才能做到这一点?
我尝试过这样做,但没有成功。
if (square.y > 150) then
function square:touch (event)
// do something
end
end
最佳答案
您定义了函数 square:touch
,但没有调用它。您需要使用 square:touch()
调用它:
if square.y > 150 then
function square:touch (event)
print("calling touch")
end
square:touch()
end
如果需要,您还可以在 if
语句之外定义函数,以使其更加清晰:
function square:touch (event)
print("calling touch")
end
if square.y > 150 then
square:touch()
end
关于lua - 如果对象传递 y 值则调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21948761/