Syntax for adding event listener to object?

I’m trying to add an enterFrame listener to some of my objects, but I can’t seem to get the syntax right. I’ve looked at some examples on the forums and the docs…I must be missing something very obvious :slight_smile:

The enterFrame listener is being called just fine, but I can’t access any of the properties on my object within it. I get an endless stream of runtime exceptions complaining that my properties are nil.

Should I not use “self” in the enterFrame listener function? Am I screwing up the “.” vs “:” syntax?

[code]
local myObject = {}
local myObject_mt = { __index = myObject }

function myObject.new( displayObject )
local newObject = displayObject
newObject.alpha = 0
newObject.fadingIn = true
newObject.startingTime = system.getTimer()
newObject.transitionTime = 10000

Runtime:addEventListener( “enterFrame”, newObject )

return setmetatable( newObject, myObject_mt )
end

function myObject:enterFrame( event )
if self.fadingIn then
local newAlpha = ( system.getTimer() - self.startingTime ) / ( self.transitionTime )

if newAlpha > 1 then
self.alpha = 1
self.startingTime = system.getTimer()
self.fadingIn = false
else
self.alpha = newAlpha
end
else
local newAlpha = 1 - (( system.getTimer() - self.startingTime ) / ( self.transitionTime ))

if newAlpha < 0 then
self.alpha = 0
self.startingTime = system.getTimer()
self.fadingIn = true
else
self.alpha = newAlpha
end
end
end

return myObject
[/code] [import]uid: 82003 topic_id: 20781 reply_id: 320781[/import]

I think the problem is you aren’t accessing a displayObject when you access self in the enterFrame function. I changed a few things around:

  
local foo = function()  
  
 local myObject = {}  
 local myObject\_mt = { \_\_index = myObject }  
  
  
 function myObject:enterFrame( event )  
  
 --print("displayObject alpha: " .. newObject.display.alpha)  
  
 if self.fadingIn then  
 local newAlpha = ( system.getTimer() - self.startingTime ) / ( self.transitionTime )  
  
 if newAlpha \> 1 then  
 self.alpha = 1  
 self.startingTime = system.getTimer()  
 self.fadingIn = false  
 else  
 self.display.alpha = newAlpha  
 end  
 else  
 local newAlpha = 1 - (( system.getTimer() - self.startingTime ) / ( self.transitionTime ))  
  
 if newAlpha \< 0 then  
 self.alpha = 0  
 self.startingTime = system.getTimer()  
 self.fadingIn = true  
 else  
 self.display.alpha = newAlpha  
 end  
 end  
 end  
  
 function myObject.new( displayObject )  
 print("made new")  
  
 local newObject = {} -- first I declared newObject as a table  
  
 newObject.display = displayObject -- I made newObject.display a reference to the displayObject  
 newObject.display.alpha = 0  
  
 newObject.fadingIn = true  
 newObject.startingTime = system.getTimer()  
 newObject.transitionTime = 10000  
  
 Runtime:addEventListener( "enterFrame", newObject )  
  
 return setmetatable( newObject, myObject\_mt )  
 end   
  
  
  
 return myObject  
  
end  
  
local thing = foo() --instantiate your object  
  
local dspobj = display.newImage("Path/To/Graphic.png") --or some other displayObject  
  
thing.new(dspobj) --create new thing with the displayObject  
  

I do things a bit differently than this so I don’t know if it’s 100% correct, but the object appears and fades in and out if that is your intent. [import]uid: 120 topic_id: 20781 reply_id: 81728[/import]

Ah! That does work! Thanks for helping me out :slight_smile: Problem solved :slight_smile: [import]uid: 82003 topic_id: 20781 reply_id: 81740[/import]