Scene problem

Really looks like you need to post more code than you are. You’re asking for help on code which appears to be breaking but only posting fragments.

I posted everything that is in error.

Rob - If you’re read that - any propositions?

Hey,

I have one doubt and not yet cleared…

just assume that I have two scenes in my game  and I have to go to the another scene from first scene. but the process of the first scene should not stop, I want it to run in the background even though I am working on the second scene, so when I come back to first scene again, I can see the progress should go on even though scene wasn’t active. 

how to do it???

Thank You

@piyushnitnaware, you might find some success using composer.showOverlay().

@wiktorpl, you need to look in your device’s console log or if you’re using the simulator, the console log and see if there are other error messages besides what pops up.  But you also need to post some code.  Your asking us to know what’s wrong but we can’t read your mind or look at the code on your computer.

Rob

Console:

http://i.imgur.com/kYi5aLZ.png

Code:

local composer = require( "composer" ) local json = require "json" local scene = composer.newScene() local myGridlvl1 local myGrid2lvl1 local myCircle1 local button1 local timer1 local function down (event) myCircle1.y = myCircle1.y + 0 end local function movelevel1(event) myGridlvl1.x = myGridlvl1.x - 4 myGrid2lvl1.x = myGrid2lvl1.x - 4 end function endGamelevel1() local endtext1 = display.newText ("You lost!", 160, 50, font, 32) endtext1:setFillColor (0,0,0) timer.cancel(timer1) Runtime:removeEventListener( "enterFrame", onUpdatelevel1 ) Runtime:removeEventListener( "enterFrame", down ) Runtime:removeEventListener( "enterFrame", movelevel1 ) myCircle1:removeEventListener("collision", myCircle1) button1:removeEventListener( "tap", tap ) local function restart (event) composer.gotoScene( "restart1" ) end local restartbutton1 = display.newImage("restart.png", 150, 200, 100, 100) restartbutton1:addEventListener( "tap", restart ) end local function Collision(self, event) if (event.phase == "began") then if (self.ID == "Circle1" and event.other.ID == "Crash1") then endGamelevel1() end end end local function level2 (event) Runtime:removeEventListener( "enterFrame", onUpdatelevel1 ) Runtime:removeEventListener( "enterFrame", down ) Runtime:removeEventListener( "enterFrame", movelevel1 ) myCircle1:removeEventListener("collision", myCircle1) button1:removeEventListener( "tap", tap ) composer.gotoScene("tolvl2") end local function onUpdatelevel1 (event) if (myGridlvl1.x \< 100) then myGridlvl1.x = 160 if (myGrid2lvl1.x \< 100) then myGrid2lvl1.x = 160 end end end local function tap (event) myCircle1.y = myCircle1.y - 20 end function scene:create( event ) local sceneGroup = self.view physics.start() physics.pause() display.setStatusBar( display.HiddenStatusBar ) button1 = display.newImage("button.png", 0, 200, 1000, 1000) local physics = require "physics" physics.setGravity( 0, 0 ) local backgroundlevel1 = display.newImage("backgroundlevel1.png", 160, 240, 500, 600) myCircle1 = display.newImage( "circle.png", 90, 225, 35 ) myCircle1:scale( 0.25, 0.25 ) myCircle1.ID = "Circle1" local myTable = { ["myCircle1PositionY"] = myCircle1.y, } physics.addBody( myCircle1, {radius=38.5} ) myCircle1.collision = Collision myGridlvl1 = display.newRect (160, 100, 1000, 22) myGridlvl1:setFillColor (0,0,0) myGrid2lvl1 = display.newRect (160, 350, 1000, 22) myGrid2lvl1:setFillColor (0,0,0) myGridlvl1.ID = "Crash1" myGrid2lvl1.ID = "Crash1" physics.addBody( myGridlvl1, "static") physics.addBody( myGrid2lvl1, "static") myCircle1:toFront() button1:toFront() local sceneGroup = display.newGroup(button1, backgroundlevel1, myCircle1, myGridlvl1, myGrid2lvl1) end function scene:show( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then composer.removeHidden() elseif ( phase == "did" ) then Runtime:addEventListener( "enterFrame", onUpdatelevel1 ) Runtime:addEventListener( "enterFrame", down ) Runtime:addEventListener( "enterFrame", movelevel1 ) myCircle1:addEventListener("collision", myCircle1) button1:addEventListener( "tap", tap ) physics.start() timer1 = timer.performWithDelay( 5000, level2) end end function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then physics.stop() local jsonSaveData = json.encode( myTable ) local path = system.pathForFile( "myCircle1Position.txt", system.DocumentsDirectory ) local file = io.open( path, "w" ) file:write( jsonSaveData ) io.close( file ) file = nil elseif ( phase == "did" ) then composer.removeHidden() end end function scene:destroy( event ) local sceneGroup = self.view sceneGroup:remove() display.remove( button1 ) display.remove( backgroundlevel1 ) display.remove( myCircle1 ) display.remove( myGridlvl1 ) display.remove( myGrid2lvl1 ) display.remove( endtext1 ) display.remove( restartbutton1 ) end scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) return scene

Again this is a scope problem.  Look at your line 28 (which the error message clued you in to look at):

button1:removeEventListener( “tap”, tap )

It’s complaining about something being nil.  In fact it’s looking for a property lookup.  This basically means there is a table with some value in it.  That value is indexed by a “key”.  That key-value pair is known as a property.  When you go to look up a property you use it’s key.  There are two possible things that can be “nil”, removeEventListener itself (but that would have generated a different message) or the function tap.  The event “key” is a string so it’s not nil.   Given the message isn’t complaining about a function being nil, that tells me that tap is nil.

And it is.  Lua is a one pass compiler.  That is it starts at the top and scans down to fill things out before it runs the code.  When trying to determine the function named tap at line 28, tap hasn’t been declared yet.  You declare it further down in the code.  Thus the “property” you’re trying to look up is nil giving you that error.

You have to define the function tap before you can use it.  Simply move it somewhere above the function containing line 28. The order of your code matters.

Rob

If I did that then:

rzd4vA8.png

You can’t just move stuff without paying attention to your other code.  Stuff has to happen in the right order.   Look at your errors.  See what’s nil at line 58 and fix it.  You may have to re-arrange other things.

Rob

Code:

local function onUpdatelevel1 (event) if (myGridlvl1.x \< 100) then myGridlvl1.x = 160 if (myGrid2lvl1.x \< 100) then myGrid2lvl1.x = 160 end end end

In line 58 is:

if (myGridlvl1.x \< 100) then

What I can do with that?

Any ideas?

Where do you define MyGridLvl1?

In:   function scene:create( event )

All my code is on previous page.

Anybody?

EDIT:

In the top of main chunk is:

local myGridlvl1
local myGrid2lvl1

myGridlvl1.x = 160
myGrid2lvl1.x = 160

and…

onQjmQu.png

What is going on?

That’s not a very good description of what’s wrong. Which functions are not working? Is that all the code you have in restart1.lua? If so, you’re missing code. Not every function gets called every time you call goto - create and destroy don’t get called every time, for example.

Because of the order that things happen in, you can’t execute these line:

composer.removeScene(“lvl1”)
composer.removeHidden()
composer.gotoScene(“lvl1”)

in the scene’s main chunk.  These need to go into the scene’s scene:show() event.   Your scene needs to implement a create as well and actually create a scene.

Rob

local composer = require( "composer" ) local scene = composer.newScene() composer.removeScene("lvl1") composer.removeHidden() composer.gotoScene("lvl1") return scene

If it’s all code in restart1 and remove/goto should be in functon:show, what should be in function:create?

Your restart scene needs to be a scene.  In your scene:create() put in a background, a message saying “Game over” or “Try again?” and give them a button to select to go back.

I suspect you’re trying to work around restarting a scene by deleting the old scene so it will reset when it restarts.  There are two ways to do this.  The first is to use a cut scene, which is what you’re trying to do.  This allows you to blow away the game scene and which will start it back to the original.  But the idea behind a cut scene is, well that it’s a scene.  This is why I say to make the scene something that the user has to interact with.  This gives you time to remove the level scene in the composer:show()'s did phase.  This allows your cut scene to be created and shown while simultaneously removing the level scene.  The lines:

composer.removeScene(“lvl1”)
composer.removeHidden()
composer.gotoScene(“lvl1”)

need to go inside scene:show():

function scene:show( event ) &nbsp;&nbsp;&nbsp;&nbsp; if event.phase == "did" then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; composer.removeScene("lvl1") &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; composer.removeHidden() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; composer.gotoScene("lvl1") &nbsp;&nbsp;&nbsp; end end

If you do not want a visible cut-scene, then your choice is to manually reset everything that you need to reset in your lvl1 scene.  This should be done in the scene’s composer:show() in the “will” phase.

But I highly recommend using the visual cut scene method.

Rob

Thanks.

I don’t want to do visible cutscene.

My idea is something like that:

in lvl1 is game over, click restart button -> invisible restart1 -> new game in lvl1

Questions:

  1. composer.removeScene(“lvl1”) is using scene:destroy, which is in lvl1 code?

  2. How I can restart timer?

The cut scene lets you use a simple hack of removing the scene and forcing to to be recreated from scratch.  If you refuse to use the cut scene, then you need to reset your timers, reposition objects, etc. in the scene:show()'s will phase.  Then start your timers and such in the did phase.

Rob

Another problem:

In scene:create I added

local function movelevel1(event)

and if I will in scene:show add 

Runtime:addEventListener( "enterFrame", movelevel1 )

then:

  A2EuZjA.png

How can I fix it?