function inside function?

Hi

i got function that called when game is done, looks like this

function gameresult()   local result = display.newImage('images/result.png')   result:setReferencePoint(display.BottomLeftReferencePoint)   result.x = 0   result.y = 320   function result:tap() --or result:touch     storyboard.gotoScene( "nextlevel" )   end   result:addEventListener("tap",result) end  

its suppose work like this

game playing - game done - show image result (called function gameresult) - the image that show up from gameresult function, when i click it, goes to the next scene/level

but when i click it its nothing happen, and when i put that 2nd function outside the 1st function like this

function gameresult()   local result = display.newImage('images/result.png')   result:setReferencePoint(display.BottomLeftReferencePoint)   result.x = 0   result.y = 320 end   function result:tap() --or result:touch     storyboard.gotoScene( "nextlevel" )   end   result:addEventListener("tap",result)  

it error with something about “nil” value

anyone can help me? thank you

The second block of code has a “scope” problem.  The variable “result” only exists inside the function because you put the word “local” in front of it.  It is local to that function only.

If you instead did:

local result   function gameresult()   result = display.newImage('images/result.png')   result:setReferencePoint(display.BottomLeftReferencePoint)   result.x = 0   result.y = 320 end   function result:tap() --or result:touch     storyboard.gotoScene( "nextlevel" )   end   result:addEventListener("tap",result)

it should solve your problem since we declared result outside of the function where your other code can see it.

Hi Rob,

thanks for the reply, but when i try to do that it still not work, because the result only show up after game done, so if i declare outside the function its nil because the image not exist yet (until the game end)

i found the problem, it seem the next scene is showup but hide by the previous scene, how i remove previous scene completelly, i read about storyboard.purgeAll/purgeScene and storyboard.removeAll/removeScene but not working, where shoud i put that code?

That sounds like you’re not putting the display objects you want storyboard to manage in the scene’s “view” (group).  At the beginning of scene:createScene(event) function there should be this line:

  

      local group = self.view

Group is a display.newGroup() that you have to insert anything you want the scene to manage into:

      group:insert(background)

for instance.  Failure to do so will leave those objects on top of the display and anything storyboard does is underneath it.

Rob you’re genius :smiley:

thank you now its work, i forgot to add group:insert(background) in some part, that why they left of the next scene :slight_smile:

thank you!!

(move to the next part)

The second block of code has a “scope” problem.  The variable “result” only exists inside the function because you put the word “local” in front of it.  It is local to that function only.

If you instead did:

local result   function gameresult()   result = display.newImage('images/result.png')   result:setReferencePoint(display.BottomLeftReferencePoint)   result.x = 0   result.y = 320 end   function result:tap() --or result:touch     storyboard.gotoScene( "nextlevel" )   end   result:addEventListener("tap",result)

it should solve your problem since we declared result outside of the function where your other code can see it.

Hi Rob,

thanks for the reply, but when i try to do that it still not work, because the result only show up after game done, so if i declare outside the function its nil because the image not exist yet (until the game end)

i found the problem, it seem the next scene is showup but hide by the previous scene, how i remove previous scene completelly, i read about storyboard.purgeAll/purgeScene and storyboard.removeAll/removeScene but not working, where shoud i put that code?

That sounds like you’re not putting the display objects you want storyboard to manage in the scene’s “view” (group).  At the beginning of scene:createScene(event) function there should be this line:

  

      local group = self.view

Group is a display.newGroup() that you have to insert anything you want the scene to manage into:

      group:insert(background)

for instance.  Failure to do so will leave those objects on top of the display and anything storyboard does is underneath it.

Rob you’re genius :smiley:

thank you now its work, i forgot to add group:insert(background) in some part, that why they left of the next scene :slight_smile:

thank you!!

(move to the next part)