Add runtime listener to a gameObject

Good day. I was wondering if there is anyway to add a Runtime event listener to a gameObject

the idea is just like this : 

GameObject = display.newImageRect("sample.png", centerX,centerY) GameObject:addEventListener("enterFrame",myFunction)

it appears that this doesn’t work? for me, no result is printing whenever I try to print the Y axis on the “myFunction” function

Yes.  Use a table listener:

local GameObject = display.newImageRect("sample.png", centerX,centerY) function GameObject.enterFrame( self ) print(self.x, self.y, system.getTimer()) end Runtime:addEventListener( "enterFrame", GameObject )

Also, sometimes coded like this:

local GameObject = display.newImageRect("sample.png", centerX,centerY) GameObject.enterFrame = function( self ) print(self.x, self.y, system.getTimer()) end Runtime:addEventListener( "enterFrame", GameObject )

wow it works, thanks. another question:

-will it work independently? like If I have the same name of a GameObject, will the runtime listener will correspond to its own gameObject?:

sample

local GameObject = display.newImageRect("sample.png", centerX,centerY) GameObject.enterFrame = function( self ) print(self.x, self.y, system.getTimer()) end Runtime:addEventListener( "enterFrame", GameObject ) local GameObject = display.newImageRect("sample.png", centerX,centerY) GameObject.enterFrame = function( self ) print(self.x, self.y, system.getTimer()) end Runtime:addEventListener( "enterFrame", GameObject )

will the Runtime GameObject still listens to the 1st GameObject?

  1. Is the “Wow” because it worked or because Corona is awesome?  I’ll assume the latter since I hope their isn’t any surprise it worked. :slight_smile:

  2. Yes, each instance of GameObject has its own listener.  But if you’re doing to have lots of objects and the enterFrame listener is the same for each do this instead:

    local function onEnterFrame ( self ) print(self.x, self.y, system.getTimer()) end local GameObject = display.newImageRect(“sample.png”, centerX,centerY) GameObject.enterFrame = onEnterFrame Runtime:addEventListener( “enterFrame”, GameObject ) local GameObject = display.newImageRect(“sample.png”, centerX + 20,centerY) GameObject.enterFrame = onEnterFrame Runtime:addEventListener( “enterFrame”, GameObject ) local GameObject = display.newImageRect(“sample.png”, centerX + 40,centerY) GameObject.enterFrame = onEnterFrame Runtime:addEventListener( “enterFrame”, GameObject ) …

Now, all objects are executing the same function as their listener, so you’re not wasting memory, and no there is no ‘state’ so two objects using the same listener are not somehow attached via the listener.  

This works the same as the uniquely defined functions, but uses a single instance to save memory.

Yes.  Use a table listener:

local GameObject = display.newImageRect("sample.png", centerX,centerY) function GameObject.enterFrame( self ) print(self.x, self.y, system.getTimer()) end Runtime:addEventListener( "enterFrame", GameObject )

Also, sometimes coded like this:

local GameObject = display.newImageRect("sample.png", centerX,centerY) GameObject.enterFrame = function( self ) print(self.x, self.y, system.getTimer()) end Runtime:addEventListener( "enterFrame", GameObject )

wow it works, thanks. another question:

-will it work independently? like If I have the same name of a GameObject, will the runtime listener will correspond to its own gameObject?:

sample

local GameObject = display.newImageRect("sample.png", centerX,centerY) GameObject.enterFrame = function( self ) print(self.x, self.y, system.getTimer()) end Runtime:addEventListener( "enterFrame", GameObject ) local GameObject = display.newImageRect("sample.png", centerX,centerY) GameObject.enterFrame = function( self ) print(self.x, self.y, system.getTimer()) end Runtime:addEventListener( "enterFrame", GameObject )

will the Runtime GameObject still listens to the 1st GameObject?

  1. Is the “Wow” because it worked or because Corona is awesome?  I’ll assume the latter since I hope their isn’t any surprise it worked. :slight_smile:

  2. Yes, each instance of GameObject has its own listener.  But if you’re doing to have lots of objects and the enterFrame listener is the same for each do this instead:

    local function onEnterFrame ( self ) print(self.x, self.y, system.getTimer()) end local GameObject = display.newImageRect(“sample.png”, centerX,centerY) GameObject.enterFrame = onEnterFrame Runtime:addEventListener( “enterFrame”, GameObject ) local GameObject = display.newImageRect(“sample.png”, centerX + 20,centerY) GameObject.enterFrame = onEnterFrame Runtime:addEventListener( “enterFrame”, GameObject ) local GameObject = display.newImageRect(“sample.png”, centerX + 40,centerY) GameObject.enterFrame = onEnterFrame Runtime:addEventListener( “enterFrame”, GameObject ) …

Now, all objects are executing the same function as their listener, so you’re not wasting memory, and no there is no ‘state’ so two objects using the same listener are not somehow attached via the listener.  

This works the same as the uniquely defined functions, but uses a single instance to save memory.