我试图通过使用一个函数产生多个输出,简化输入,并用于另一个函数。是否有办法做到这一点?我必须为每个输入创建一个返回单个变量的函数吗?
--here is a snippet of what im trying to do (for a game)
--Result is the same for game environment and lua demo.
en = {
box ={x=1,y=2,w=3}
}
sw = {
box = {x=1,y=2,w=3}
}
function en.getbox()
return en.box.x,en.box.y,en.box.w,en.box.w
end
function sw.getbox()
return sw.box.x,sw.box.y,sw.box.w,sw.box.w
end
function sw.getvis()
return true
end
function CheckCollision(x1,y1,w1,h1, x2,y2,w2,h2)
return x1 < x2+w2 and
x2 < x1+w1 and
y1 < y2+h2 and
y2 < y1+h1
end
if CheckCollision(en.getbox(),sw.getbox()) == true then
if sw.getvis() == true then
en.alive = false
end
end
print(tostring(en.alive))
我希望敌人(en)死亡(en.alive = false),但是我得到了错误:input:25: attempt to perform arithmetic on a nil value (local 'w2')。
CheckCollision(en.getbox(),sw.getbox())
中,你不能像想象中那样随意地将一个元组放在另一个元组后面 - 第一个元组将被调整为一个值。这是Lua的非常奇怪的一面。 - Egor Skriptunoff