Hello, I am making a game that has currently 4 scenes, splash, menu, game, and information. When clicking on the button to go to the Information scene it loads that scene fine, and if I just click the play button it loads it fine. If I go to the information scene, back to the menu, and then click play I get an error saying “attempt to index upvalue ‘platform’ (a nil value)” and then referring to line 363. I don’t know why it shows this as I remove the game scene and the information scene on transitioning to the game scene. All variables are stated at the top of the script, so I am not sure what I am doing wrong. Below is the part of the script that is causing problems. line 363 is in scene:hide() where it says platform:removeEventListener(). Any advice is appreciated.
[lua]
function scene:create( event )
physics.pause()
local sceneGroup = self.view
– Code here runs when the scene is first created but has not yet appeared on screen
backGroup = display.newGroup()
sceneGroup:insert(backGroup)
mainGroup = display.newGroup()
sceneGroup:insert(mainGroup)
uiGroup = display.newGroup()
sceneGroup:insert(uiGroup)
background = display.newImageRect(backGroup,“background.png”,720,1280)
background.x = display.contentCenterX
background.y = display.contentCenterY
base = display.newImageRect(mainGroup,“data/images/base.png”, 768,50)
base.x = display.contentCenterX
base.y = display.contentHeight - 25
base.myName = “base”
physics.addBody(base,“static”)
platform = display.newImageRect(mainGroup,“data/images/platform.png”,200,25)
platform:setFillColor(1,0,0)
platform.x = display.contentCenterX
platform.y = display.contentHeight - 100
platform.myName = “platform”
physics.addBody(platform,“static”)
scoreText = display.newText(uiGroup,score,display.contentCenterX,50,native.systemFont, 72)
baseHealth = display.newText(uiGroup,"Health: " …health,base.x,base.y,native.systemFont,32)
backArrow = display.newImageRect(uiGroup,“data/images/BackArrow.png”,64,64)
backArrow.x = 32
backArrow.y = 32
end
– show()
function scene:show( event )
local sceneGroup = self.view
local phase = event.phase
if ( phase == “will” ) then
– Code here runs when the scene is still off screen (but is about to come on screen)
elseif ( phase == “did” ) then
– Code here runs when the scene is entirely on screen
Runtime:addEventListener(“collision”,onCollision)
platform:addEventListener(“touch”,dragPlatform)
timer1 = timer.performWithDelay(math.random(250,1000),fireLaser,0)
backArrow:addEventListener(“tap”,endGame)
physics.start()
end
end
– hide()
function scene:hide( event )
local sceneGroup = self.view
local phase = event.phase
if ( phase == “will” ) then
– Code here runs when the scene is on screen (but is about to go off screen)
elseif ( phase == “did” ) then
– Code here runs immediately after the scene goes entirely off screen
Runtime:removeEventListener(“collision”, onCollision)
platform:removeEventListener(“touch”,dragPlatform)
backArrow:removeEventListener(“tap”,endGame)
timer.cancel(timer1)
physics.pause()
– audio.stop(1)
end
end
[/lua]