Greetings.
I am trying to make a set of 3 images I have, randomly appear as my background image. So every 3 seconds there wil be a different background. Can anyone suggest how this is done? Thank you.
Greetings.
I am trying to make a set of 3 images I have, randomly appear as my background image. So every 3 seconds there wil be a different background. Can anyone suggest how this is done? Thank you.
[lua]
local names = {‘img1.png’, ‘img2.png’, ‘img3.png’}
local background = nil
math.randomize(os.time())
local function ChangeBackground()
if background then
background:removeSelf()
background = nil
end
local imageName = names[math.random(1, #names)]
background = display.newImage(imageName)
end
ChangeBackground()
timer.performWithDelay(3000, ChangeBackground, -1)
[/lua]
WOW! I didn’t expect all that…lol…Thanks a lot!
I’m gonna test it out right now! Much appreciated, kind sir/mam!
Teen male 
I was writing from memory on phone so check all math functions if naming is correct.
Okay, I just got home to test this. I got an error so I changed some things and still have an error. The hardest thing for me is understanding the debug panel…Anyway, here is what I’m using -
local bgNames = {‘images/bg1.png’, ‘images/bg2.png’, ‘images/bg3.png’}
local background = bgNames
math.random (os.time())
– Local Function
local function ChangeBackground()
if background then
background:removeSelf()
background = nil
end
local imageBGName = bgNames[math.random(1, #bgNames)]
background = display.newImage(imageBGName)
end
ChangeBackground()
timer.performWithDelay(3000, ChangeBackground, -1)
Above is slightly modified code that you gave me earlier. I’m just not sure why it’s posing an error. It “looks” okay to me, but my IDE throws me an error.
The error I am getting from the code is -
stack traceback:
?: in function <?:218>
[C]: in function ‘removeSelf’
…ns(Builds)/MyFile(default)/MyFile/main.lua:25: in function ‘ChangeBackground’
…ns(Builds)/MyFile(default)/MyFile/main.lua:31: in main chunk
My chosen IDE is LUA Glider ( of course! :) ).
It would be nice to find a learning course on how to read programming errors, I dont think that exists though…Anyway, any help is much appreciated. Thanks again.
math.randomseed(os.time()) instead math.random (os.time()) - you didn’t check 
local background = bgNames
No! why?! it was placeholder for image, you assigned table there and there is removeSelf() trying to do something on this table and crushes (because it’s not display object)
local names = {'img1.png', 'img2.png', 'img3.png'} local background = nil math.randomseed(os.time()) local function ChangeBackground() if background then background:removeSelf() background = nil end local imageName = names[math.random(1, #names)] background = display.newImage(imageName) end ChangeBackground() timer.performWithDelay(3000, ChangeBackground, -1)
Please, put your code inside code tags for readabiltiy
I see. Well thanks for the update, it does work, now.
I’m going to study this post as well as head over to the blogs. I think I need to do a little catching up on my “lua know-how”. It’s hard to study and keep up with corona sdk since it updates so frequently…Verses my real life work which takes a good 90% of my time everyday. Well, anyhow, I appreciate your response very much. I’m happy to see it works. Thanks again. B)
what is printed on console is so called stack trace - it shows you last code executions leading to error. From last one to few executions before. I will try to break it. First code, comment underneath.
stack traceback:
We begin listing code execution history leading to crush
?: in function \<?:218\>
It shows that error is somewhere at line 128 but corona wasn’t able to trace what failed
[C]: in function 'removeSelf'
We go back in history to check what lead to error at line 128. We see that code executed before it was removeSelf - so it last known code part which failed. However, we do not know why it crused because there was “?” in line above which tells us corona wasn’t able to track it in such way that it can show us. Let’s go back to see what called removeSelf
...ns(Builds)/MyFile(default)/MyFile/main.lua:25: in function 'ChangeBackground'
So what called removeSelf was function ChangeBackground begining at line 25 in file main.lua
...ns(Builds)/MyFile(default)/MyFile/main.lua:31: in main chunk
it just say’s that what was before was general code execution of app.
[lua]
local names = {‘img1.png’, ‘img2.png’, ‘img3.png’}
local background = nil
math.randomize(os.time())
local function ChangeBackground()
if background then
background:removeSelf()
background = nil
end
local imageName = names[math.random(1, #names)]
background = display.newImage(imageName)
end
ChangeBackground()
timer.performWithDelay(3000, ChangeBackground, -1)
[/lua]
WOW! I didn’t expect all that…lol…Thanks a lot!
I’m gonna test it out right now! Much appreciated, kind sir/mam!
Teen male 
I was writing from memory on phone so check all math functions if naming is correct.
Okay, I just got home to test this. I got an error so I changed some things and still have an error. The hardest thing for me is understanding the debug panel…Anyway, here is what I’m using -
local bgNames = {‘images/bg1.png’, ‘images/bg2.png’, ‘images/bg3.png’}
local background = bgNames
math.random (os.time())
– Local Function
local function ChangeBackground()
if background then
background:removeSelf()
background = nil
end
local imageBGName = bgNames[math.random(1, #bgNames)]
background = display.newImage(imageBGName)
end
ChangeBackground()
timer.performWithDelay(3000, ChangeBackground, -1)
Above is slightly modified code that you gave me earlier. I’m just not sure why it’s posing an error. It “looks” okay to me, but my IDE throws me an error.
The error I am getting from the code is -
stack traceback:
?: in function <?:218>
[C]: in function ‘removeSelf’
…ns(Builds)/MyFile(default)/MyFile/main.lua:25: in function ‘ChangeBackground’
…ns(Builds)/MyFile(default)/MyFile/main.lua:31: in main chunk
My chosen IDE is LUA Glider ( of course! :) ).
It would be nice to find a learning course on how to read programming errors, I dont think that exists though…Anyway, any help is much appreciated. Thanks again.
math.randomseed(os.time()) instead math.random (os.time()) - you didn’t check 
local background = bgNames
No! why?! it was placeholder for image, you assigned table there and there is removeSelf() trying to do something on this table and crushes (because it’s not display object)
local names = {'img1.png', 'img2.png', 'img3.png'} local background = nil math.randomseed(os.time()) local function ChangeBackground() if background then background:removeSelf() background = nil end local imageName = names[math.random(1, #names)] background = display.newImage(imageName) end ChangeBackground() timer.performWithDelay(3000, ChangeBackground, -1)
Please, put your code inside code tags for readabiltiy
I see. Well thanks for the update, it does work, now.
I’m going to study this post as well as head over to the blogs. I think I need to do a little catching up on my “lua know-how”. It’s hard to study and keep up with corona sdk since it updates so frequently…Verses my real life work which takes a good 90% of my time everyday. Well, anyhow, I appreciate your response very much. I’m happy to see it works. Thanks again. B)
what is printed on console is so called stack trace - it shows you last code executions leading to error. From last one to few executions before. I will try to break it. First code, comment underneath.
stack traceback:
We begin listing code execution history leading to crush
?: in function \<?:218\>
It shows that error is somewhere at line 128 but corona wasn’t able to trace what failed
[C]: in function 'removeSelf'
We go back in history to check what lead to error at line 128. We see that code executed before it was removeSelf - so it last known code part which failed. However, we do not know why it crused because there was “?” in line above which tells us corona wasn’t able to track it in such way that it can show us. Let’s go back to see what called removeSelf
...ns(Builds)/MyFile(default)/MyFile/main.lua:25: in function 'ChangeBackground'
So what called removeSelf was function ChangeBackground begining at line 25 in file main.lua
...ns(Builds)/MyFile(default)/MyFile/main.lua:31: in main chunk
it just say’s that what was before was general code execution of app.