function setup()
background(0, 0, 0, 0)
Sun = 0
Track = 0
Rise = 1
Day = 2
Set = 3
Night = 4
Time = Rise
parameter("DaySpeed",0.00,2.00,0.5)
end
function draw()
if Time == Rise then
if Sun < 255 then
Sun = Sun + DaySpeed
else
Time = Day
Track = 0
end
elseif Time == Day or Time == Night then
if Track < 255 then
Track = Track + DaySpeed
else
if Time == Day then
Time = Set
else
Time = Rise
end
end
elseif Time == Set then
if Sun > 0 then
Sun = Sun - DaySpeed
else
Time = Night
Track = 0
end
end
background(Sun,Sun, 0, 0)
sprite("Planet Cute:Grass Block",HEIGHT/2,WIDTH/20)
tint(Sun, Sun, Sun, 255)
sprite("Planet Cute:Character Boy",HEIGHT/2,WIDTH/10)
tint(Sun, Sun, Sun, 255)
end
Interesting. When I run this, the sprites are halfway off the screen at the lower right. The format for the sprite command is sprite(“name”,x,y). The x value is usually some fraction of the WIDTH and the y value some fraction of the HEIGHT. If I switch HEIGHT and WIDTH in the sprite lines, it works a lot better. I normally do stuff in portrait mode, so it wasn’t working right. If done in landscape mode, it seems OK.
This version is independent of the frame rate (the rate at which draw() is called by Codea):
supportedOrientations(ANY)
function setup()
twilight = 2/24 -- Two hours, twice a day
parameter("DayDuration", 2, 10, 10) -- Length of 'day', in seconds
sky = color(138, 222, 223) -- Sky is blue
end
function daylight(time)
local t = (time % DayDuration)/DayDuration
if (t >= twilight and t < 0.5 - twilight/2) then return 1 end
if (t > 0.5 + twilight/2 and t < 1) then return 0 end
if t < twilight then return t/twilight end
return 0.5 - (t - 0.5)/twilight
end
function draw()
local sun = daylight(ElapsedTime)
background(sky.r * sun, sky.g * sun, sky.b * sun)
tint(sun * 255)
sprite("Planet Cute:Grass Block",WIDTH/2, 50)
sprite("Planet Cute:Character Boy", WIDTH/2, 80)
end