Hi! I'm kinda new and I really need help to find out what's wrong with my coded, can someone help me please?
function love.load()
totalStars = 1000
stars = {}
for i = 0, totalStars, 1 do
stars [1] = {
r = love.math.random(0,255) / 255,
g = love.math.random(0,255) / 255,
b = love.math.random(0,255) / 255,
a = love.math.random(0,255) / 255,
x = love.math.random(0, love.graphics.getWidth()),
y = love.math.random(0, love.graphics.getHeight()),
radio = love.math.random(1,5)
}
end
end
function love.draw()
for i = 0, totalStars, 1 do
love.graphics.setColor(
stars.r,
stars.g,
stars.b,
stars.a
)
love.graphics.circle(
"fill",
stars.x,
stars.y,
stars.radio)
end
end
function love.update(dt)
stars[100].x = stars[100].x + 1
stars[100].y = stars[100].y + 1
if stars[101].x > love.graphics.getWidth() then
stars[101].x = 0
end
if stars[101].y > love.graphics.getHeight() then
stars[101].y = 0
end
end
And this is what appears to me when I run it:
Error
main.lua:39: attempt to index a nil value
Traceback
[love "callbacks.lua"]:228: in function 'handler'
main.lua:39: in function 'update'
[love "callbacks.lua"]:162: in function <[love "callbacks.lua"]:144>
[C]: in function 'xpcall'
REQUESTING HELP
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
-
- Party member
- Posts: 548
- Joined: Wed Oct 05, 2016 11:53 am
Re: REQUESTING HELP
Welcome to the forums.
The issue is here:
If you look at that for loop carefully, you only ever assign tables to the slot 1. As such, when you try to access slot 100 in your love.update, that slot is not defined, leading to a nil index error.
You will need to change the code above to this:
Please use code tags when posting your code.
The issue is here:
Code: Select all
for i = 0, totalStars, 1 do
stars [1] = { -- the "1" here is the problem
r = love.math.random(0,255) / 255,
g = love.math.random(0,255) / 255,
b = love.math.random(0,255) / 255,
a = love.math.random(0,255) / 255,
x = love.math.random(0, love.graphics.getWidth()),
y = love.math.random(0, love.graphics.getHeight()),
radio = love.math.random(1,5)
}
end
You will need to change the code above to this:
Code: Select all
for i = 0, totalStars, 1 do
stars [i] = { -- simply swap the "1" with "i"
-- ...rest of the code
Who is online
Users browsing this forum: No registered users and 2 guests