Skip to content

Instantly share code, notes, and snippets.

@omidahourai
Created December 5, 2012 16:46
Show Gist options
  • Save omidahourai/4217300 to your computer and use it in GitHub Desktop.
Save omidahourai/4217300 to your computer and use it in GitHub Desktop.
Custom Event Tutorial (OO-Lua, 3/4) http://blog.ardentkid.com
--Cat.lua
local scene = scene
local Cat = {Instances={}}
--CAT CLASS METHODS (FIRST LETTER IS CAPITOLIZED)
Cat.Get = scene.Get
Cat.Show = scene.Show
Cat.Dispose = scene.Dispose
function Cat:New()
--creates a cat instance
local cat = display.newImage('cat.png')
cat.show = self.show
cat.hide = self.hide
cat.onMouseShow = self.onMouseShow
cat.onMouseHide = self.onMouseHide
table.insert(self.Instances, cat)
return cat
end
--CAT INSTANCE METHODS (FIRST LETTER IS LOWERCASE)
function Cat:show(config)
--show this cat somewhere on-screen
self.x, self.y = config.x, config.y
self.isVisible = true
--start listeninig for these mouse events
scene:addEventListener('onMouseShow', self)
scene:addEventListener('onMouseHide', self)
end
function Cat:hide()
--hide this cat
Cat:Dispose(self)
self.isVisible = false
--stop this cat from responding to mice.
scene:removeEventListener('onMouseShow', self)
scene:removeEventListener('onMouseHide', self)
end
--CAT INSTANCE CUSTOM EVENT LISTENERS
function Cat:onMouseShow(event)
--code to start chasing the mouse goes here.
end
function Cat:onMouseHide(event)
--code to stop chasing the mouse goes here.
end
--CREATE 3 CATS
for i=1,3 do Cat:New() end
return Cat
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment