【发布时间】:2019-11-25 14:54:44
【问题描述】:
我想看看我是否可以通过使用一个产生多个输出的函数来简化输入,以便与另一个函数一起使用。有什么办法可以做到这一点?我是否必须创建一个函数来为每个输入返回单个变量?
--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),但我收到错误消息:输入:25:尝试对 nil 值执行算术运算(本地 'w2')
【问题讨论】:
-
您不能在
CheckCollision(en.getbox(),sw.getbox())中随意放置一个元组 - 第一个元组将被调整为一个值。 Lua 非常奇怪的一面。