Lua 正在尝试访问表中的表值
我正试着这么做Lua 正在尝试访问表中的表值,lua,Lua,我正试着这么做 local ball = { width = 20, height = 20, position = { x = (game.width / 2) - (width / 2), -- Place the ball at the center of the screen y = (game.height / 2) - (height / 2) }, v
local ball = {
width = 20,
height = 20,
position = {
x = (game.width / 2) - (width / 2), -- Place the ball at the center of the screen
y = (game.height / 2) - (height / 2)
},
velocity = 200,
color = { 255, 255, 255 }
}
但是Love2D告诉我尝试对全局“宽度”(零值)执行算术运算。我怎样才能修好它?
我已经尝试将width/2
替换为ball.width/2
,但我得到了尝试索引全局“ball”(零值)
请记住local some\u name=expression
相当于:
local some_name
some_name = expression
这允许一些\u名称出现在表达式中。特别是,它允许使用局部函数进行递归。但是,在实际完成计算表达式之前,some\u name
的值仍然是nil
因此,在表初始化中,ball
是一个nil
值。初始化表时,无法访问该表的成员。但是,您可以在之后执行此操作:
local ball = {
width = 20,
height = 20,
velocity = 200,
color = { 255, 255, 255 }
}
ball.position = {
x = (game.width / 2) - (ball.width / 2), -- Place the ball at the center of the screen
y = (game.height / 2) - (ball.height / 2)
}
请记住,local some\u name=expression
相当于:
local some_name
some_name = expression
这允许一些\u名称出现在表达式中。特别是,它允许使用局部函数进行递归。但是,在实际完成计算表达式之前,some\u name
的值仍然是nil
因此,在表初始化中,ball
是一个nil
值。初始化表时,无法访问该表的成员。但是,您可以在之后执行此操作:
local ball = {
width = 20,
height = 20,
velocity = 200,
color = { 255, 255, 255 }
}
ball.position = {
x = (game.width / 2) - (ball.width / 2), -- Place the ball at the center of the screen
y = (game.height / 2) - (ball.height / 2)
}