annikke.exe! That use of the sin function gave me a great idea for a level, but I'm not nearly skilled enough to put it into effect.
The way you do it is by making a continuously-updated variable (using a loop).
So start out with your function LevelLogic() and lets make a loop:
function LevelLogic()
While GameRunning() do
-- commands go here
coroutine.yield()
end
end
Hopefully you follow what is going on so far. We've created a loop that will run indefinitely.
Next, lets make a variable to represent the up-and-down wavy motion we get with sin:
function LevelLogic()
While GameRunning() do
-- commands go here
fluffy = math.sin(GetGameTime())
coroutine.yield()
end
end
Now we have a continously updating value.
fluffy will slowly roll between the values 1 and -1 as time goes by.
Here's a way to visualise what's going on:

Imagine the circle is a clock, and as it turns it represents time going by.
On the line above, the horizontal axis represents the value of
fluffy.
Maybe we want the pulses to go faster...
(spot the difference here!)
function LevelLogic()
While GameRunning() do
-- commands go here
fluffy = math.sin((GetGameTime() * 2.5))
coroutine.yield()
end
end
After 1 second, we'll be taking the math.sin of 2.5, instead of 1.
So it will rush through the cycles faster. Adjust to taste. :>
Now, drifting between 1 and -1 is all well and good, but perhaps we'd like to change the background colour between 0 and 255. How to do that?
Well, all we do is run some maths on
fluffy and we can convert the minimum value (-1) to 0 and the maximum value (1) to 255.
How to do that?
Like this:
function LevelLogic()
While GameRunning() do
-- commands go here
fluffy = math.sin((GetGameTime() * 2.5))
fluffy = fluffy + 1
-- now fluffy ranges between 0 and 2
fluffy = fluffy / 2
-- now fluffy ranges between 0 and 1
fluffy = fluffy * 255
-- now fluffy ranges between 0 and 255!
coroutine.yield()
end
end
So with that bit of maths, now we have a constantly-updating variable with a suitable range to use for background colour. :>
Finally, lets hook our variable up to control background colour:
function LevelLogic()
While GameRunning() do
-- commands go here
fluffy = math.sin((GetGameTime() * 2.5))
fluffy = fluffy + 1
-- now fluffy ranges between 0 and 2
fluffy = fluffy / 2
-- now fluffy ranges between 0 and 1
fluffy = fluffy * 255
-- now fluffy ranges between 0 and 255!
SetBackdropColour(fluffy,fluffy,fluffy)
coroutine.yield()
end
end
Now our background colour will range between totally black (0,0,0) and totally white (255,255,255).
Maybe we want it to fade between totally black and totally red. Then we would replace the existing background colour command with this one:
SetBackdropColour(fluffy,0,0)
Hope this makes it clearer. Let me know if there's anything you don't understand :>