The environment in my game is composed of several overlays, and each overlay gradually fades in and out over time. I was using transition.to() to handle the fading until I tested it on a device; it absolutely kills the framerate (I have a total of 72 items fading in and out simultaneously).
So I decided to write my own fading logic based off of the enterFrame event. I’m hoping that performance won’t be an issue anymore. But I can’t seem to get my objects to fade in properly.
Here’s the relevant code:
-- (transitionTime = 600 and I'm running it at 60 FPS)
function overlay:Update()
if self.delay \> 0 then
self.delay = self.delay - 1
return
end
if self.fadingIn then
self.texture.alpha = self.texture.alpha + (1 / self.transitionTime)
else
self.texture.alpha = self.texture.alpha - (1 / self.transitionTime)
end
if self.texture.alpha \<= 0 or self.texture.alpha \>= 1.0 then
self.delay = math.random( 30, 120 )
self.fadingIn = not self.fadingIn
if self.fadingIn then self.texture.alpha = 0.0 else self.texture.alpha = 1.0 end
end
end
What’s weird is that my objects fade out perfectly. They just refuse to fade in! This is the line of code that’s causing the problem:
if self.fadingIn then
self.texture.alpha = self.texture.alpha + (1 / self.transitionTime)
I can’t seem to assign alpha a value here for some reason, even though it works fine when I’m fading out. If I print out alpha before and after this assignment, alpha’s value doesn’t change.
Is my syntax messed up? I must be missing something really obvious…
Thanks for the help! 
(Here’s the whole file in case that’s useful)
local overlay = {}
local overlay\_mt = { \_\_index = overlay }
function overlay.new( texture, group )
local newOverlay = {}
newOverlay.texture = texture
newOverlay.texture.alpha = 0.1
newOverlay.fadingIn = true
newOverlay.delay = math.random( 30, 120 )
newOverlay.transitionTime = 600
if group then group:insert( newOverlay.texture ) end
return setmetatable( newOverlay, overlay\_mt )
end
function overlay:Update()
if self.delay \> 0 then
self.delay = self.delay - 1
return
end
if self.fadingIn then
self.texture.alpha = self.texture.alpha + (1 / self.transitionTime)
else
self.texture.alpha = self.texture.alpha - (1 / self.transitionTime)
end
if self.texture.alpha \<= 0 or self.texture.alpha \>= 1.0 then
self.delay = math.random( 30, 120 )
self.fadingIn = not self.fadingIn
if self.fadingIn then self.texture.alpha = 0.0 else self.texture.alpha = 1.0 end
end
end
return overlay
[import]uid: 82003 topic_id: 20674 reply_id: 320674[/import]