Reputation: 1371
I'm trying to do this
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 }
}
But Love2D say me attempt to perform arithmetic on global 'width' (a nil value)
. How can I fix it?
I already tried to replace width / 2
with ball.width / 2
but I got attempt to index global 'ball' (a nil value)
.
Upvotes: 1
Views: 63
Reputation: 474436
Remember that local some_name = expression
is equivalent to:
local some_name
some_name = expression
This allows some_name
to appear in expression
. In particular, it allows recursion with local functions. However, until expression
is actually finished being evaluated, the value of some_name
is still nil
.
So within your table initialization, ball
is a nil
value. There is no way to access members of a table while that table is being initialized. You can however do so afterwards:
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)
}
Upvotes: 5