I am trying to making the background change colors randomly, here is the code that I am using, what am I doing wrong?
local background = display.setDefault( "background", math.random(1, 255), math.random(1, 255), math.random(1, 255) )
I am trying to making the background change colors randomly, here is the code that I am using, what am I doing wrong?
local background = display.setDefault( "background", math.random(1, 255), math.random(1, 255), math.random(1, 255) )
Colors are now in a range of 0…1, not 0…255. If you call math.random() without passing in any parameters you will get a number between 0 and 1 which you can use for the background.
Rob
Thank you Rob, but how do I make it so the background color also changes every 10 seconds?
Set up a timer that calls a function to change the colour.
local background function bgChange() background = display.setDefault( "background", math.random(0,1), math.random(0,1), math.random(0,1) ) end bgTimer = timer.performWithDelay( 10000, bgChange, -1)
^
it’s preferred to do it this way :
local background = display.setDefault( “background”, math.random(0,1), math.random(0,1), math.random(0,1) )
function bgChange()
background:setFillColor(math.random(0,1), math.random(0,1), math.random(0,1))
end
bgTimer = timer.performWithDelay( 10000, bgChange, -1)
this way you’ll avoid some memory issues that may occur on low devices.
Colors are now in a range of 0…1, not 0…255. If you call math.random() without passing in any parameters you will get a number between 0 and 1 which you can use for the background.
Rob
Thank you Rob, but how do I make it so the background color also changes every 10 seconds?
Set up a timer that calls a function to change the colour.
local background function bgChange() background = display.setDefault( "background", math.random(0,1), math.random(0,1), math.random(0,1) ) end bgTimer = timer.performWithDelay( 10000, bgChange, -1)
^
it’s preferred to do it this way :
local background = display.setDefault( “background”, math.random(0,1), math.random(0,1), math.random(0,1) )
function bgChange()
background:setFillColor(math.random(0,1), math.random(0,1), math.random(0,1))
end
bgTimer = timer.performWithDelay( 10000, bgChange, -1)
this way you’ll avoid some memory issues that may occur on low devices.