Skip to content

Instantly share code, notes, and snippets.

@JesterXL
Last active December 19, 2015 10:49
Show Gist options
  • Save JesterXL/5943653 to your computer and use it in GitHub Desktop.
Save JesterXL/5943653 to your computer and use it in GitHub Desktop.
Corona Invalidation Sample 1 of 2: Component.lua. Show's using enterFrame as a way to ensure drawing only once per frame.
Component = {}
function Component:new()
local component = display.newGroup()
component.classType = "Component"
component.commitDirty = false
function component:construct()
self:createChildren()
self:commitProperties()
end
function component:createChildren()end
function component:commitProperties()end
function component:invalidateProperties()
self.commitDirty = true
self.invalidateNextFrame()
end
function component:invalidate()
self.invalidateProperties()
end
function component:invalidateNextFrame()
Runtime:addEventListener("enterFrame", self)
end
function component:enterFrame()
self:validate()
end
function component:validate()
if self.commitDirty == true then
self.commitDirty = false
self:commitProperties()
end
Runtime:removeEventListener("enterFrame", self)
end
function component:validateNow(recursive)
if recursive == nil then recursive = false end
self:validate()
if recursive == true then
local child
local len = self.numChildren
while len > 0 do
child = self[len]
if child.classType == "Component" then
child:validateNow()
end
len = len - 1
end
end
end
component:construct()
return component
end
return Component
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment