Objects Still Present When Switching Scenes

In my menu for my game I am using a moving background, and a physics body just to make it move. I am using the same code as was provided by corona for the menu and level1. When it switches to level1, the physics bodies are still there and are interacting with the physics bodies I have in the actual scene, and the entire menu scene is behind my level1. Is there anything I am not doing that I need to add in? here is the code minus the objects and pictures Im using.

[lua]

local storyboard = require( “storyboard” )

local scene = storyboard.newScene()

– include Corona’s “widget” library

local widget = require “widget”


– forward declarations and other locals

local playBtn

– ‘onRelease’ event listener for playBtn

local function onPlayBtnRelease()

    

    – go to level1.lua scene

    storyboard.gotoScene( “level1”, “fade”, 500 )

    

    return true    – indicates successful touch

end


– BEGINNING OF YOUR IMPLEMENTATION

– 

– NOTE: Code outside of listener functions (below) will only be executed once,

–         unless storyboard.removeScene() is called.

– 


– Called when the scene’s view does not exist:

function scene:createScene( event )

    local group = self.view

    – create a widget button (which will loads level1.lua on release)

    playBtn = widget.newButton{

        --label=“Play Now”,

        --labelColor = { default={255}, over={128} },

        default=“playbutton1.png”,

        over=“playbutton2.png”,

        width=231, height=60,

        onRelease = onPlayBtnRelease    – event listener function

    }

    playBtn:setReferencePoint( display.CenterReferencePoint )

    playBtn.x = display.contentWidth*0.5

    playBtn.y = display.contentHeight - 545

    playBtn.xScale = 2.5

    playBtn.yScale = 2.5

    – all display objects must be inserted into group

    group:insert( background )

    group:insert( titleLogo )

    group:insert( playBtn )

    group:insert( face)

    group:insert( background2 )

    group:insert( background3 )

    group:insert( background4 )

    group:insert( floor )

    group:insert( floor2 )

end

– Called immediately after scene has moved onscreen:

function scene:enterScene( event )

    local group = self.view

    

    – INSERT code here (e.g. start timers, load audio, start listeners, etc.)

    

end

– Called when scene is about to move offscreen:

function scene:exitScene( event )

    local group = self.view

    – INSERT code here (e.g. stop timers, remove listenets, unload sounds, etc.)

    

end

– If scene’s view is removed, scene:destroyScene() will be called just prior to:

function scene:destroyScene( event )

    local group = self.view

    

    if playBtn then

        playBtn:removeSelf()    – widgets must be manually removed

        playBtn = nil

    end

end


– END OF YOUR IMPLEMENTATION


– “createScene” event is dispatched if scene’s view does not exist

scene:addEventListener( “createScene”, scene )

– “enterScene” event is dispatched whenever scene transition has finished

scene:addEventListener( “enterScene”, scene )

– “exitScene” event is dispatched whenever before next scene’s transition begins

scene:addEventListener( “exitScene”, scene )

– “destroyScene” event is dispatched before view is unloaded, which can be

– automatically unloaded in low memory situations, or explicitly via a call to

– storyboard.purgeScene() or storyboard.removeScene().

scene:addEventListener( “destroyScene”, scene )


return scene

[/lua]

i’m really newbee at corona sdk , but the problem you have is similar to one i got and solved this way.

don’t know if is the “Perfect Solution”   but is working for me.

When moving to other scene i remove all object created.

– Exit  Scene Function

function scene:exitScene(event)

    ball1:removeSelf()

    ball2:removeSelf()

    physics.stop()

    startbutt:removeEventListener ( “touch”, start )

    

end 

i’m really newbee at corona sdk , but the problem you have is similar to one i got and solved this way.

don’t know if is the “Perfect Solution”   but is working for me.

When moving to other scene i remove all object created.

– Exit  Scene Function

function scene:exitScene(event)

    ball1:removeSelf()

    ball2:removeSelf()

    physics.stop()

    startbutt:removeEventListener ( “touch”, start )

    

end 

Hi M4rqee…

I try – ball1:removeSelf () and it did not work. – MAYBE I’M doing it wrong. COULD YOU HELP ME PLEASE?


MY CODE IS THIS:

local storyboard = require( “storyboard” )
local widget = require “widget”
local scene = storyboard.newScene()
local wrongSound = audio.loadSound( “wrongSound.mp3”)

– --==******************[FUNCTIONS TO GO TO ANOTHER SCENE]**********************+±- –

local function buttonLearnLevel1()
        storyboard.gotoScene( “level1Q1”, “crossFade”, 1000 )    
    return true
end

– --==**************************[CREATE SCENE]**********************************+±- –

function scene:createScene( event )
    local group = self.view
    
    local background = display.newImage( “backgroundWrong1.png” )
    
    buttonLearnLevel1 = widget.newButton{
        defaultFile=“buttonWrong1release.png”,
        overFile=“buttonWrong1over.png”,
        onRelease = buttonLearnLevel1
    }
    buttonLearnLevel1.x = 890
    buttonLearnLevel1.y = 650
    
---------------------------------------------------------------------insert into group----
    
    group:insert ( background )
    group:insert ( buttonLearnLevel1 )
    
end

– --==***************************[ENTER SCENE]**********************************+±- –

function scene:enterScene( event )
    local group = self.view
    
    local littleBees = display.newImage (“littleBees.png”)
    littleBees.x = 28
    littleBees.y = 30
    transition.to(littleBees, {x=487, y=65, time=3000})
    
    group:insert ( littleBees )
    
    
    
end

– --==***************************[EXIT SCENE]**********************************+±- –

function scene:exitScene( event )
    local group = self.view

    littleBees:removeSelf()
    
end

– --==**************************[DESTROY SCENE]*********************************+±- –

function scene:destroyScene( event )
    local group = self.view    

    
    if buttonLearnLevel1 then
        buttonLearnLevel1:removeSelf()
        buttonLearnLevel1 = nil
    end
    
    if littleBees then
        littleBees:removeSelf()
        littleBees = nil
    end
        
    
end

– --==*************************[EVENT LISTENER]*********************************+±- –

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

return scene


I GOT AN ERROR

File: …mbarba/Desktop/Music Theory Game - CORONA/wrong1.lua
Line: 69

Attempt to index global ‘litttleBees’ (a nil value)


I try :

– storyboard.purgeOnSceneChange = true – I put this one on main.lua – DID NOT WORK

storyboard.purgeScene() – IT DID NOT WORK – I don’t know where to put this code, I try it in exitScene and nothing

PLEASE help me out, I’m still have the little bees showing when I return to the same scene, so the transition animation works

but I have a duplicate image of the littleBees image.

Thanks

Victor

 

You don’t need to manually remove objects as suggested by m4rqee, I’m not even sure you need to do that with widgets. As long as they are all inserted into group they will be cleaned up automatically by storyboard. You haven’t posted the code for your physics objects so I can’t see whether they’ve all been inserted.

I’m not using Physics. The code I post back there – THAT’S IT!

The littleBees is just an image .png that has a transition.to – point A to point B

The first time I see the Scene I see the .png “littleBees” move from point A to point B

When I come back to the same scene, The “littleBees” .png image IS THERE in the scene already, in point B.

Now a new .png image makes the transition.to and NOW I HAVE 2 images.

I have to RELAUNCH the program to reset everything.

I HAVE BEEN HAVING THIS PROBLEM SINCE LAST WEEK – 13 POST – 7 PEOPLE ANSWERED – NO ONE SEEMS TO KNOW HOW TO DO IT

I GUESS IT’S A GLITCH or something in CORONA because NOBODY KNOWS THE ANSWER

Victor

Just to make sure I understand the problem.

It is the physics objects hanging around, not the display objects.  In other words the graphic doesn’t show up, but if you turn on the hybrid debug mode you still see the physics bodies.

Both scenes  use physics.

Is this correct?

Just a reminder.  Please don’t hijack threads (that is change their direction from the topic the original poster posted about).  This is against forum rules.

Victor, your problem is this:

File: …mbarba/Desktop/Music Theory Game - CORONA/wrong1.lua
Line: 69
Attempt to index global ‘litttleBees’ (a nil value)

I’m suspecting that line 69 is in your exitScene() function, but basically you make littleBee’s “local” in enterScene() which means that variable only exists in enterScene and no where else in the code.  The fix for this is to put:

local littleBees

at the top of your module, probably after you require storyboard and then  take the “local” off in enterScene()

Hi Rob, thank you for everything you’re doing, I know that you don’t have to do anything for me and I appreciate the time you’re taking to help me, I am very sorry if I have not been the best person in this forum, sorry for that, I will be better.

I recorded a video that explain the problem I have, I hope you can watch this video and help me out, if you want.

I have no “physics” in my code, and all the objects or images work fine, but I have a duplicate image every time I go back to the same scene, please watch the video, I hope I can find the answer for this problem.

Thanks

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

Victor, there are two things you can do.  Do one or the other but not both.

1.  Create your notes, bees, spiders etc.  in createScene() and then in enterScene, before you do the transition, reset it’s X and Y to it’s starting location.  This is the most efficient way.

2.  In each of your scenes where you have code like this:

local function buttonLearnLevel1()
        storyboard.gotoScene( “level1Q1”, “crossFade”, 1000 )   
    return true
end

change it to:

local function buttonLearnLevel1()

        storyboard.purgeScene(“level1Q1”)
        storyboard.gotoScene( “level1Q1”, “crossFade”, 1000 )   
    return true
end

This way takes more work and your scenes get re-created each time.

Hi Rob. – Still is not working.

When I push the button “Please try again” it calls the function

 

local function buttonLearnLevel1()

        storyboard.purgeScene(“level1Q1”)
        storyboard.gotoScene( “level1Q1”, “crossFade”, 1000 )  
    return true
end

 

 

So it’s purging the scene (by the way, purging is “Cutting or erasing” correct?)

and then it’s goes to the scene.

so when I press the “please try again” button – everything else from there stops working

the buttons don’t work anymore

– What do you think I’m doing wrong

I change this:

local function buttonLearnLevel1()
        storyboard.gotoScene( “level1Q1”, “crossFade”, 1000 )  
    return true
end

 

For this:

 

local function buttonLearnLevel1()

        storyboard.purgeScene(“level1Q1”)
        storyboard.gotoScene( “level1Q1”, “crossFade”, 1000 )  
    return true
end

 

 

 

Thanks

 

Victor

Victor, do you have a thread for this topic? Let’s post there.  I don’t want to derail this from the question @willgomolka99 is trying to get answered.

 

at the end the best solution in found is to “delete” the previus scene when entering the one selected :

this is my LEVEL1.lua  code

i put in every lvl the  remove scene comand.

the object must be NOT declared local.

When you put objects into the group that is himself local 

they will be deleted on exit screen or when called the removescene “command”


LVL1.lua


display.setStatusBar ( display.HiddenStatusBar )

local physics = require(“physics”)

local storyboard = require (“storyboard”)

local scene = storyboard.newScene()

function scene:createScene(event)

    previous_scene = storyboard.getPrevious()

    storyboard.removeScene( previous_scene )

    screenGroup = self.view

-------------Creation of the level ----------------------------------------    

     Asse1 = display.newImage(“Asse480x30viti.png”)

     screenGroup:insert(Asse1)

     physics.addBody( Asse1,“static”, WOODDATA )

     Asse1.x = Asse1.contentWidth/2

     Asse1.y = display.contentHeight /2 -100

     Asse1.rotation = 0

     Asse1.alpha = 1

     Asse1.isSleepingAllowed = ITEMSTATUS

.

.

.

end

Rob. – Victor, do you have a thread for this topic?

I don’t know how to do a new thread.

I don’t know how to go to the right place in the forum.

I don’t know where I can find those answers.

I don’t know how to ask a question in the “correct way” – I don’t know which is the correct way.

I don’t know a lot of things…

I feel that I am not welcome in this forum anymore, I’m just trying to learn, but I guess I’m causing you a lot of inconvenience.

I am really sorry Rob.

Tell me if it’s still okay to try to learn in here. And please forgive me for everything.

Victor

Hi Victor.  To the contrary, we want you (and everyone) to feel welcome.  We want you to learn and we want to encourage you to be successful in your endeavors.  However, at the end of the day, this is an Internet Forum and there are certain rules and expected decorum that all community members need to abide by.   We have spelled out some specific rules in the Newbie forum in one of the posts that stays at the top:

http://forums.coronalabs.com/topic/32657-welcome-to-our-forums-please-read-forum-rules-and-guidelines/

There are other expected rules known as “Netiquette” or Network Etiquette.  One of those rules, which is why we are here is called hijacking. This is where you post on a message about X but you start talking about Y.  This is considered rude because the person who started the topic wants to talk about X.  It’s always best to create your own topics when you have a question, if it’s not directly related to an existing topic.   Click the “Start New Topic” button at the top left of the page from a form message list screen to start  your own message.

start_new_topic.png

Even for your thread about adding sounds to buttons, that thread by the end had nothing to do with adding sounds to buttons.  This makes it hard for people to find topics too.  Readers see really long threads and won’t read to the end.

So please post away.  Google “Netiquette” and read about it.  We don’t have these rules to be a pain to you or discourage you, but to foster a healthy community and encourage you to post in ways where more people will respond and be involved in helping solve your problems.

Thank you Rob. I read the guidelines. I will be a good member.

Hi M4rqee…

I try – ball1:removeSelf () and it did not work. – MAYBE I’M doing it wrong. COULD YOU HELP ME PLEASE?


MY CODE IS THIS:

local storyboard = require( “storyboard” )
local widget = require “widget”
local scene = storyboard.newScene()
local wrongSound = audio.loadSound( “wrongSound.mp3”)

– --==******************[FUNCTIONS TO GO TO ANOTHER SCENE]**********************+±- –

local function buttonLearnLevel1()
        storyboard.gotoScene( “level1Q1”, “crossFade”, 1000 )    
    return true
end

– --==**************************[CREATE SCENE]**********************************+±- –

function scene:createScene( event )
    local group = self.view
    
    local background = display.newImage( “backgroundWrong1.png” )
    
    buttonLearnLevel1 = widget.newButton{
        defaultFile=“buttonWrong1release.png”,
        overFile=“buttonWrong1over.png”,
        onRelease = buttonLearnLevel1
    }
    buttonLearnLevel1.x = 890
    buttonLearnLevel1.y = 650
    
---------------------------------------------------------------------insert into group----
    
    group:insert ( background )
    group:insert ( buttonLearnLevel1 )
    
end

– --==***************************[ENTER SCENE]**********************************+±- –

function scene:enterScene( event )
    local group = self.view
    
    local littleBees = display.newImage (“littleBees.png”)
    littleBees.x = 28
    littleBees.y = 30
    transition.to(littleBees, {x=487, y=65, time=3000})
    
    group:insert ( littleBees )
    
    
    
end

– --==***************************[EXIT SCENE]**********************************+±- –

function scene:exitScene( event )
    local group = self.view

    littleBees:removeSelf()
    
end

– --==**************************[DESTROY SCENE]*********************************+±- –

function scene:destroyScene( event )
    local group = self.view    

    
    if buttonLearnLevel1 then
        buttonLearnLevel1:removeSelf()
        buttonLearnLevel1 = nil
    end
    
    if littleBees then
        littleBees:removeSelf()
        littleBees = nil
    end
        
    
end

– --==*************************[EVENT LISTENER]*********************************+±- –

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

return scene


I GOT AN ERROR

File: …mbarba/Desktop/Music Theory Game - CORONA/wrong1.lua
Line: 69

Attempt to index global ‘litttleBees’ (a nil value)


I try :

– storyboard.purgeOnSceneChange = true – I put this one on main.lua – DID NOT WORK

storyboard.purgeScene() – IT DID NOT WORK – I don’t know where to put this code, I try it in exitScene and nothing

PLEASE help me out, I’m still have the little bees showing when I return to the same scene, so the transition animation works

but I have a duplicate image of the littleBees image.

Thanks

Victor

 

You don’t need to manually remove objects as suggested by m4rqee, I’m not even sure you need to do that with widgets. As long as they are all inserted into group they will be cleaned up automatically by storyboard. You haven’t posted the code for your physics objects so I can’t see whether they’ve all been inserted.

I’m not using Physics. The code I post back there – THAT’S IT!

The littleBees is just an image .png that has a transition.to – point A to point B

The first time I see the Scene I see the .png “littleBees” move from point A to point B

When I come back to the same scene, The “littleBees” .png image IS THERE in the scene already, in point B.

Now a new .png image makes the transition.to and NOW I HAVE 2 images.

I have to RELAUNCH the program to reset everything.

I HAVE BEEN HAVING THIS PROBLEM SINCE LAST WEEK – 13 POST – 7 PEOPLE ANSWERED – NO ONE SEEMS TO KNOW HOW TO DO IT

I GUESS IT’S A GLITCH or something in CORONA because NOBODY KNOWS THE ANSWER

Victor