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
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
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]