Hi Guys,
I’m a total n00b at Corona, and I thought it might be fun to teach myself. I’ve made lots of progress reading the forums and so on, but I’ve run into a bit of a problem cleaning up objects.
I’ve read several similar threads and I just cannot get my head around what I’m doing wrong - hopefully one of you more experienced people will be able to see it instantly.
When my game levels is over, I have to present a ‘dialogue box’ to my user with some text along the lines of “good work”, or “you failed”, or “time’s up”. To make it readable, I’ve decided to throw up a background. When the user acknowledges the dialogue that should go away.
Now, since I’m going to have several dialogues in the software, I thought a standard background would be a good idea to maintain consistency in look and feel.
My code architecture isn’t yet that great - I’m justifying this is my head as “i’m a beginner and this will come in time”
Anyway, let’s get down the code:
local sceneName = "gameCore" -- exists for printing debug messages local dialogueDG = display.newGroup(); --the display group for the dialog BG local dialogueTxtDG = display.newGroup(); --display group for the text function gc:dialogBG() local fadeInMS = 250; -- adjust the speed of the dialogue box here, to prevent errors later --render a dialogue box background txtBackground = display.newRect(dialogueDG,display.contentCenterX, display.contentCenterY, 100, 100); txtBackground:setFillColor(1,1,1,0.85); txtBackground.strokeWidth = 2; txtBackground:setStrokeColor(0,0,0,1); --grow the dialogue box background to fill the desired area transition.scaleBy( txtBackground, {xScale = 4.0, yScale = 1.5, time=fadeInMS}); --I still haven't worked out scaleBy v scaleTo but this "looks" ok on the screen end function gc:dialogBGerase() local fadeOutMS = 250; -- adjust the speed of the shrink here, to save errors later --shrink the dialogue background transition.scaleTo( txtBackground, {xScale = 0.25, yScale = 0.67, time=fadeOutMS}); --define the erase method used in the timer below local function eraseDialogBG() display.remove(txtBackground); if txtBackground ~=nil then txtBackground:removeSelf(); -- line 134 in my lua code end end timer.performWithDelay(fadeOutMS, eraseDialogBG, 0); end
and here is the result in the Output:
2014-04-26 23:26:11.506 Corona Simulator[10422:507] Runtime error
/Users/username/code/moocow/octopus/gameCore.lua:134: attempt to call method ‘removeSelf’ (a nil value)
stack traceback:
/Users/username/code/moocow/octopus/gameCore.lua:134: in function ‘_listener’
?: in function <?:141>
?: in function <?:218>
Program stopped (pid: 10422).
Program completed in 11.41 seconds (pid: 10422).
because I’m still writing stuff, I’m just using stub code in places - this is the function that calls the cleanup - it appears to be working correctly.
function gc.levelEnd() gc.dialogBG(); timer.performWithDelay(2000, gc.dialogBGerase, 0); end
I simply cannot see where I’m going wrong - any help you could offer would be appreciated.