Try printing the values that produces. If you're using the default love.run (ie. you haven't defined it yourself), I imagine you're getting non-integer values because of the delta time. This then gives you table indexes that don't exist, thus giving you errors when you end up passing nil values to love.graphics.setBackgroundColor.
At the very least you probably need to use math.floor on the calculated value, though I figure you want a more fool proof solution.
For example, this a very simple naive approach where instead of using changeTimer you add another variable to track the table index:
Code: Select all
function love.load()
rainbowChangeRed = {237, 250, 247, 93, 81, 131, 241}
rainbowChangeGreen = {27, 162, 236, 187, 166, 118, 118}
rainbowChangeBlue = {81, 27, 47, 77, 220, 156, 166}
changeTimer = 0
-- tracker variable for the table index
changeIndex = 1
end
function love.update(dt)
changeTimer = changeTimer + dt
-- If accumulated deltatime passes a certain threshold update the changeIndex
-- Here it's 0.5 as an example
if changeTimer >= 0.5 then
changeTimer = 0
changeIndex = changeIndex + 1
-- Wrap around when changeIndex >= 8
if changeIndex > 7 then
changeIndex = 1
end
end
end
function love.draw()
-- Change background color based on changeIndex
love.graphics.setBackgroundColor(rainbowChangeRed[changeIndex], rainbowChangeGreen[changeIndex], rainbowChangeBlue[changeIndex])
end
As an aside, I recommend against using global variables, and instead try to use local ones instead.