Removing "native" objects when using "storyboard"

this is my complete code Alan


– --==***************************************************************************+±- –
– Music Theory Game
– 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 wrongSound = audio.loadSound( “wrongSound.mp3”)

– --==******************[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( “level1Q6”, “crossFade”, 1000 )    
    return true
end

local function buttonRound4()
        storyboard.gotoScene( “wrong1”, “crossFade”, 1000 )
        audio.play(wrongSound)
    return true
end

local function buttonRound5()
        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( “backgroundLevel1Q5.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=“button2Counts.png”,
        onRelease = buttonRound1
    }
    buttonRound1.x = 199
    buttonRound1.y = 312
    
    buttonRound2 = widget.newButton{
        defaultFile=“button1Count.png”,
        onRelease = buttonRound2
    }
    buttonRound2.x = 700
    buttonRound2.y = 432
    
    buttonRound3 = widget.newButton{
        defaultFile=“button4Counts.png”,
        onRelease = buttonRound3
    }
    buttonRound3.x = 800
    buttonRound3.y = 300
    
    buttonRound4 = widget.newButton{
        defaultFile=“button3Counts.png”,
        onRelease = buttonRound4
    }
    buttonRound4.x = 200
    buttonRound4.y = 600
    
    buttonRound5 = widget.newButton{
        defaultFile=“buttonHalfCount.png”,
        onRelease = buttonRound5
    }
    buttonRound5.x = 323
    buttonRound5.y = 444
---------------------------------------------------------------------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 )
    group:insert ( buttonRound5 )
    
end

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

function scene:enterScene( event )
    local group = self.view
    
    local quarterNote = display.newImage( “wholeNote.png” )
    quarterNote.x = 390
    quarterNote.y = 532
    transition.to(quarterNote, {x=987 , y=532, time=5000})
    
    group:insert ( quarterNote )
end

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

function scene:exitScene( event )
    local group = self.view
    
    
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
        
        if buttonRound5 then
            buttonRound5:removeSelf()
            buttonRound5 = nil
        end
end

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

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

return scene


in the enterScene I have the image display. and this scene is – level 5 or level5.lua

you can see the transition.to point A to point B without any problems and everything works fine.

so I keep playing and going to another level level 6, level 7, level 8 but when I come back to level 5 …

As soon as I see the scene, the image (quarter note) is there already in point B

and a new image quarter note start to move and going from point A to point B, and you can see two images.

I would like the scene to start normal like the first time, every time.

I hope this will help you understand my problem. And I hope that you can help me.

Thank you Alan.

Victor

Unfortunately, that didn’t work for me. I am trying to remove a native map from a scene using: 

function scene:exitScene(event) local group = self.view if myMap then myMap:removeSelf() myMap = nil end end

– I am a newbie in Corona (Lua) , so I need your help.Thanks in advance!

Try replacing:

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

with:

if myMap then myMap.parent:remove(myMap) myMap = nil end

and see if that helps.

Are you getting any errors, or is it just not working? It could be that you’ve made the object local to the function where it is created, and so exitScene sees it as nil.

Thank you Alan I know where was the problem now.

in my enterScene I have this

function scene:enterScene( event )     local group = self.view     local quarterNote = display.newImage( "wholeNote.png" )     quarterNote.x = 390     quarterNote.y = 532     transition.to(quarterNote, {x=987 , y=532, time=5000})     group:insert ( quarterNote ) end

and in my exitScene I had nothing. I just had to add this in my exitScene

function scene:exitScene( event )     local group = self.view     display.remove (quarterNote) end

And that fix the problem, I don’t know if it’s the best way, or the correct way, but it did work.

I hope this helps someone else also.

Thanks Alan.

Victor

PS – My app is “Waiting for review” status, I hope they approve it.

 Thanks Alan for the reply. I’m afraid that also didn’t work for me… the map is still visible when I switch scenes. No, I didn’t get any errors. And yeah, I get your point about the “local” to the function. Is there any other way?.. I am really seeking help for this, well, thanks in advance!

@9.SemperFidels

I’ll need a bit more information to help you out. You said you get my point about the mapview being local to the function, but not whether it is local in your code.

Before I see any code, try this:

At the top of your code, BEFORE your createScene, enterScene etc functions write this:

local myMap

Just that, not “myMap = native.newMapView” or whatever it is.

Then in the place you currently create the map view, if it has the word ‘local’ before it, remove the word ‘local’. This will allocate your mapView to the previously defined variable called myMap, and make it visible to all functions in that lua file.

Then hopefully, your removal code should work as it is. 

If that doesn’t work, then you will definitely need to post some more of your code, as something else is wrong.

Unfortunately, that didn’t work for me. I am trying to remove a native map from a scene using: 

function scene:exitScene(event) local group = self.view if myMap then myMap:removeSelf() myMap = nil end end

– I am a newbie in Corona (Lua) , so I need your help.Thanks in advance!

Try replacing:

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

with:

if myMap then myMap.parent:remove(myMap) myMap = nil end

and see if that helps.

Are you getting any errors, or is it just not working? It could be that you’ve made the object local to the function where it is created, and so exitScene sees it as nil.

Thank you Alan I know where was the problem now.

in my enterScene I have this

function scene:enterScene( event )     local group = self.view     local quarterNote = display.newImage( "wholeNote.png" )     quarterNote.x = 390     quarterNote.y = 532     transition.to(quarterNote, {x=987 , y=532, time=5000})     group:insert ( quarterNote ) end

and in my exitScene I had nothing. I just had to add this in my exitScene

function scene:exitScene( event )     local group = self.view     display.remove (quarterNote) end

And that fix the problem, I don’t know if it’s the best way, or the correct way, but it did work.

I hope this helps someone else also.

Thanks Alan.

Victor

PS – My app is “Waiting for review” status, I hope they approve it.

 Thanks Alan for the reply. I’m afraid that also didn’t work for me… the map is still visible when I switch scenes. No, I didn’t get any errors. And yeah, I get your point about the “local” to the function. Is there any other way?.. I am really seeking help for this, well, thanks in advance!

@9.SemperFidels

I’ll need a bit more information to help you out. You said you get my point about the mapview being local to the function, but not whether it is local in your code.

Before I see any code, try this:

At the top of your code, BEFORE your createScene, enterScene etc functions write this:

local myMap

Just that, not “myMap = native.newMapView” or whatever it is.

Then in the place you currently create the map view, if it has the word ‘local’ before it, remove the word ‘local’. This will allocate your mapView to the previously defined variable called myMap, and make it visible to all functions in that lua file.

Then hopefully, your removal code should work as it is. 

If that doesn’t work, then you will definitely need to post some more of your code, as something else is wrong.

It’s incredible how things work in a few months.

In May 10 I post “I have this problem of removing a .png image on my scene and NO ONE KNOWS HOW TO DO IT.”

I was frustrated, very sad, and maybe mad.

I put all in caps, even Rob told me that was wrong (Thanks Rob)

I was very upset.

I kept working, and studying, and reading, and practicing, and making a lot of errors. And now I read this again,

today is July 02.

I have one app in the app store,

I’m working on my second app,

I read this post and I understand the concept of removing an object much better than on May.

Thanks to all of you in the community, you help me a lot, and I don’t have words to tell you

how grateful I am.

The reason why I’m writing this, is for those of you who are like I was 2 months ago.

Don’t give up, never. The people on the other side of the computer in the Corona Family

are the best, if you really want to learn, they will help you all the way, just keep learning

Thanks to all my friends in Corona

Congratulations on releasing your first app!  

thanks I have sold about 60 already!

Wonderful news!!! And I am very happy to see you helping other beginners now. Thats the spirit. Wishing you all the success in your new app.

It’s incredible how things work in a few months.

In May 10 I post “I have this problem of removing a .png image on my scene and NO ONE KNOWS HOW TO DO IT.”

I was frustrated, very sad, and maybe mad.

I put all in caps, even Rob told me that was wrong (Thanks Rob)

I was very upset.

I kept working, and studying, and reading, and practicing, and making a lot of errors. And now I read this again,

today is July 02.

I have one app in the app store,

I’m working on my second app,

I read this post and I understand the concept of removing an object much better than on May.

Thanks to all of you in the community, you help me a lot, and I don’t have words to tell you

how grateful I am.

The reason why I’m writing this, is for those of you who are like I was 2 months ago.

Don’t give up, never. The people on the other side of the computer in the Corona Family

are the best, if you really want to learn, they will help you all the way, just keep learning

Thanks to all my friends in Corona

Congratulations on releasing your first app!  

thanks I have sold about 60 already!

Wonderful news!!! And I am very happy to see you helping other beginners now. Thats the spirit. Wishing you all the success in your new app.