Help on purgeScene I still don't understand it

Please don’t judge me yet…

believe me I have read the documentations many times, I have tried many sample codes, and I still don’t understand the – purgeScene ( ).

Please be patient with me, I really want to learn and I just need this to finish my app.

I post before to anton2111, that it works! I was really happy. Then I do another scene

with the same code and it doesn’t work, I’m going crazy!

I’m including a video so you I can explain myself better

http://www.youtube.com/watch?v=HplAVPcuObs&feature=youtu.be

I will explain the best I can:

The level1 scene has 1 object with physics, and 4 rec, with display.newRect

there is one spot, on the clearTop, (let’s say x=100, y=10) that I don’t have any rec, so the object can “escape” through there.

When I go to level 2, another scene, the display.newRect now is in a different position

so the “escape” spot now is in the clearLeft, in (x = 1, y=280) .

I know that in level2 scene the “escape” spot now has “pysics” on (x.100, y=10)

When I come back to level1 view the “memory” of the program has that information (physics on x=100,y=10)

that’s why --I guess-- now I can not “escape” in the level1 scene, because it’s still in the memory of the program.

–NOW how do I fix this?

----------------------------------FIRST POSSIBILITY----------------------------------------

I’m in “escape1” (scene1) first view. (I remember Ron told me, I purge my Scenes before I go to them)

so before I go to “escape2”, in “escape1.lua” I place

– remove previous scene’s view
    storyboard.purgeScene( “escape2” ) – where?

1.- At the very top of the file? Where I requiere widgets and physics?

2.- at the beginning of createScene? right after local screenGroup?

3.- at the beginning of enterScene?

then, in “escape2.lua” I purgeScene (“escape3”) and so on?


--------------------------------SECOND POSSIBILITY----------------------------------------

Before I go to them" –

In “escape2” as soon as I createScene, I purgeScene (“escape1”)

so I’m purging “escape1” then I createScene escape2?


I just don’t understand this, if you explain really clear and put the code exactly as it should work, I believe this will help a lot of people, not just me.  I’m including the complete code for “escape1” and “escape2”

Thank you very much for helping me really understand the purgeScene ( ) thing.


--------------------- “escape1.lua”--------------------------------------------------------------


local storyboard = require( “storyboard” )
local widget = require “widget”
local scene = storyboard.newScene()

local physics = require (“physics”)
physics.start()

local image, text1, text2, memTimer
local ladyBug
local clearFloor
local clearLeft
local clearTop
local clearRightTop
local clearRightBottom

local function movePlace()
        storyboard.gotoScene( “home”, “crossFade”, 500 )    
    return true
end

local function nextLevel()
        storyboard.gotoScene( “escape2”, “crossFade”, 300 )    
    return true
end

local function dragBody ( event )
    local body = event.target
    local phase = event.phase
    local stage = display.getCurrentStage ( )
    
    if “began” == phase then
        stage:setFocus ( body, event.id )
        body.isFocus = true
        body.tempJoint = physics.newJoint ( “touch”, body, event.x, event.y )
    elseif body.isFocus then
    
            if “moved” == phase then
                body.tempJoint:setTarget ( event.x, event.y )
            elseif “ended” == phase or “cancelled” == phase then
                stage:setFocus (body, nil)
                body.isFocus = false
                body.tempJoint:removeSelf ( )
            end
        
    end
    
    return true
    
end
--------------------------------------------------------------CREATE SCENE----------
function scene:createScene( event )
    local screenGroup = self.view
    
        image = display.newImage( “backgroundEscape1.png” )
        screenGroup:insert( image )
        image.x = 512
        image.y = 384
    
        text1 = display.newText( “Help the Butterfly escape!”, 0, 0, nil, 20 )
        text1:setTextColor( 255 )
        text1.x = 777
        text1.y = 180
        screenGroup:insert( text1 )
        
        text2 = display.newText( “Level 1”, 0, 0, nil, 20 )
        text2:setTextColor( 255 )
        text2.x = 55
        text2.y = 50
        screenGroup:insert( text2 )
        
         clearFloor = display.newRect(1,770,1020,5)
        --clearFloor.strokeWidth = 1
        clearFloor:setFillColor(0, 0, 255)
        clearFloor:setStrokeColor(0, 255, 0)
        screenGroup:insert( clearFloor )
        physics.addBody (clearFloor, “static”)
        
        clearLeft = display.newRect(-5, 1, 5, 768)
        --clearLeft.strokeWidth = 1
        clearLeft:setFillColor(255, 0, 0)
        clearLeft:setStrokeColor(0, 255, 0)
        screenGroup:insert( clearLeft )
        physics.addBody (clearLeft, “static”)
        
        clearTop = display.newRect(2, -5, 350, 5)
        --clearTop.strokeWidth = 1
        clearTop:setFillColor(100, 13, 98)
        clearTop:setStrokeColor(0, 255, 0)
        screenGroup:insert( clearTop )
        physics.addBody (clearTop, “static”)
        
        clearRightTop = display.newRect(580, -5, 500, 5) --visible (1000, 1, 5, 240)
        --clearRightTop.strokeWidth = 1
        clearRightTop:setFillColor(255, 255, 0)
        clearRightTop:setStrokeColor(0, 255, 0)
        screenGroup:insert( clearRightTop )
        physics.addBody (clearRightTop, “static”)
        
        clearRightBottom = display.newRect(1030, 1, 5, 768) --visible (1000, 301, 5, 468)
        --clearRightBottom.strokeWidth = 1
        clearRightBottom:setFillColor(255, 100, 0)
        clearRightBottom:setStrokeColor(0, 255, 0)
        screenGroup:insert( clearRightBottom )
        physics.addBody (clearRightBottom, “static”)

end

--------------------------------------------------------------ENTER SCENE----------
function scene:enterScene( event )
    local group = self.view
    
        – remove previous scene’s view
    storyboard.purgeScene( “home” )
    
        buttonB = widget.newButton{
        defaultFile=“buttonHome.png”,
        onRelease = movePlace
        }
        buttonB.x = 500
        buttonB.y = 705
        group:insert ( buttonB )
        
        buttonNext = widget.newButton{
        defaultFile=“buttonNext.png”,
        onRelease = nextLevel
        }
        buttonNext.x = 900
        buttonNext.y = 705
        group:insert ( buttonNext )
                
        ladyBug = display.newImage (“butterflySmall.png”)
        ladyBug.x = 200
        ladyBug.y = 600
        physics.addBody (ladyBug, “dynamic”, {density=3, friction=-0.7 , bounce = 0.6})

        ladyBug:addEventListener (“touch”, dragBody)
    
end

--------------------------------------------------------------EXIT SCENE----------
function scene:exitScene()
    
    ladyBug:removeEventListener (“touch”, dragBody)
    
    display.remove ( ladyBug )

end

--------------------------------------------------------------DESTROY SCENE----------
function scene:destroyScene( event )

        if buttonB then
            buttonB:removeSelf()
            buttonB = nil
        end
        
        if buttonNext then
            buttonNext:removeSelf()
            buttonNext = nil
        end
end

– END OF YOUR IMPLEMENTATION

scene:addEventListener( “createScene”, scene )
scene:addEventListener( “enterScene”, scene )
scene:addEventListener( “exitScene”, scene )
scene:addEventListener( “destroyScene”, scene )

return scene


--------------------- “escape2.lua”--------------------------------------------------------------


local storyboard = require( “storyboard” )
local widget = require “widget”
local scene = storyboard.newScene()

local physics = require (“physics”)
physics.start()

local image, text1, text2, memTimer
local ladyBug
local clearFloor
local clearLeft
local clearTop
local clearRight

local function movePlace()
        storyboard.gotoScene( “home”, “crossFade”, 500 )    
    return true
end

local function nextLevel()
        storyboard.gotoScene( “escape2”, “crossFade”, 300 )    
    return true
end

local function dragBody ( event )
    local body = event.target
    local phase = event.phase
    local stage = display.getCurrentStage ( )
    
    if “began” == phase then
        stage:setFocus ( body, event.id )
        body.isFocus = true
        body.tempJoint = physics.newJoint ( “touch”, body, event.x, event.y )
    elseif body.isFocus then
    
            if “moved” == phase then
                body.tempJoint:setTarget ( event.x, event.y )
            elseif “ended” == phase or “cancelled” == phase then
                stage:setFocus (body, nil)
                body.isFocus = false
                body.tempJoint:removeSelf ( )
            end
        
    end
    
    return true
    
end
--------------------------------------------------------------CREATE SCENE----------
function scene:createScene( event )
    local screenGroup = self.view
    
    – remove previous scene’s view
    storyboard.purgeScene( “escape1” )
    
        image = display.newImage( “backgroundEscape2.png” )
        screenGroup:insert( image )
        image.x = 512
        image.y = 384
    
        text1 = display.newText( “Help the ant escape!”, 0, 0, nil, 20 )
        text1:setTextColor( 255 )
        text1.x = 777
        text1.y = 180
        screenGroup:insert( text1 )
        
        text2 = display.newText( “Level 2”, 0, 0, nil, 20 )
        text2:setTextColor( 255 )
        text2.x = 55
        text2.y = 50
        screenGroup:insert( text2 )
        
         clearFloor = display.newRect(1,760,1020,5) – blue
        --clearFloor.strokeWidth = 1
        clearFloor:setFillColor(0, 0, 255)
        clearFloor:setStrokeColor(0, 255, 0)
        screenGroup:insert( clearFloor )
        physics.addBody (clearFloor, “static”)
        
        clearLeft = display.newRect(10, 1, 5, 768) – red
        --clearLeft.strokeWidth = 1
        clearLeft:setFillColor(255, 0, 0)
        clearLeft:setStrokeColor(0, 255, 0)
        screenGroup:insert( clearLeft )
        physics.addBody (clearLeft, “static”)
        
        clearTop = display.newRect(1, 20, 1020, 5) – purple
        --clearTop.strokeWidth = 1
        clearTop:setFillColor(100, 13, 98)
        clearTop:setStrokeColor(0, 255, 0)
        screenGroup:insert( clearTop )
        physics.addBody (clearTop, “static”)
        
        clearRight = display.newRect(1020, 1, 5, 768) – green
        --clearRight.strokeWidth = 1
        clearRight:setFillColor(0, 255, 0)
        clearRight:setStrokeColor(0, 255, 0)
        screenGroup:insert( clearRight )
        physics.addBody (clearRight, “static”)
        
        

end

--------------------------------------------------------------ENTER SCENE----------
function scene:enterScene( event )
    local group = self.view
    
        buttonB = widget.newButton{
        defaultFile=“buttonHome.png”,
        onRelease = movePlace
        }
        buttonB.x = 500
        buttonB.y = 705
        group:insert ( buttonB )
        
        buttonNext = widget.newButton{
        defaultFile=“buttonNext.png”,
        onRelease = nextLevel
        }
        buttonNext.x = 900
        buttonNext.y = 705
        group:insert ( buttonNext )
                
        ladyBug = display.newImage (“ant.png”)
        ladyBug.x = 200
        ladyBug.y = 600
        physics.addBody (ladyBug, “dynamic”, {density=3, friction=-0.7 , bounce = 0.6})

        ladyBug:addEventListener (“touch”, dragBody)
    
end

--------------------------------------------------------------EXIT SCENE----------
function scene:exitScene()
    
    ladyBug:removeEventListener (“touch”, dragBody)
    
    display.remove ( ladyBug )

end

--------------------------------------------------------------DESTROY SCENE----------
function scene:destroyScene( event )

        if buttonB then
            buttonB:removeSelf()
            buttonB = nil
        end
        
        if buttonNext then
            buttonNext:removeSelf()
            buttonNext = nil
        end
end

– END OF YOUR IMPLEMENTATION

scene:addEventListener( “createScene”, scene )
scene:addEventListener( “enterScene”, scene )
scene:addEventListener( “exitScene”, scene )
scene:addEventListener( “destroyScene”, scene )

return scene
 

 

Can you try doing a object:removeBody() in your destoryScene?

Hi Rob, I put this


--------------------------------------------------------------DESTROY SCENE----------
function scene:destroyScene( event )

        if buttonB then
            buttonB:removeSelf()
            buttonB = nil
        end
        
        if buttonNext then
            buttonNext:removeSelf()
            buttonNext = nil
        end
        
        object:removeBody( clearFloor )
        object:removeBody( clearLeft )
        object:removeBody( clearTop )
        object:removeBody( clearRightTop )
        object:removeBody( clearRightBottom )
end

I remove all the objects that has physics in the destroy scene, and it doesn’t work!


I you see the code, in the dragBody function there is one line of code

body.tempJoint:setTarget ( event.x, event.y )

I have no errors in the “tempJoint”

I try to use on the exitScene, the

physics.stop() – I though maybe it works like the audio stuff.

but when I did that I got error in the tempJoint and it doesn’t work either.

What can be wrong?

Victor

You should put your physics.start() in enterScene and physics.stop() in your exitScene().  Since you create the bodies in createScene(), you need to remove them in destoryScene().

Now your objects will still exist until you purge the scene.  So in your enterScene() I would do a storyboard.purgeAll() in your enterScene() to clean out your previous scenes, or in your main.lua after you load storyboard, do a

storyboard.purgeOnSceneChange = true

then it will purge your scenes for you.  This makes sure that your createScene()'s fire every time the scene loads and destroyScene() fires when leaving it.

Let’s see if I get it correct “You should put your physics.start() in enterScene” Done!

--------------------------------------------------------------ENTER SCENE----------
function scene:enterScene( event )
    local group = self.view
    
    local physics = require (“physics”) – Here in the enterScene
physics.start()
    
        buttonB = widget.newButton{
        defaultFile=“buttonHome.png”,
        onRelease = movePlace
        }  – etc…


“and physics.stop() in your exitScene().” – Done!

--------------------------------------------------------------EXIT SCENE----------
function scene:exitScene()
    
    ladyBug:removeEventListener (“touch”, dragBody)
    
    display.remove ( ladyBug )
    
    physics.stop() – It’s here done!

end


"or in your main.lua after you load storyboard, do a storyboard.purgeOnSceneChange = true – Done!


display.setStatusBar( display.HiddenStatusBar )
local storyboard = require “storyboard”
storyboard.purgeOnSceneChange = true
storyboard.gotoScene( “home” )


When I run the coprogram

File: …sers/victormbarba/Desktop/Piano For Kids/escape1.lua
Line: 83
Attempt to index global ‘physics’ (a nil value)

– this is the line 83 “physics.addBody (clearFloor, “static”)”


This is waht I need “This makes sure that your createScene()'s fire every time the scene loads and destroyScene() fires when leaving it.”

Thanks Rob for your help, we’re almost there at the end

Victor

you need to put the:

local physics = require(“physics”)

back towards the top of the module.  I only wanted you to put the physics.start() line in enterScene()

Something weir happened!

The physics thing it seems to work now, thanks God and you. But now the “escape2” it’s only loading the createScene

everything in the enterScene is not showing – well not true.

I have 3 objects in enterScene

1.- the home button – widget

2.- the next button – widget

  1. the ant it’s an image – display image

All I see it’s the displayImage the ant

I don’t see the 2 buttons.


– --==***************************************************************************+±- –
– Piano For Kids
– By Victor M. Barba
– Copyright 2013 – All Rights Reserved

– Version 1.0
– --==***************************************************************************+±- –

local storyboard = require( “storyboard” )
local widget = require “widget”
local scene = storyboard.newScene()

local physics = require (“physics”)

local image, text1, text2, memTimer
local ladyBug
local clearFloor
local clearLeft
local clearLeft1
local clearTop
local clearRight

local function movePlace()
        storyboard.gotoScene( “home”, “crossFade”, 500 )    
    return true
end

local function nextLevel()
        storyboard.gotoScene( “escape3”, “crossFade”, 300 )    
    return true
end

local function dragBody ( event )
    local body = event.target
    local phase = event.phase
    local stage = display.getCurrentStage ( )

    
    if “began” == phase then
        stage:setFocus ( body, event.id )
        body.isFocus = true
        body.tempJoint = physics.newJoint ( “touch”, body, event.x, event.y )
    elseif body.isFocus then
    
            if “moved” == phase then
                body.tempJoint:setTarget ( event.x, event.y )
            elseif “ended” == phase or “cancelled” == phase then
                stage:setFocus (body, nil)
                body.isFocus = false
                body.tempJoint:removeSelf ( )
            end
        
    end
    
    return true
    
end
--------------------------------------------------------------CREATE SCENE----------
function scene:createScene( event )
    local screenGroup = self.view
    
        physics.start()
    
        image = display.newImage( “backgroundEscape2.png” )
        screenGroup:insert( image )
        image.x = 512
        image.y = 384
    
        text1 = display.newText( “Help the ant escape!”, 0, 0, nil, 20 )
        text1:setTextColor( 255 )
        text1.x = 600
        text1.y = 300
        screenGroup:insert( text1 )
        
        text2 = display.newText( “Level 2”, 0, 0, nil, 20 )
        text2:setTextColor( 255 )
        text2.x = 55
        text2.y = 50
        screenGroup:insert( text2 )
        
         clearFloor = display.newRect(1,770,1020,5) – blue
        --clearFloor.strokeWidth = 1
        clearFloor:setFillColor(0, 0, 255)
        clearFloor:setStrokeColor(0, 255, 0)
        screenGroup:insert( clearFloor )
        physics.addBody (clearFloor, “static”)
        
        clearLeft = display.newRect(-10, 1, 5, 350) – red
        --clearLeft.strokeWidth = 1
        clearLeft:setFillColor(255, 0, 0)
        clearLeft:setStrokeColor(0, 255, 0)
        screenGroup:insert( clearLeft )
        physics.addBody (clearLeft, “static”)
        
        clearLeft1 = display.newRect(-10, 450, 5, 350) – red
        --clearLeft1.strokeWidth = 1
        clearLeft1:setFillColor(255, 0, 0)
        clearLeft1:setStrokeColor(0, 255, 0)
        screenGroup:insert( clearLeft1 )
        physics.addBody (clearLeft1, “static”)
        
        clearTop = display.newRect(1, -15, 1020, 5) – purple
        --clearTop.strokeWidth = 1
        clearTop:setFillColor(100, 13, 98)
        clearTop:setStrokeColor(0, 255, 0)
        screenGroup:insert( clearTop )
        physics.addBody (clearTop, “static”)
        
        clearRight = display.newRect(1035, 1, 5, 768) – green
        --clearRight.strokeWidth = 1
        clearRight:setFillColor(0, 255, 0)
        clearRight:setStrokeColor(0, 255, 0)
        screenGroup:insert( clearRight )
        physics.addBody (clearRight, “static”)
        
    
        

end

--------------------------------------------------------------ENTER SCENE----------
function scene:enterScene( event )
    local group = self.view

        buttonB = widget.newButton{
        defaultFile=“buttonHome.png”,
        onRelease = movePlace
        }
        buttonB.x = 500
        buttonB.y = 705
        group:insert ( buttonB )
        
        buttonNext = widget.newButton{
        defaultFile=“buttonNext.png”,
        onRelease = nextLevel
        }
        buttonNext.x = 900
        buttonNext.y = 705
        group:insert ( buttonNext )
                
        ladyBug = display.newImage (“ant.png”)
        ladyBug.x = 200
        ladyBug.y = 600
        physics.addBody (ladyBug, “dynamic”, {density=9, friction=-0.9 , bounce = 0.1})

        ladyBug:addEventListener (“touch”, dragBody)
        
    

    
end

--------------------------------------------------------------EXIT SCENE----------
function scene:exitScene()
    
    ladyBug:removeEventListener (“touch”, dragBody)
    
    display.remove ( ladyBug )
    
    physics.stop()

end

--------------------------------------------------------------DESTROY SCENE----------
function scene:destroyScene( event )

        if buttonB then
            buttonB:removeSelf()
            buttonB = nil
        end
        
        if buttonNext then
            buttonNext:removeSelf()
            buttonNext = nil
        end
        

        
end

– END OF YOUR IMPLEMENTATION

scene:addEventListener( “createScene”, scene )
scene:addEventListener( “enterScene”, scene )
scene:addEventListener( “exitScene”, scene )
scene:addEventListener( “destroyScene”, scene )

return scene
 

Victor

I think it works Rob

I think it works!!!

I’m not 100% sure but I think it works!

I just add at the beginning the

local buttonB

local buttonNext

The buttons were not showing so I put them in the top and now I see them!

and the physics works YES!

I’m going to work on the other 3 “escape.lua” and then test the whole app

But I think it works

Thank you, thank you Rob, thanks a lot

Yes

Victor

I knew it, too good to be true…

Everything in the game works fine, all the “escape1” “escape2” etc…

but now the main app in the very beginning it doesn’t work, Nothing!

I can not use the main buttons in home.lua to go to “play” or “learn” or “Songs”

so only the scenes in the game with physics works

all the other scenes where I have no physics they don’t work

I know it’s difficult, but we’ll get it working , I know that

Victor

I think the line of code at the main.lua

display.setStatusBar( display.HiddenStatusBar )
local storyboard = require “storyboard”
storyboard.purgeOnSceneChange = true – THIS ONE!
storyboard.gotoScene( “home” )

is preventing me from using all the other files.lua

I don’t know but I’m just guessing

You see? If I take that line of code out, the whole app works fine

but the 5 .lua files with the physic game they don’t work.

And if I put the line back, the game with the physics works, but everything else don’t

I don’t get it.

If I get the – storyboard.purgeOnSceneChange = true – THIS ONE!

out everything works but the second time I go to any “escape1” scene I get this

File: …sers/victormbarba/Desktop/Piano For Kids/escape1.lua
Line: 53
Attempt to index field ‘tempJoint’ (a nil value)

I read in here

http://developer.coronalabs.com/content/game-edition-physics-joints#Dragging_objects_with_touch_joints

But I don’t get it

what’s “tempJoint” is it a variable, I guess not, I tried to put it on top

local tempJoint

because the error says “nil” but still is not working

I’m going Crazy, why is so difficult?

I just want to have a little game in my app, as a bonus.

If the “escape” game can not work, I’m trying the “bullet” one on the sample code

you throw a brick an destroy some cans…

The problem I see in most of the codes, or videos in you tube or corona is that they make all the sample code

for the main.lua file. Just 1 file, and it works

but when I have the storyboard that’s the problem.

I made the code work so far with the bullet but when I go to another scene

the “bricks” and the “cans” they don’t go away, everything else is fine.

So I notice that the bricks and the cans are in a special function


local cans = {}

for i = 1, 7 do
    for j = 1, 8 do
    cans[i] = display.newImage( “soda_can.png”, 190 + (i*24), 220 - (j*40) )
    
    physics.addBody( cans[i], { density=0.2, friction=0.1, bounce=0.4 } )
    end
end

local bricks = {}
local n = 0

local function throwBrick()
    n = n + 1
    bricks[n] = display.newImage( “brick.png”, -20, 140  - (n*20) )

    physics.addBody( bricks[n], { density=3.0, friction=0.5, bounce=0.05 } )

    – remove the “isBullet” setting below to see the brick pass through cans without colliding!
    bricks[n].isBullet = true

    bricks[n].angularVelocity = 100
    bricks[n]:applyForce( 200, 0, bricks[n].x, bricks[n].y ) – 1200 for 200
end


I tried the display.remove ( cans ) on exitScene – NO GOOD!

I tried the insert in to group thing – group:insert( cans ) – right after this line

cans[i] = display.newImage( “soda_can.png”, 190 + (i*24), 220 - (j*40) )

group:insert( cans )

– NO GOOD!

I tried the

if cans then
            cans:removeSelf()
            cans = nil
        end

on the destroy scene n=and NO GOOD

how do I make the cans and the bricks go away. I’m trying to think logic, and using what I know so far to try to make sense but things don’t work as I think they should work, I know – I need to learn more –

That’s why I’m asking please for your help.

I can make the “escape” game or the “Bullet” game work, if not I can try another one, one should work.

Another thing, Do you know where I can get sample codes for “things” but in a storyboard, not just in main.lua?

Thanks

victor

Let me try and explain this.

Each storyboard scene is a lua module file where the file itself is a chunk of code that is executed exactly once, the very first time it loads.  Anything you put in that chunk, like code to run a function or initialize a variable only happens once.  Functions you create can be stored in the main chunk.  In that main chunk, the last few lines do something very important.  They setup a series of event handlers that handle various storyboard events, like when the scene is created or exited.  Those functions (createScene, enterScene, exitScene and destoryScene) **MAY** get called at certain points.   The code that sets up the handlers is in the modules main chunk and are executed once to set them up.

Now you need to think about this next bit.   Part of scene creation is the creation of it’s “view”, a large display group for lack of a better description.  If the scene’s view does not exist, it is created and the createScene event gets triggered which then executes the code in scene:createScene().  This happens off screen.  This is where you should create the various bits that will be shown.  When this scene finishes, it is transitioned on the screen and you now have a scene view to work with.

Once the scene is on the screen, an enterScene event is dispatched and the scene:enterScene() function executes.

When you call storyboard.gotoScene(“somewhere”) the scene’s exitScene event is displatched and scene:exitScene() gets run.

Now is where the fun begins.  By default, the assumption is you want to come back to this scene and use it again.  If you can afford the memory, storyboard will keep it alive and in memory for you.  It’s offscreen so people can’t see and interact with it (except for your physics problem).   When you come back to this scene, since it’s still in memory, storyboard simply transitions it back on the screen (createScene is **NOT** called, but enterScene is!).  Anything in enterScene gets run again and your scene restarts.  Leave it again and exitScene  gets called.

Now if you run out of memory, storyboard will start removing old scenes that haven’t been used in a while.  This is called Purging.  (Don’t worry if this seems hard to get, it took me a while to understand it).  Purging involves calling the scene’s destoryScene event then it removes all the display objects (but not physics or audio) to free up memory and destorys the scene’s “view” (group).

When you eventually go back to that scene, storyboard says “Whoa! I don’t have a view for it”, so it calls your createScene again to rebuild it, transition it on screen, call eventScene and we are back to normal.  Note… the code in the main chunk does not get re-executed. 

There are two other (well three) ways that scenes can get purged.  By your command using either storyboard.purgeScene(“scenename”) or by calling storyboard.purgeAll()   Or you can set a flag in your main.lua to have the scenes purged automatically for you:   

     storyboard.purgeOnSceneChange = true

You are instructing storyboard to automatically clean up scenes when it’s done with them.  That means  everytime you leave a scene, it’s destoryScene() gets called.  This is how your physics bodies are getting removed.   The problem is though that any functions you call that’s in the main chunk like physics.start() or setting some value:    local var = 10, doesn’t happen a 2nd time.   They need to be in the enterScene() function or the createScene() (depending on where it is logical to do it).

By you purging the scenes each time, your objects get cleaned up and you don’t have any lingering physics bodies sitting around to mess with the next scene.

Can you post your home.lua code.  Please type in the string before you paste the code in (without the space) and when you're done put a at the end (again without the space inside the brackets).  It will make your code more readable.

Now this likely broke your other scenes because you have bits of code where it’s not going to execute properly.

Thank you Rob for explaining that, I’m beginning to understand, just a little bit.

thank you for the --[–code–]-- thing I was wondering how you guys did that. Thanks.

The app start at home.lua and has 4 buttons –

  1. play

  2. learn

  3. songs

  4. game – in the game I have all the physics. I call the “escape1” “escape2” and so on.

---------main.lua-------

display.setStatusBar( display.HiddenStatusBar ) local storyboard = require "storyboard" storyboard.purgeOnSceneChange = true storyboard.gotoScene( "home" )

I start the app with the home.lua file. In there I have the 4 buttons.

-----------home.lua ------------

local storyboard = require( "storyboard" ) local widget = require "widget" local scene = storyboard.newScene() local wrongSound = audio.loadSound( "wrongSound.mp3") local buttonPlay local function buttonPlay()         storyboard.gotoScene( "play1", "crossFade", 500 )         return true end local function buttonLearn()         storyboard.gotoScene( "learn1", "crossFade", 500 )         return true end local function buttonSongs()         storyboard.gotoScene( "list1", "crossFade", 500 )         return true end local function playGame()         storyboard.gotoScene( "escape1", "crossFade", 500 )         return true end -- --==\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*[CREATE SCENE]\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++-- -- function scene:createScene( event )     local group = self.view          local background = display.newImage( "backgroundHome.png" )          buttonPlay = widget.newButton{         defaultFile="buttonPlayHomeRelease.png",         overFile="buttonPlayHomeOver.png",         onRelease = buttonPlay     }     buttonPlay.x = 240     buttonPlay.y = 377     buttonPlay:scale (.9, .9)          buttonLearn = widget.newButton{         defaultFile="buttonLearnHomeRelease.png",         overFile="buttonLearnHomeOver.png",         onRelease = buttonLearn     }     buttonLearn.x = 810     buttonLearn.y = 377     buttonLearn:scale (.9, .9)          buttonSongs = widget.newButton{         defaultFile="buttonSongsHomeRelease.png",         overFile="buttonSongsHomeOver.png",         onRelease = buttonSongs     }     buttonSongs.x = 240     buttonSongs.y = 550     buttonSongs:scale (.9, .9)          buttonGame = widget.newButton{         defaultFile="buttonGameHomeRelease.png",         overFile="buttonGameHomeOver.png",         onRelease = playGame     }     buttonGame.x = 810     buttonGame.y = 550     buttonGame:scale (.9, .9) ---------------------------------------------------------------------insert into group----     group:insert ( background )     group:insert ( buttonPlay )     group:insert ( buttonLearn )     group:insert ( buttonSongs )     group:insert ( buttonGame )      end -- --==\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*[ENTER SCENE]\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++-- -- function scene:enterScene( event )     local group = self.view end -- --==\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*[EXIT SCENE]\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++-- -- function scene:exitScene( event )     local group = self.view end -- --==\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*[DESTROY SCENE]\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++-- -- function scene:destroyScene( event )     local group = self.view              if buttonPlay then         buttonPlay:removeSelf()         buttonPlay = nil     end          if buttonLearn then         buttonLearn:removeSelf()         buttonLearn = nil     end          if buttonSongs then         buttonSongs:removeSelf()         buttonSongs = nil     end          if buttonGame then         buttonGame:removeSelf()         buttonGame = nil     end    end -- --==\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*[EVENT LISTENER]\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++-- -- ---------------------------------------------------------------------------------- scene:addEventListener( "createScene", scene ) scene:addEventListener( "enterScene", scene ) scene:addEventListener( "exitScene", scene ) scene:addEventListener( "destroyScene", scene ) return scene

All of the scenes without physics, they work fine. The problem is getting them to work

physics and no physics. All of the files without physics works pretty much the same

– a background, a few buttons, and an object for animation with transition.to like this

----------------------- level1Q14--------

local storyboard = require( "storyboard" ) local widget = require "widget" local scene = storyboard.newScene() local wrongSound = audio.loadSound( "wrongSound.mp3") local ladyBug local ladyBug1 local ladyBug2 local ladyBug3 -- --==\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*[FUNCTIONS TO GO TO ANOTHER SCENE]\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++-- -- local function buttonHome()         storyboard.gotoScene( "home", "crossFade", 1000 )         return true end local function buttonApple()         storyboard.gotoScene( "learn1", "crossFade", 1000 )         return true end local function buttonBulbHome()         storyboard.gotoScene( "ending", "crossFade", 1000 )         return true end local function buttonRound1()         storyboard.gotoScene( "wrong1", "crossFade", 1000 )         audio.play(wrongSound)     return true end local function buttonRound2()         storyboard.gotoScene( "wrong1", "crossFade", 1000 )         audio.play(wrongSound)     return true end local function buttonRound3()         storyboard.gotoScene( "level1Q15", "crossFade", 1000 )     return true end local function buttonRound4()         storyboard.gotoScene( "wrong1", "crossFade", 1000 )         audio.play(wrongSound)     return true end -- --==\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*[CREATE SCENE]\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++-- -- function scene:createScene( event )     local group = self.view          local background = display.newImage( "backgroundLevel1Q14.png" )     buttonHome = widget.newButton{         defaultFile="buttonHome.png",         onRelease = buttonHome     }     buttonHome.x = 522     buttonHome.y = 655          buttonApple = widget.newButton{         defaultFile="buttonApple.png",         onRelease = buttonApple     }     buttonApple.x = 420     buttonApple.y = 710          buttonBulbHome = widget.newButton{         defaultFile="buttonBulbHomeRelease.png",         overFile="buttonBulbHomeOver.png",         onRelease = buttonBulbHome     }     buttonBulbHome.x = 615     buttonBulbHome.y = 685          buttonRound1 = widget.newButton{         defaultFile="buttonHalfRest.png",         onRelease = buttonRound1     }     buttonRound1.x = 166     buttonRound1.y = 312          buttonRound2 = widget.newButton{         defaultFile="buttonWholeRest.png",         onRelease = buttonRound2     }     buttonRound2.x = 510     buttonRound2.y = 432          buttonRound3 = widget.newButton{         defaultFile="buttonQuarterRest.png",         onRelease = buttonRound3     }     buttonRound3.x = 800     buttonRound3.y = 322          buttonRound4 = widget.newButton{         defaultFile="buttonEightRest.png",         onRelease = buttonRound4     }     buttonRound4.x = 233     buttonRound4.y = 579      ---------------------------------------------------------------------insert into group----          group:insert ( background )     group:insert ( buttonHome )     group:insert ( buttonApple )     group:insert ( buttonBulbHome )     group:insert ( buttonRound1 )     group:insert ( buttonRound2 )     group:insert ( buttonRound3 )     group:insert ( buttonRound4 )      end -- --==\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*[ENTER SCENE]\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++-- -- function scene:enterScene( event )     local group = self.view          ladyBug = display.newImage( "bat.png" )     ladyBug.x = 600     ladyBug.y = 800     ladyBug:scale (.4, .4)     ladyBug.rotation = ( -10 )     transition.to(ladyBug, {x=200 , y=112, time=4000})          ladyBug1 = display.newImage( "bat.png" )     ladyBug1.x = 50     ladyBug1.y = 800     ladyBug1:scale (.4, .4)     ladyBug1.rotation = ( 87 )     transition.to(ladyBug1, {x=900 , y=112, time=3300})          ladyBug2 = display.newImage( "bat.png" )     ladyBug2.x = 1000     ladyBug2.y = 400     ladyBug2:scale (.4, .4)     ladyBug2.rotation = ( -50 )     transition.to(ladyBug2, {x=23 , y=300, time=3300})          ladyBug3 = display.newImage( "bat.png" )     ladyBug3.x = 666     ladyBug3.y = -400     ladyBug3:scale (.4, .4)     ladyBug3.rotation = ( 190 )     transition.to(ladyBug3, {x=800 , y=700, time=3300})          group:insert ( ladyBug )     group:insert ( ladyBug1 )     group:insert ( ladyBug2 )     group:insert ( ladyBug3 )      end -- --==\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*[EXIT SCENE]\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++-- -- function scene:exitScene( event )     local group = self.view          display.remove(ladyBug)     display.remove(ladyBug1)     display.remove(ladyBug2)     display.remove(ladyBug3) end -- --==\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*[DESTROY SCENE]\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++-- -- function scene:destroyScene( event )     local group = self.view             if buttonHome then             buttonHome:removeSelf()             buttonHome = nil         end                  if buttonApple then             buttonApple:removeSelf()             buttonApple = nil         end                  if buttonBulbHome then             buttonBulbHome:removeSelf()             buttonBulbHome = nil         end                  if buttonRound1 then             buttonRound1:removeSelf()             buttonRound1 = nil         end                  if buttonRound2 then             buttonRound2:removeSelf()             buttonRound2 = nil         end                  if buttonRound3 then             buttonRound3:removeSelf()             buttonRound3 = nil         end                  if buttonRound4 then             buttonRound4:removeSelf()             buttonRound4 = nil         end end -- --==\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*[EVENT LISTENER]\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++-- -- ---------------------------------------------------------------------------------- scene:addEventListener( "createScene", scene ) scene:addEventListener( "enterScene", scene ) scene:addEventListener( "exitScene", scene ) scene:addEventListener( "destroyScene", scene ) return scene

And all of the “escape1” lua files with physics they work the same

– a background, the walls to prevent the object from leaving the screen

and a “magic spot” where I have no rects, so the object (or bee) can escape.

-------------------------- escape1.lua -----------

local storyboard = require( "storyboard" ) local widget = require "widget" local scene = storyboard.newScene() local physics = require ("physics") local image, text1, text2, memTimer local ladyBug local clearFloor local clearLeft local clearTop local clearRightTop local clearRightBottom local buttonB local buttonNext local function movePlace()         storyboard.gotoScene( "home", "crossFade", 500 )         return true end local function nextLevel()         storyboard.gotoScene( "escape2", "crossFade", 300 )         return true end local function dragBody ( event )     local body = event.target     local phase = event.phase     local stage = display.getCurrentStage ( )          if "began" == phase then         stage:setFocus ( body, event.id )         body.isFocus = true         body.tempJoint = physics.newJoint ( "touch", body, event.x, event.y )     elseif body.isFocus then                  if "moved" == phase then                 body.tempJoint:setTarget ( event.x, event.y )             elseif "ended" == phase or "cancelled" == phase then                 stage:setFocus (body, nil)                 body.isFocus = false                 body.tempJoint:removeSelf ( )             end              end          return true      end --------------------------------------------------------------CREATE SCENE---------- function scene:createScene( event )     local screenGroup = self.view              physics.start()              image = display.newImage( "backgroundEscape1.png" )         screenGroup:insert( image )         image.x = 512         image.y = 384              text1 = display.newText( "Help the Butterfly escape!", 0, 0, nil, 20 )         text1:setTextColor( 255 )         text1.x = 777         text1.y = 180         screenGroup:insert( text1 )                  text2 = display.newText( "Level 1", 0, 0, nil, 20 )         text2:setTextColor( 255 )         text2.x = 55         text2.y = 50         screenGroup:insert( text2 )                   clearFloor = display.newRect(1,770,1020,5)         --clearFloor.strokeWidth = 1         clearFloor:setFillColor(0, 0, 255)         clearFloor:setStrokeColor(0, 255, 0)         screenGroup:insert( clearFloor )         physics.addBody (clearFloor, "static")                  clearLeft = display.newRect(-5, 1, 5, 768)         --clearLeft.strokeWidth = 1         clearLeft:setFillColor(255, 0, 0)         clearLeft:setStrokeColor(0, 255, 0)         screenGroup:insert( clearLeft )         physics.addBody (clearLeft, "static")                  clearTop = display.newRect(2, -5, 350, 5)         --clearTop.strokeWidth = 1         clearTop:setFillColor(100, 13, 98)         clearTop:setStrokeColor(0, 255, 0)         screenGroup:insert( clearTop )         physics.addBody (clearTop, "static")                  clearRightTop = display.newRect(580, -5, 500, 5) --visible (1000, 1, 5, 240)         --clearRightTop.strokeWidth = 1         clearRightTop:setFillColor(255, 255, 0)         clearRightTop:setStrokeColor(0, 255, 0)         screenGroup:insert( clearRightTop )         physics.addBody (clearRightTop, "static")                  clearRightBottom = display.newRect(1030, 1, 5, 768) --visible (1000, 301, 5, 468)         --clearRightBottom.strokeWidth = 1         clearRightBottom:setFillColor(255, 100, 0)         clearRightBottom:setStrokeColor(0, 255, 0)         screenGroup:insert( clearRightBottom )         physics.addBody (clearRightBottom, "static")          end -------------------------------------------------------------------------------------- --------------------------------------------------------------ENTER SCENE---------- function scene:enterScene( event )     local group = self.view              buttonB = widget.newButton{         defaultFile="buttonHome.png",         onRelease = movePlace         }         buttonB.x = 500         buttonB.y = 705         group:insert ( buttonB )                  buttonNext = widget.newButton{         defaultFile="buttonNext.png",         onRelease = nextLevel         }         buttonNext.x = 900         buttonNext.y = 705         group:insert ( buttonNext )                          ladyBug = display.newImage ("butterflySmall.png")         ladyBug.x = 200         ladyBug.y = 600         physics.addBody (ladyBug, "dynamic", {density=3, friction=-0.7 , bounce = 0.5})         ladyBug:addEventListener ("touch", dragBody)                              end -------------------------------------------------------------------------------------- --------------------------------------------------------------EXIT SCENE---------- function scene:exitScene()          ladyBug:removeEventListener ("touch", dragBody)          display.remove ( ladyBug )          physics.stop()      end -------------------------------------------------------------------------------------- --------------------------------------------------------------DESTROY SCENE---------- function scene:destroyScene( event )         if buttonB then             buttonB:removeSelf()             buttonB = nil         end                  if buttonNext then             buttonNext:removeSelf()             buttonNext = nil         end          end --------------------------------------------------------------------------------- -- END OF YOUR IMPLEMENTATION --------------------------------------------------------------------------------- scene:addEventListener( "createScene", scene ) scene:addEventListener( "enterScene", scene ) scene:addEventListener( "exitScene", scene ) scene:addEventListener( "destroyScene", scene ) --------------------------------------------------------------------------------- return scene

That’s it. I know that even a simple app, can have a lot of things going on.

I’m learning a lot, but I know that I still need a long way to go.

I just want to thank you again, for taking the time to help me out with this app.

I hope apple approve it once it’s finished, and I will keep learning

to make better apps to help kids learn music in a nice way.

Thanks

Victor

You are trashing your own functions.  Look at this snippet pulled from home.lua:

local function buttonPlay()         storyboard.gotoScene( "play1", "crossFade", 500 )         return true end ...     buttonPlay = widget.newButton{         defaultFile="buttonPlayHomeRelease.png",         overFile="buttonPlayHomeOver.png",         onRelease = buttonPlay     }     buttonPlay.x = 240     buttonPlay.y = 377     buttonPlay:scale (.9, .9)  

In the main chunk you declare a local named “buttonPlay” which happens to be a function.

In createScene()  you overwrite the buttonPlay function with a widget.newButton named buttonPlay. 

These variables need to be different.  One variable name (and function names are variable names internally) can’t do two things at once.  Try adding the word Handler to the end of the function names and change your code to be more like:

local function buttonPlayHandler()         storyboard.gotoScene( "play1", "crossFade", 500 )         return true end ...     buttonPlay = widget.newButton{         defaultFile="buttonPlayHomeRelease.png",         overFile="buttonPlayHomeOver.png",         onRelease = buttonPlayHandler     }     buttonPlay.x = 240     buttonPlay.y = 377     buttonPlay:scale (.9, .9)

Then you won’t be overwriting your buttons. 

Rob, I think God gave you the wisdom to show me how to fix the problem.

I have about 70, files .lua I changed a few of them an it seems to work.

I change this

buttonLearnLevel1 = widget.newButton{         defaultFile="buttonBack.png",         onRelease = buttonLearnLevel1     }     buttonLearnLevel1.x = 100     buttonLearnLevel1.y = 670

for this line, onRelease I add the word handler to the function

buttonLearnLevel1 = widget.newButton{         defaultFile="buttonBack.png",         onRelease = buttonLearnLevel1Handler     }     buttonLearnLevel1.x = 100     buttonLearnLevel1.y = 670

I also put this

local buttonLearnLevel1

on top of the file, because I notice that in the createScene I had “buttonLearnLevel1” without the local

so I put it at the top. And I also change the function for to add the handler word

local function buttonLearnLevel1Handler()         storyboard.gotoScene( "home", "crossFade", 500 )         return true end

So I tried that and it seems to be working fine. I don’t want to say 100% perfect, but it’s working.

So it will take me time to change all 70 files, and I will let you know.

If it works, The app is finished.

Thank you, thank you one more time Rob.

Victor

Victor, I use a product called Lua Glider which is a commercial editor you can trial for 28 days. This editor allows you to search & replace stuff in all files in your project. I’m sure some others might do the same but I thought changing 70 files in one shot might be worth the few dollars you need to spend. You might even be able to do this during your trial period and not have to buy it but I bet you will like it. Have a look. http://www.mydevelopersgames.com/Glider/

PS, I’m not affiliated with the developer at all. Just a reasonably happy customer. 

Thank you Ksan. I did not know about it. But I finished all the 70 files, done!

And today is a happy day. The app WORKS!!!

I’m going to test it Saturday and Sunday, if all the buttons works, on Monday I will summit it.

Thanks you Rob, for all your help. Thanks to everyone who help me develop this app. I pray to God that apple will accept my app, please pray for me that that will happen.

I will let you know soon

Thanks

Victor

Congratulations. Good luck with your Apple review.

I’m stuck. (and sad)

in application loader I got an error, that know body seems to know how to fix that.

The application wrapper must end in .app /var/folders/kh/really long long numbers bla bla bla

I know, I’m really sad, and no where to turn to. All Monday morning since 8 A.M. wasted. (right now it’s 2:33 pm)

Let’s see tomorrow if I have any luck.

Victor

Can you try doing a object:removeBody() in your destoryScene?