I am creating an app with start.lua , game.lua and restart.lua scenes . When I click start I go into the game , play the game smoothly . When I die I go to the restart.lua scene . I click restart and go back to game.lua . I can do this a number of times until a certain point where I get this error :
ERROR: Runtime error ?:0: attempt to index field 'contentBounds' (a nil value) stack traceback: ?: in function '\_saveSceneAndHide' ?: in function 'gotoScene' restart.lua:26: in function '?'
I did not add a _saveSceneAndHide function to any of my scenes , Can someone help me with this problem ?
restart.lua:
-- requires local composer = require( "composer" ) local scene = composer.newScene() -- background function scene:create(event) local screenGroup = self.view local randomImage = math.random(1,33) local background = display.newImageRect("images/background"..randomImage..".jpg",display.contentWidth,display.contentHeight) background.x = display.contentCenterX background.y = display.contentCenterY screenGroup:insert(background) local restart = display.newText( "Touch Screen to Play Again", display.contentCenterX, display.contentCenterY, native.systemFont, 25 ) screenGroup:insert(restart) end local function touchScreen( event ) if event.phase == "began" then composer.gotoScene("game", "fade", 400) --line 26 where is error is happening end end function scene:show(event) composer.removeScene( "game" ) Runtime:addEventListener("touch", touchScreen) end function scene:hide(event) display.remove(currentTimeText) Runtime:removeEventListener("touch", touchScreen) end function scene:destroy(event) end scene:addEventListener("create", scene) scene:addEventListener("show", scene) scene:addEventListener("hide", scene) scene:addEventListener("destroy", scene) return scene
This is what I have and I’m getting the same error:
local options = { effect = "fade", time = 400, } local function touchScreen( event ) if event.phase == "began" then composer.gotoScene( "game", options ) end end
I know this doesn’t answer your question but I’d also highly suggest seeding your random call. Take a look at math.randomseed() which will give you more random numbers. Also make sure that the last item in your array doesn’t have a comma at the end (t = {1, 2, 3,}; is not correct while t = {1, 2, 3}; is). Not sure that would cause that error but it definitely isn’t correct afaik
The error points to a situation in composer.gotoScene() where we are trying to save and hide the current scene. We attempt to get the contentBounds of the currentScene. We are not testing for the scene to be nil before trying to fetch it’s bounding box. We probably should harden that.
Some how in your code you’ve managed to get the current scene to be nil. It’s like you’ve managed the remove the scene you are currently in. I would look at your code and make sure you’re not trying to remove the current scene.
function scene:show(event) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then print("Phase started") elseif ( phase == "did" ) then print("phase showing objects") end composer.removeScene( "restart" ) end scene:addEventListener( "show" )
function scene:show(event) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then print("Phase started") elseif ( phase == "did" ) then print("phase showing objects") composer.removeScene( "restart" ) -- MOVE IT TO DID end end scene:addEventListener( "show" )