How to make an image removeSelf on storyboard

I have a button in scene B.

OnPress it display an image,

How can I onRelease make that image removeSelf()?

also when I go to another scene, the image is still there, how can I get ride of that image?

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

Complete Code:


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

– --==******************[FUNCTIONS TO GO TO ANOTHER SCENE]**********************+±- –
local function buttonHome()
        storyboard.gotoScene( “sceneC”, “crossFade”, 1000 )    
    return true
end

local function playSound()

    local pinguin = display.newImage (“pinguin.png”)
        pinguin.x = 800
        pinguin.y = 400
        pinguin.alpha = 1

        audio.play(musicOn)
    
    return true
end

local function anotherFunction()

    local spider = display.newImage (“spiderSon.png”)
        spider.x = 100
        spider.y = 200

    
    return true
end
– --==**************************[CREATE SCENE]**********************************+±- –
function scene:createScene( event )
    local group = self.view
    
    local background = display.newImage( “backgroundLevel1Q4.png” )

    buttonHome = widget.newButton{
        defaultFile=“buttonHome.png”,
        onRelease = buttonHome
    }
    buttonHome.x = 522
    buttonHome.y = 655
    
    buttonDo = widget.newButton{
        defaultFile=“buttonDo.png”,
        onPress = playSound,
        onRelease = anotherFunction
    }
    buttonDo.x = 200
    buttonDo.y = 300    
---------------------------------------------------------------------insert into group----
    group:insert ( background )
    group:insert ( buttonHome )
    group:insert ( buttonDo )
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 buttonHome then
            buttonHome:removeSelf()
            buttonHome = nil
        end
        
        if buttonDo then
            buttonDo:removeSelf()
            buttonDo = nil
        end
end
– --==*************************[EVENT LISTENER]*********************************+±- –

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

return scene


Thank you for all your help.

Victor

If you have an image that you create in one scene and when you goto the next scene and the image is hanging around, its because you did not put it into the scene’s view (group).  You’re pinguin for example never gets put into group.  Of course if you try to put it in group there, it won’t work because that function doesn’t know about the scene view.

local function playSound()
    local group = scene.view

    local pinguin = display.newImage (“pinguin.png”)

    group:insert(pinguin)

        pinguin.x = 800
        pinguin.y = 400
        pinguin.alpha = 1
        audio.play(musicOn)
        return true
end

local function anotherFunction()
    local group = scene.view
    local spider = display.newImage (“spiderSon.png”)

    group:insert(spider)
        spider.x = 100
        spider.y = 200
    return true
end

Hi Rob. Thank you very much, this solve one of the problems and it works! Thank you again.

Now I know that I have to insert the image right after I display the image, it works!

Now…

When I go scene C, then go back to scene A, and again to scene B (where the pinguin is)

I expected the scene to do the same thing, but the pinguin is already there. So there is no point in pressing the button to display the pinguin, because de pinguin is there.

I have to refresh the app (command+R) for the scene to go normal.

I remember you said in one of the post, – I purge my scenes before I go to them.

I did not really understand that, and also, I don’t know exactly where I have to write that line of code. And which of these I have to use and why?

– storyboard.removeAll( )

– storyboard.removeScene( )

– storyboard.purgeScene( )

Thnaks

Victor

P.S. I’m almost done with my app. I just need this to work and the transition.to still is not working.

Actually if the code you posted above is your live code, you are not purging before you goto scene:

[lua]

local function buttonHome()
        storyboard.gotoScene( “sceneC”, “crossFade”, 1000 )   
    return true
end

[/lua]

to purge you would do:

[lua]

local function buttonHome()

        storyboard.purgeScene(“sceneC”)

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

[/lua]

Great! it works… yes!

Thank you Rob, thank you very much, you’re the best!

Now… one more little problem. I have 4 files – main.lua –  sceneA.lua – sceneB.lua – sceneC.lua

main.lua


display.setStatusBar( display.HiddenStatusBar )
local storyboard = require “storyboard”
storyboard.gotoScene( “sceneA” )


sceneA.lua – because I’m going to scene B


local function buttonHome()
        storyboard.purgeScene(“sceneB”)
        storyboard.gotoScene( “sceneB”, “crossFade”, 1000 )    
    return true
end


scene B – this is because I’m going to scene C


local function buttonHome()
        storyboard.purgeScene(“sceneC”)
        storyboard.gotoScene( “sceneC”, “crossFade”, 1000 )    
    return true
end


scene C –  now I’m going to scene A again to repeat the program. or make a cycle.


local function buttonHome()
        storyboard.purgeScene(“sceneA”)
        storyboard.gotoScene( “sceneA”, “crossFade”, 1000 )    
    return true
end


And the rest of the code is like the one above.


When I run the app…

sceneA – works great – transition is good and the image is gone after I leave the scene

sceneB – works great – I press the button and the pinguin is visible, and it’s gone once I go to another scene

sceneC – perfect, I have the new animation and the image is gone when I go to A

For the second time

sceneA – works great, perfect …

until, I press the button to go to scene B the app STOPS!

nothing happens, I press the button and nothing, the app is dead.

What do you think is the problem?

Victor

Are you getting any errors in your console log?

Hi Rob.

I don’t know what’s a “console log”.

Where do I find it?

How do I use it?

Victor

If you’re on a Mac do you start a terminal window that starts up Corona?  If so, your debugging output and errors will be in that terminal Window.  I think on the PC it starts up a command window where  messages are written.

Rob. The problem is gone!

Someone named “pavement907” in youtube tolde me the answer.

I’m sharing it here for other people with the same problem.

I had to put

local ladyBug – at the top of my program


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


Then in the enterScene


function scene:enterScene( event )
    local group = self.view
    
    ladyBug = display.newImage( “ladyBug.png” ) – There is no “local” at the beginning of ladyBug = display etc…
    ladyBug.x = 100
    ladyBug.y = 500
    transition.to(ladyBug, {x=700 , y=500, time=1000})
    
    group:insert ( ladyBug )        
end


And at the end on the exitScene


function scene:exitScene( event )
    local group = self.view   
    display.remove(ladyBug)  ------------ I added this line
end

-------------------------------------------------------------------------That’s it----------------

And it works, it does work! Yes!, I’m so happy!

now I can finish my app…I think.

Thank you pavement907.

And thanks to all of you for helping me.

Victor

If you have an image that you create in one scene and when you goto the next scene and the image is hanging around, its because you did not put it into the scene’s view (group).  You’re pinguin for example never gets put into group.  Of course if you try to put it in group there, it won’t work because that function doesn’t know about the scene view.

local function playSound()
    local group = scene.view

    local pinguin = display.newImage (“pinguin.png”)

    group:insert(pinguin)

        pinguin.x = 800
        pinguin.y = 400
        pinguin.alpha = 1
        audio.play(musicOn)
        return true
end

local function anotherFunction()
    local group = scene.view
    local spider = display.newImage (“spiderSon.png”)

    group:insert(spider)
        spider.x = 100
        spider.y = 200
    return true
end

Hi Rob. Thank you very much, this solve one of the problems and it works! Thank you again.

Now I know that I have to insert the image right after I display the image, it works!

Now…

When I go scene C, then go back to scene A, and again to scene B (where the pinguin is)

I expected the scene to do the same thing, but the pinguin is already there. So there is no point in pressing the button to display the pinguin, because de pinguin is there.

I have to refresh the app (command+R) for the scene to go normal.

I remember you said in one of the post, – I purge my scenes before I go to them.

I did not really understand that, and also, I don’t know exactly where I have to write that line of code. And which of these I have to use and why?

– storyboard.removeAll( )

– storyboard.removeScene( )

– storyboard.purgeScene( )

Thnaks

Victor

P.S. I’m almost done with my app. I just need this to work and the transition.to still is not working.

Actually if the code you posted above is your live code, you are not purging before you goto scene:

[lua]

local function buttonHome()
        storyboard.gotoScene( “sceneC”, “crossFade”, 1000 )   
    return true
end

[/lua]

to purge you would do:

[lua]

local function buttonHome()

        storyboard.purgeScene(“sceneC”)

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

[/lua]

Great! it works… yes!

Thank you Rob, thank you very much, you’re the best!

Now… one more little problem. I have 4 files – main.lua –  sceneA.lua – sceneB.lua – sceneC.lua

main.lua


display.setStatusBar( display.HiddenStatusBar )
local storyboard = require “storyboard”
storyboard.gotoScene( “sceneA” )


sceneA.lua – because I’m going to scene B


local function buttonHome()
        storyboard.purgeScene(“sceneB”)
        storyboard.gotoScene( “sceneB”, “crossFade”, 1000 )    
    return true
end


scene B – this is because I’m going to scene C


local function buttonHome()
        storyboard.purgeScene(“sceneC”)
        storyboard.gotoScene( “sceneC”, “crossFade”, 1000 )    
    return true
end


scene C –  now I’m going to scene A again to repeat the program. or make a cycle.


local function buttonHome()
        storyboard.purgeScene(“sceneA”)
        storyboard.gotoScene( “sceneA”, “crossFade”, 1000 )    
    return true
end


And the rest of the code is like the one above.


When I run the app…

sceneA – works great – transition is good and the image is gone after I leave the scene

sceneB – works great – I press the button and the pinguin is visible, and it’s gone once I go to another scene

sceneC – perfect, I have the new animation and the image is gone when I go to A

For the second time

sceneA – works great, perfect …

until, I press the button to go to scene B the app STOPS!

nothing happens, I press the button and nothing, the app is dead.

What do you think is the problem?

Victor

Are you getting any errors in your console log?

Hi Rob.

I don’t know what’s a “console log”.

Where do I find it?

How do I use it?

Victor

If you’re on a Mac do you start a terminal window that starts up Corona?  If so, your debugging output and errors will be in that terminal Window.  I think on the PC it starts up a command window where  messages are written.

Rob. The problem is gone!

Someone named “pavement907” in youtube tolde me the answer.

I’m sharing it here for other people with the same problem.

I had to put

local ladyBug – at the top of my program


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


Then in the enterScene


function scene:enterScene( event )
    local group = self.view
    
    ladyBug = display.newImage( “ladyBug.png” ) – There is no “local” at the beginning of ladyBug = display etc…
    ladyBug.x = 100
    ladyBug.y = 500
    transition.to(ladyBug, {x=700 , y=500, time=1000})
    
    group:insert ( ladyBug )        
end


And at the end on the exitScene


function scene:exitScene( event )
    local group = self.view   
    display.remove(ladyBug)  ------------ I added this line
end

-------------------------------------------------------------------------That’s it----------------

And it works, it does work! Yes!, I’m so happy!

now I can finish my app…I think.

Thank you pavement907.

And thanks to all of you for helping me.

Victor