Module class and display groups are doing my head in!

Hey all,
 
I’m trying to get my head around using module classes and manipulating what is returned. I follow this tutorial, and can generate and display the timer that I want, but that’s as far as I can get. I’d like to have the create group transition onto the screen, and then, when the timer hits zero, transition off the screen. But when I use the transition.from function, I get an error saying that there is no object (But I can print the x value of the group).
 
Here’s my code: growthTimer.lua

 local playerData = require "playerdata" local oddbodData = require "oddboddata" local growthTimer = {} local growthTimer\_mt = { \_\_index = growthTimer } -- metatable local growth = display.newGroup() local growthText local gtBGl local gtBG local growthBadge function growthTimer:createTimer() gtBGl = display.newImageRect("assets/game\_ui/timerBg\_left.png" , 44, 30) gtBGl.anchorX = 0 gtBGl.x = (gtBGl.contentWidth \* 0.5) gtBGl.y = (gtBGl.contentHeight \* 0.5) + 26 growth:insert(gtBGl) gtBGr = display.newImageRect("assets/game\_ui/timerBg\_right.png" , 66, 30) gtBGr.anchorX = 0 gtBGr.x = gtBGl.x + gtBGl.contentWidth gtBGr.y = gtBGl.y growth:insert(gtBGr) growthBadge = display.newImageRect("assets/game\_ui/growthBadge.png" , 33, 37) growthBadge.x = (growthBadge.contentWidth \* 0.5) + 7 growthBadge.y = (growthBadge.contentHeight \* 0.5) + 23 growth:insert(growthBadge) gtBGl:toFront() growthBadge:toFront() local fontName = "Riffic" local growthOptions = { font = fontName, text = "", x = gtBGl.x + 32, y = gtBGl.y - 1.5, fontSize = 12, align = "left" } growthText = display.newText( growthOptions ) growthText.anchorX = 0 growthText:setFillColor( 1, 1, 1 ) growth:insert(growthText) transition.from(growth,{time=700,transition=easing.inBack,x=-(growth.contentWidth + 50)}) return setmetatable( growth, growthTimer\_mt) end function growthTimer:updateGrowth( secondsLeft ) local days = math.floor( secondsLeft / 86400 ) local hours = math.floor( (secondsLeft - (days\*86400)) / 3600 ) local minutes = math.floor( (secondsLeft - (hours\*3600) - (days\*86400)) / 60 ) local seconds = secondsLeft % 60 local leftDays, leftHours, leftMins, leftSec if days \>= 10 then leftDays = string.format( "%02dd ", days) elseif days \< 10 and days \> 0 then leftDays = string.format( "%01dd ", days) else leftDays = "" end if hours \>= 10 then leftHours = string.format( "%02dh ", hours) elseif hours \< 10 and hours \> 0 then leftHours = string.format( "%01dh ", hours) else leftHours = "" end if minutes \>= 10 then leftMins = string.format( "%02dm ", minutes) elseif minutes \< 10 and minutes \> 0 then leftMins = string.format( "%01dm ", minutes) else leftMins = "" end if seconds \>= 10 then leftSec = string.format( "%02ds", seconds) else leftSec = string.format( "%01ds", seconds) end local timeDisplay = leftDays..leftHours..leftMins..leftSec growthText.text = timeDisplay local lastScale = tonumber((string.format("%0.3f", gtBGr.xScale))) local scale = tonumber(( string.format("%0.3f", (growthText.contentWidth / 66 )))) if scale - lastScale \> 0.06 or lastScale - scale \> 0.06 then gtBGr.xScale = scale end if scale \< 1 then local oldX = gtBGr.x local newX = growthText.x + growthText.contentWidth + 12 gtBGr.anchorX = 1 if newX - oldX \> 3 or oldX - newX \> 3 then gtBGr.x = newX end gtBGr.xScale = 1 else gtBGr.anchorX = 0 gtBGr.xScale = scale gtBGr.x = gtBGl.x + gtBGl.contentWidth end end function growthTimer:add( event ) print(self) end function growthTimer:remove( event ) self:removeSelf() --transition.to(growth,{time=700,transition=easing.inBack,x=-(growthData.contentWidth + 50)}) end return growthTimer

And then my main.lua:

 local growthTimer = require "growthTimer" local createTimer = growthTimer:createTimer() group:insert(createTimer) createTimer:add() local loadedGrowth = oddbodData.get("growthTime") local function updateGrowth( event ) local secondsLeft = loadedGrowth - os.time(os.date("!\*t")) if secondsLeft \>= 0 then growthTimer:updateGrowth(secondsLeft) else timer.cancel( event.source ) createTimer:removeSelf() end end local countDownTimer = timer.performWithDelay( 1000, updateGrowth, (loadedGrowth - os.time(os.date("!\*t"))) )

I don’t really care where then transition onscreen is called (when the growth group is created or from a seperate :add function), but I can’t get wither to work, and the :remove function does nothing at all.

There’s a few bits and pieces of random code in there from my stuffing around trying to figure this out, but the idea is

  1. Create a display group with the timer elements in it and have that group transition on screen
  2. Loop the updateTimer function until the timer hits zero (currently working fine)
  3. Remove the display group containing the timer elements

I feel like it should be simple, but I just can’t get it to work. Any ideas would be awesome!
Thanks

if i toss out the term “metatable chaining” does that ring any bells?  if not, then i’d suggest you approach it differently - messing with corona display objects’ metatables to base a class on them is NOT “simple”.

it is far (FAR) easier to base your class on a plain lua table that serves only as a store (as in “storage” not as in “buy”) for your display objects.  say your class has a property named “group” where you’ve stashed the reference to its display group, then to transition it from a class method, just something like:  transition.to(self.group, {…whatever

You’re right in assuming I don’t know much about metatables… I only used it because the tutorial I followed suggested I should. But, I’ve changed my code to return just the group and ditched the metatable altogether and it works just like I wanted. Thanks Dave!

if i toss out the term “metatable chaining” does that ring any bells?  if not, then i’d suggest you approach it differently - messing with corona display objects’ metatables to base a class on them is NOT “simple”.

it is far (FAR) easier to base your class on a plain lua table that serves only as a store (as in “storage” not as in “buy”) for your display objects.  say your class has a property named “group” where you’ve stashed the reference to its display group, then to transition it from a class method, just something like:  transition.to(self.group, {…whatever

You’re right in assuming I don’t know much about metatables… I only used it because the tutorial I followed suggested I should. But, I’ve changed my code to return just the group and ditched the metatable altogether and it works just like I wanted. Thanks Dave!