(This is most definitely a hijack, so if requested I’ll remove this post; Also I am sure this is better documented elsewhere so again, if requested I’ll remove this post or shorten it and link to the official docs.)
Not about SSK, but it occured to me that nomenclature is confusing and I often mess it up, so … at the risk of hijacking this thread, I’d like to post my understanding of events, listeners, and the nomenclature for them…
Events
As I have always understood it, there are two basic categories of events: local and global. Most events exist in only one category, while some existin both.
- enterFrame - global
- collision - local
- touch - local or globl
Listeners
Similarly, listeners come in two basic varieties:
-
Function Listeners
local obj = display.newCircle( 10, 10, 10 ) local function enterFrame() end Runtime:addEventListener( “enterFrame”, enterFrame )
-
Table Listeners
local obj = display.newCircle( 10, 10, 10 ) local function enterFrame( self ) end obj.enterFrame = enterFrame Runtime:addEventListener( “enterFrame”, obj )
The Confusing Part (It was for me… for a long time)
There is generally no specific rule that says one type of event must use one type of listener. In fact, all of these examples are legal.
global event - enterFrame w/ function listener
local obj = display.newCircle( 10, 10, 10 ) local function enterFrame() obj.x = obj.x + 1 end Runtime:addEventListener( "enterFrame", enterFrame )
global event - enterFrame w/ table listener
local obj = display.newCircle( 10, 10, 10 ) local function enterFrame( self ) self.x = self.x + 1 end obj.enterFrame = enterFrame Runtime:addEventListener( "enterFrame", obj )
global event - touch w/ function listener (corrected after initial post)
-- obj is not the object that is being touched. It is the screen. local obj = display.newCircle( 10, 10, 10 ) local function touch( event ) obj:setFillColor( 1, 1, math.random() ) end Runtime:addEventListener( "touch", touch)
global event - touch w/ table listener (corrected after initial post)
-- obj is not the object that is being touched. It is the screen. local obj = display.newCircle( 10, 10, 10 ) local function touch( self, event ) self:setFillColor( 1, 1, math.random() ) end obj.touch = touch Runtime:addEventListener( "touch", obj )
local event - touch w/ function listener (corrected after initial post)
local obj = display.newCircle( 10, 10, 10 ) local function touch( self, event ) self:setFillColor( 1, 1, math.random() ) end obj:addEventListener( "touch", touch )
local event - touch w/ table listener
local obj = display.newCircle( 10, 10, 10 ) local function touch( self, event ) self:setFillColor( 1, 1, math.random() ) end obj.touch = touch obj:addEventListener( "touch" )
Having said that, I typically restrict myself to table listeners because that makes associating the code with objects easier and cleaner.
Finally, if I messed up anything in this post, please correct me! I don’t want to be wrong and I don’t want to lead anyone astray! I think I have this all straight, but I have misused these terms many a time in the past.
Cheers,
Ed