在Lua中使用math.random函数来定位对象。

shape:setY(math.random(0, 450 - 50)):使用带有最小值和最大值的 math.random 函数是我理解的,但在 math.random 函数中使用算术运算符的含义是什么?例如在上面的代码行中,为什么是 450 - 50 而不是 400?有什么区别?

同样地,

self:setPosition(540, math.random(160) + 40)

点赞
用户88888888
用户88888888

没有区别。它等价于 shape:setY(math.random(0, 400))

第二行代码等价于以下代码:

self:setPosition(540, math.random(1, 160) + 40)

这显然旨在更清晰和/或更简单地编写以下代码:

self:setPosition(540, math.random(41, 200))
2014-05-03 11:53:11