Return to the previous scene

I don’t see any:  sceneGroup:insert()'s in your scene:create() in escenaPelota.lua.

All of those display.newRect, display.newImage, display.newImageRect() calls need inserted into sceneGroup or they need to be inserted in another group and have that group inserted into sceneGroup. If not they are unmanaged and sit on the top of everything else.

Rob

Great that was the problem!!

Thanks again Rob :slight_smile:

What  do you mean it is not working for me?

Simply you can use code 

composer.gotoScene("scene1")

Maybe put button in scene2 scene which leads to menu or main scene.

Also read about similar problems on forum

https://forums.coronalabs.com/topic/60383-change-to-game-over-screen/

http://stackoverflow.com/questions/26884342/failed-to-back-to-menu-from-game-over-score-unable-to-display-on-game-over-scr

ldurniat

Thanks for your answer!!

To sum up, from scene1 when I do

[lua]composer.gotoScene(“scene2”)[/lua]

It works perfect.

But from scene2 when I do

[lua]composer.gotoScene(“scene1”)[/lua]

It doesn’t work.

Any idea?

Thanks again!!

What do you mean by “It doesn’t work”? Are you getting an error? Please describe the problem. Copy and Pasted the complete error message from the console log Window Corona opens. Look at the file and line numbers referenced and share the code around those lines pointing out the line where the problem is.

Thanks,

Rob

No error, it just doesn’t work.

Do I have to destry “scene1” before?

DETALIS + CODE:

[lua]-- START SCENE –
local composer = require (“composer”)
local scene = composer.newScene()

– code …

local function chanceScene()
    composer.gotoScene(“ballSCENE”, {effect = “slideLeft”, time = 1000})
end

– show()
function scene:show(event)

    local sceneGroup = self.view
    local phase = event.phase

    if ( phase == “will” ) then
        – Code here runs when the scene is still off screen (but is about to come on screen)
        
    elseif ( phase == “did” ) then
            
            – more code …    
            sceneGroup:insert(ball)
            ball:addEventListener(“tap”,chanceScene)
            
    end
end
– END START SCENE –

– BALL SCENE –
local composer = require (“composer”)
local scene = composer.newScene()

local function chanceScene()
    – HERE IS WHERE I SHOULD RETURN TO THE PREVIOUS SCENE, RIGHT?
    composer.gotoScene(“start”, {effect = “slideRight”, time = 1000})
end

local function endGame()
    ballB:addEventListener(“collision”,soundBall)
    ballB:removeSelf()
    chanceScene()
end

– code …

local function onLocalCollision(self,event)
    if (event.phase == “began” ) then
    
            – code …
            
              if (event.other.ID == “ballB”) then
                endGame()
             end
        end
    end
end

– show()
function scene:show( event )

    local sceneGroup = self.view
    local phase = event.phase

    if ( phase == “will” ) then
        – Code here runs when the scene is still off screen (but is about to come on screen)
        
    elseif ( phase == “did” ) then

        – code …
        
        barritaColision.collision = onLocalCollision
    end
end
– END BALL SCENE --[/lua]

Thanks!!!

Forgive me for pushing on this, but there has to be a condition that differentiates between works and doesn’t work. What are you expecting to happen? What is really happening?

Does it go to a blank screen?

Is that the complete code?

OK, what I need to do is, once the ball enter in a basket, the scene should return to the previous one (I mean, from the ballScene return to the start scene).

Once the ball collides with the basket, it stays in the same scene, and once I tap on it, it gives me the error below:

“ballScene.lua:380: attempt to call method “addEventListener” (a nil value)
stack tracebak:
        ballScene.lua:380: in function <ballScene.lua:369>
        ?: in function “dispatchEvent”
        …”

This is not the full code of course, but I believe is the only lines that makes me doubt.

Thanks!!

Is line 380:  

ball:addEventListener("tap",chanceScene)

???

If so, the error is saying that ball isn’t a display object. I’m assuming you are creating ball in scene:create() and you probably have “local” at the beginning. When you put local in front of a variable/object name inside a function like scene:create() you’re making it only visible to that function and it’s children. This is a programming term called “Scope”.  scene:show() can’t see things that are local to scene:create(). The solution is to pre-declare ball at the top:

local ball

And then in scene:create() leave off the local since you want access to the object in multiple functions scene wide.

Rob

Thanks again Rob for you help.

No, the 380 line code is:

[lua]ball:addEventListener(“touch”,moveBall)[/lua]

inside the “function scene:show”

and the “moveBall” function is:

[lua]function moveBall(event)
    local pelota = event.target
    pelota:applyLinearImpulse(0,-0.1,event.x,event.y)
end[/lua]

Okay, it’s the same situation as I described.

Rob

I already have the object “ball” declared outside the scene:create() function.

I can copy the full code here, but I have some variables in spanish.

Culd I put it any way?

Thanks!

Regards.

Thats fine

OK!

main.lua code:

[lua]–oculta la barra de arriba
display.setStatusBar(display.HiddenStatusBar)

local composer = require (“composer”)
composer.gotoScene(“start”, {effect = “fade”, time = 500})[/lua]

start.lua code:

[lua]–oculta la barra de arriba
display.setStatusBar(display.HiddenStatusBar)

local composer = require (“composer”)
local scene = composer.newScene()

local fondo
local pelotaGirando

local function cambiarEscena()
    composer.gotoScene(“escenaPelota”, {effect = “slideLeft”, time = 1000})
end

local sonidoPelotaCayendo = audio.loadStream( “sonidoambienteCortado.ogg”)
audio.play(sonidoPelotaCayendo, { channel=2, loops=0})
audio.setVolume(1)

– create()
function scene:create( event )

    local sceneGroup = self.view
    – Code here runs when the scene is first created but has not yet appeared on screen
            
    fondo = display.newImageRect( “Imagenes/fondoAro1.png”,320,480)
    fondo.x = 160
    fondo.y = 240
    sceneGroup:insert(fondo)
end

– show()
function scene:show(event)

    local sceneGroup = self.view
    local phase = event.phase

    if ( phase == “will” ) then
        – Code here runs when the scene is still off screen (but is about to come on screen)
        
    elseif ( phase == “did” ) then
            
            pelotaGirando = display.newImageRect(“Imagenes/pelotaBasketIntroConLogo2.png”, 50,50)
            pelotaGirando.x = 160
            pelotaGirando.y = 290

            transition.to( pelotaGirando, { xScale = 6.25, yScale = 6.25, rotation = 360 * 6, time = 2000} )
            
            sceneGroup:insert(pelotaGirando)

            pelotaGirando:addEventListener(“tap”,cambiarEscena)
            
    end
end

– hide()
function scene:hide( event )

    local sceneGroup = self.view
    local phase = event.phase

    if ( phase == “will” ) then
        – Code here runs when the scene is on screen (but is about to go off screen)

    elseif ( phase == “did” ) then
        – Code here runs immediately after the scene goes entirely off screen

    end
end

– destroy()
function scene:destroy( event )

    local sceneGroup = self.view
    – Code here runs prior to the removal of scene’s view
    
    composer.removeScene(“start”)

end


– Scene event function listeners


scene:addEventListener( “create”, scene )
scene:addEventListener( “show”, scene )
scene:addEventListener( “hide”, scene )
scene:addEventListener( “destroy”, scene )


return scene[/lua]

escenaPelota.lua code:

[lua]–oculta la barra de arriba
display.setStatusBar(display.HiddenStatusBar)

local composer = require (“composer”)
local scene = composer.newScene()

local fisica

–fisica.setDrawMode(“hybrid”)

– Fondo –
local fondo
– Fondo –

– limites de la pantalla –
local piso
local techo
local paredIzq
local paredDer
– limites de la pantalla –

– Pelota Basket –
local pelotaB
– Pelota Basket –

– Pelotas Futbol –
local pelotaFutbol1
local pelotaFutbol2
local pelotaFutbol3
local pelotaFutbol4
local pelotaFutbol5
local pelotaFutbol6
local pelotaFutbol7
local pelotaFutbol8
local pelotaFutbol9
local pelotaFutbol10
– Pelotas Futbol –

– Aro –
local aro
– Aro –

– Barra vertical a la izquierda del aro –
local barraV
– Barra vertical a la izquierda del aro –

– Barra horizontal bajo el aro –
local barraH
– Barra horizontal bajo el aro –

– Barrita colision dentro del aro –
local barritaColision
– Barrita colision dentro del aro –

– Sonidos –
local sonidoDeFondo
– Sonidos –

local function sonidoPelotaFutbol()
    media.playEventSound(“sonidopelota.wav”)
end

function moverPelotaBasket(event)
    local pelota = event.target
    pelota:applyLinearImpulse(0,-0.1,event.x,event.y)
end

local function moverPelotaFutbol(event)
    local pelota2 = event.target
    pelota2:applyLinearImpulse(0,-0.1,event.x,event.y)
end

local function sonidoPelotaBasket()
    media.playEventSound(“sonidopelotabasket.ogg”)
end

local function sonidoEmbocaPelotaFutbolError()
    media.playEventSound(“sonidoerrorpelotafutbol.ogg”)
end

local function sonidoEmbocaPelotaBasket()
    media.playEventSound(“pelotaBencestada.ogg”)
end

local function cambiarEscena()
    composer.gotoScene(“start”, {effect = “slideRight”, time = 1000})
end

local function borrarPelotaFutbol1()
 if (pelotaFutbol1 ~= nil) then
    pelotaFutbol1:addEventListener(“collision”,sonidoEmbocaPelotaFutbolError)
     pelotaFutbol1:removeSelf()
 end
end

local function borrarPelotaFutbol2()
 if (pelotaFutbol2 ~= nil) then
    pelotaFutbol2:addEventListener(“collision”,sonidoEmbocaPelotaFutbolError)
     pelotaFutbol2:removeSelf()
 end
end

local function borrarPelotaFutbol3()
 if (pelotaFutbol3 ~= nil) then
    pelotaFutbol3:addEventListener(“collision”,sonidoEmbocaPelotaFutbolError)
    pelotaFutbol3:removeSelf()
 end
end

local function borrarPelotaFutbol4()
 if (pelotaFutbol4 ~= nil) then
    pelotaFutbol4:addEventListener(“collision”,sonidoEmbocaPelotaFutbolError)
    pelotaFutbol4:removeSelf()
 end
end

local function borrarPelotaFutbol5()
 if (pelotaFutbol5 ~= nil) then
    pelotaFutbol5:addEventListener(“collision”,sonidoEmbocaPelotaFutbolError)
    pelotaFutbol5:removeSelf()
 end
end

local function borrarPelotaFutbol6()
 if (pelotaFutbol6 ~= nil) then
    pelotaFutbol6:addEventListener(“collision”,sonidoEmbocaPelotaFutbolError)
    pelotaFutbol6:removeSelf()
 end
end

local function borrarPelotaFutbol7()
 if (pelotaFutbol7 ~= nil) then
    pelotaFutbol7:addEventListener(“collision”,sonidoEmbocaPelotaFutbolError)
    pelotaFutbol7:removeSelf()
 end
end

local function borrarPelotaFutbol8()
 if (pelotaFutbol8 ~= nil) then
    pelotaFutbol18:addEventListener(“collision”,sonidoEmbocaPelotaFutbolError)
    pelotaFutbol8:removeSelf()
 end
end

local function borrarPelotaFutbol9()
 if (pelotaFutbol9 ~= nil) then
    pelotaFutbol9:addEventListener(“collision”,sonidoEmbocaPelotaFutbolError)
    pelotaFutbol9:removeSelf()
 end
end

local function borrarPelotaFutbol10()
 if (pelotaFutbol10 ~= nil) then
    pelotaFutbol10:addEventListener(“collision”,sonidoEmbocaPelotaFutbolError)
    pelotaFutbol10:removeSelf()
 end
end

local function finDelJuego()
    pelotaB:addEventListener(“collision”,sonidoEmbocaPelotaBasket)
    pelotaB:removeSelf()
    --handleButtonEvent()
    cambiarEscena()
end

local function onLocalCollision(self,event)
    if (event.phase == “began” ) then
        if (self.ID == “barrita”)
        then if (event.other.ID == “pelota1”) then
                borrarPelotaFutbol1()
             end
             if (event.other.ID == “pelota2”) then
                borrarPelotaFutbol2()
             end
             if (event.other.ID == “pelota3”) then
                borrarPelotaFutbol3()
             end
             if (event.other.ID == “pelota4”) then
                borrarPelotaFutbol4()
             end
             if (event.other.ID == “pelota5”) then
                borrarPelotaFutbol5()
             end
             if (event.other.ID == “pelota6”) then
                borrarPelotaFutbol6()
             end
             if (event.other.ID == “pelota7”) then
                borrarPelotaFutbol7()
             end
             if (event.other.ID == “pelota8”) then
                borrarPelotaFutbol8()
             end
             if (event.other.ID == “pelota9”) then
                borrarPelotaFutbol9()
             end
             if (event.other.ID == “pelota10”) then
                borrarPelotaFutbol10()
             end
              if (event.other.ID == “pelotaB”) then
                finDelJuego()
                --handleButtonEvent(event)
             end
        end
    end
end

– FUNCIONES DE Escena-- create()
function scene:create( event )

    local sceneGroup = self.view
    – Code here runs when the scene is first created but has not yet appeared on screen
    
    fisica = require(“physics”)
    fisica.start()

    fisica.setGravity(0,7)

    --fisica.setDrawMode(“hybrid”)
    
    fondo = display.newImageRect(“Imagenes/fondo3.jpg”,320,480, {onEvent = handleButtonEvent})
    fondo.x = 160
    fondo.y = 240
    
    piso = display.newRect(0,480,1000,0)
    techo = display.newRect(0,0,1000,0)
    paredIzq = display.newRect(0,0,0,1000)
    paredDer = display.newRect(320,0,0,1000)
    fisica.addBody(piso,“static”,{friction = 1.0})
    fisica.addBody(techo,“static”,{friction = 1.0})
    fisica.addBody(paredIzq,“static”,{friction = 1.0})
    fisica.addBody(paredDer,“static”,{friction = 1.0})
    
    pelotaB = display.newImageRect(“Imagenes/pelotabasket.png”,70,70)
    pelotaB.x = 160
    pelotaB.y = 300

    fisica.addBody(pelotaB, {bounce = 0.5, radius = 35, friction = 1.0})
    
    – Pelota Futbol 1 –
    pelotaFutbol1 = display.newImageRect(“Imagenes/pelotafutbol.png”,60,60)
    pelotaFutbol1.x = 30
    pelotaFutbol1.y = 30
    fisica.addBody(pelotaFutbol1, {radius = 30, friction = 0.1, bounce = 0.5})
    – Pelota Futbol 1 –

    – Pelota Futbol 2 –
    pelotaFutbol2 = display.newImageRect(“Imagenes/pelotafutbol.png”,60,60)
    pelotaFutbol2.x = 90
    pelotaFutbol2.y = 30
    fisica.addBody(pelotaFutbol2, {radius = 30, friction = 0.1, bounce = 0.5})
    – Pelota Futbol 2 –

    – Pelota Futbol 3 –
    pelotaFutbol3 = display.newImageRect(“Imagenes/pelotafutbol.png”,60,60)
    pelotaFutbol3.x = 30
    pelotaFutbol3.y = 90
    fisica.addBody(pelotaFutbol3, {radius = 30, friction = 0.1, bounce = 0.5})
    – Pelota Futbol 3 –

    – Pelota Futbol 4 –
    pelotaFutbol4 = display.newImageRect(“Imagenes/pelotafutbol.png”,60,60)
    pelotaFutbol4.x = 90
    pelotaFutbol4.y = 90
    fisica.addBody(pelotaFutbol4, {radius = 30, friction = 0.1, bounce = 0.5})
    – Pelota Futbol 4 –

    – Pelota Futbol 5 –
    pelotaFutbol5 = display.newImageRect(“Imagenes/pelotafutbol.png”,60,60)
    pelotaFutbol5.x = 30
    pelotaFutbol5.y = 150
    fisica.addBody(pelotaFutbol5, {radius = 30, friction = 0.1, bounce = 0.5})
    – Pelota Futbol 5 –

    – Pelota Futbol 6 –
    pelotaFutbol6 = display.newImageRect(“Imagenes/pelotafutbol.png”,60,60)
    pelotaFutbol6.x = 90
    pelotaFutbol6.y = 150
    fisica.addBody(pelotaFutbol6, {radius = 30, friction = 0.1, bounce = 0.5})
    – Pelota Futbol 6 –

    – Pelota Futbol 7 –
    pelotaFutbol7 = display.newImageRect(“Imagenes/pelotafutbol.png”,60,60)
    pelotaFutbol7.x = 30
    pelotaFutbol7.y = 210
    fisica.addBody(pelotaFutbol7, {radius = 30, friction = 0.1, bounce = 0.5})
    – Pelota Futbol 7 –

    – Pelota Futbol 8 –
    pelotaFutbol8 = display.newImageRect(“Imagenes/pelotafutbol.png”,60,60)
    pelotaFutbol8.x = 90
    pelotaFutbol8.y = 210
    fisica.addBody(pelotaFutbol8, {radius = 30, friction = 0.1, bounce = 0.5})
    – Pelota Futbol 8 –

    – Pelota Futbol 9 –
    pelotaFutbol9 = display.newImageRect(“Imagenes/pelotafutbol.png”,60,60)
    pelotaFutbol9.x = 30
    pelotaFutbol9.y = 270
    fisica.addBody(pelotaFutbol9, {radius = 30, friction = 0.1, bounce = 0.5})
    – Pelota Futbol 9 –

    – Pelota Futbol 10 –
    pelotaFutbol10 = display.newImageRect(“Imagenes/pelotafutbol.png”,60,60)
    pelotaFutbol10.x = 90
    pelotaFutbol10.y = 270
    fisica.addBody(pelotaFutbol10, {radius = 30, friction = 0.1, bounce = 0.5})
    – Pelota Futbol 10 –

    – Aro –
    aro = display.newImageRect(“Imagenes/aro.png”,100,80)
    aro.x = 270
    aro.y = 160
    – Aro –

    – Barra vertical a la izquierda del aro –
    barraV = display.newImageRect(“Imagenes/barraV.png”,2,80)
    barraV.x = 220
    barraV.y = 160
    barraV.isVisible = false
    fisica.addBody(barraV,“static”,{friction = 1.0})
    – Barra vertical a la izquierda del aro –
    
    – Barra horizontal bajo el aro –
    barraH = display.newImageRect(“Imagenes/barraH.png”,80,2)
    barraH.x = 260
    barraH.y = 200
    barraH.isVisible = false
    fisica.addBody(barraH,“static”,{friction = 1.0})
    – Barra horizontal bajo el aro –
    
    – Barrita colision dentro del aro –
    barritaColision = display.newImageRect(“Imagenes/barritacolision.png”,60,2)
    barritaColision.x = 270
    barritaColision.y = 190
    barritaColision.isVisible = false
    fisica.addBody(barritaColision,{friction = 1.0})
    – Barrita colision dentro del aro –
    
    sonidoDeFondo = audio.loadStream( “sonidoambiente.ogg”)
    audio.play(sonidoDeFondo, { channel=1, loops=-1})
    audio.setVolume(0.1)

    barritaColision.ID = “barrita”

    pelotaFutbol1.ID = “pelota1”
    pelotaFutbol2.ID = “pelota2”
    pelotaFutbol3.ID = “pelota3”
    pelotaFutbol4.ID = “pelota4”
    pelotaFutbol5.ID = “pelota5”
    pelotaFutbol6.ID = “pelota6”
    pelotaFutbol7.ID = “pelota7”
    pelotaFutbol8.ID = “pelota8”
    pelotaFutbol9.ID = “pelota9”
    pelotaFutbol10.ID = “pelota10”
    pelotaB.ID = “pelotaB”
        
end

– show()
function scene:show( event )

    local sceneGroup = self.view
    local phase = event.phase

    if ( phase == “will” ) then
        – Code here runs when the scene is still off screen (but is about to come on screen)
        
    elseif ( phase == “did” ) then
        – Code here runs when the scene is entirely on screen
        
        pelotaB:addEventListener(“touch”,moverPelotaBasket)
                
        pelotaFutbol1:addEventListener(“touch”,moverPelotaFutbol)
        pelotaFutbol2:addEventListener(“touch”,moverPelotaFutbol)
        pelotaFutbol3:addEventListener(“touch”,moverPelotaFutbol)
        pelotaFutbol4:addEventListener(“touch”,moverPelotaFutbol)
        pelotaFutbol5:addEventListener(“touch”,moverPelotaFutbol)
        pelotaFutbol6:addEventListener(“touch”,moverPelotaFutbol)
        pelotaFutbol7:addEventListener(“touch”,moverPelotaFutbol)
        pelotaFutbol8:addEventListener(“touch”,moverPelotaFutbol)
        pelotaFutbol9:addEventListener(“touch”,moverPelotaFutbol)
        pelotaFutbol10:addEventListener(“touch”,moverPelotaFutbol)
        
        pelotaFutbol1:addEventListener(“touch”,sonidoPelotaFutbol)
        pelotaFutbol2:addEventListener(“touch”,sonidoPelotaFutbol)
        pelotaFutbol3:addEventListener(“touch”,sonidoPelotaFutbol)
        pelotaFutbol4:addEventListener(“touch”,sonidoPelotaFutbol)
        pelotaFutbol5:addEventListener(“touch”,sonidoPelotaFutbol)
        pelotaFutbol6:addEventListener(“touch”,sonidoPelotaFutbol)
        pelotaFutbol7:addEventListener(“touch”,sonidoPelotaFutbol)
        pelotaFutbol8:addEventListener(“touch”,sonidoPelotaFutbol)
        pelotaFutbol9:addEventListener(“touch”,sonidoPelotaFutbol)
        pelotaFutbol10:addEventListener(“touch”,sonidoPelotaFutbol)
        
        pelotaB:addEventListener(“touch”,sonidoPelotaBasket)

        barritaColision:addEventListener(“collision”,barritaColision)
        
        barritaColision.collision = onLocalCollision
                
        --pelotaGirando:addEventListener(“postCollision”,cambiarEscena)
        --Runtime:addEventListener( “postCollision”, escenaAnterior )
    end
end

– hide()
function scene:hide( event )

    local sceneGroup = self.view
    local phase = event.phase

    if ( phase == “will” ) then
        – Code here runs when the scene is on screen (but is about to go off screen)

    elseif ( phase == “did” ) then
        – Code here runs immediately after the scene goes entirely off screen
        
        --pelotaB:removeEventListener(“touch”,moverPelotaBasket)
                
        --pelotaFutbol1:removeEventListener(“touch”,moverPelotaFutbol)
        --pelotaFutbol2:removeEventListener(“touch”,moverPelotaFutbol)
        --pelotaFutbol3:removeEventListener(“touch”,moverPelotaFutbol)
        --pelotaFutbol4:removeEventListener(“touch”,moverPelotaFutbol)
        --pelotaFutbol5:removeEventListener(“touch”,moverPelotaFutbol)
        --pelotaFutbol6:removeEventListener(“touch”,moverPelotaFutbol)
        --pelotaFutbol7:removeEventListener(“touch”,moverPelotaFutbol)
        --pelotaFutbol8:removeEventListener(“touch”,moverPelotaFutbol)
        --pelotaFutbol9:removeEventListener(“touch”,moverPelotaFutbol)
        --pelotaFutbol10:removeEventListener(“touch”,moverPelotaFutbol)
        
        --pelotaFutbol1:removeEventListener(“touch”,sonidoPelotaFutbol)
        --pelotaFutbol2:removeEventListener(“touch”,sonidoPelotaFutbol)
        --pelotaFutbol3:removeEventListener(“touch”,sonidoPelotaFutbol)
        --pelotaFutbol4:removeEventListener(“touch”,sonidoPelotaFutbol)
        --pelotaFutbol5:removeEventListener(“touch”,sonidoPelotaFutbol)
        --pelotaFutbol6:removeEventListener(“touch”,sonidoPelotaFutbol)
        --pelotaFutbol7:removeEventListener(“touch”,sonidoPelotaFutbol)
        --pelotaFutbol8:removeEventListener(“touch”,sonidoPelotaFutbol)
        --pelotaFutbol9:removeEventListener(“touch”,sonidoPelotaFutbol)
        --pelotaFutbol10:removeEventListener(“touch”,sonidoPelotaFutbol)
        
        --pelotaB:removeEventListener(“touch”,sonidoPelotaBasket)

        --barritaColision:removeEventListener(“collision”,barritaColision)
            

    end
end

– destroy()
function scene:destroy( event )

    local sceneGroup = self.view
    – Code here runs prior to the removal of scene’s view

end


– Scene event function listeners


scene:addEventListener( “create”, scene )
scene:addEventListener( “show”, scene )
scene:addEventListener( “hide”, scene )
scene:addEventListener( “destroy”, scene )


return scene[/lua]

Note: The game is simple: one opening scene that goes to the balls scene, where I have 100 soccer balls and 1 basket ball.

There is a basket and every ball can enter in there, but only the basket ball is the one that, once it enters in the basket, it goes to the opening scene again.

Thanks!

I’m guessing when you were posting code above you were trying to translate it into English? Because I don’t see where you’re creating an object called “ball”?  Also line 380 is a commented out line that is trying to remove a listener, so somethings are not adding up.  It’s really hard for me to troubleshoot the problem if I’m not seeing the exact code and error messages.

Now all that said, in your restart scene you should change:
 

local function cambiarEscena() composer.gotoScene("escenaPelota", {effect = "slideLeft", time = 1000}) end

to

local function cambiarEscena() composer.removeScene("escenaPelota") composer.gotoScene("escenaPelota", {effect = "slideLeft", time = 1000}) end

Rob

Thanks again Rob!

The “removeScene” works but I cannot move to the previous scene anyway.

I just translated the code to english. Please check the code translated below:

main.lua

[lua]display.setStatusBar(display.HiddenStatusBar)

local composer = require (“composer”)
composer.gotoScene(“start”, {effect = “fade”, time = 500})[/lua]

start.lua

[lua]display.setStatusBar(display.HiddenStatusBar)

local composer = require (“composer”)
local scene = composer.newScene()

local backGround
local rollingBall

local function cambiarEscena()
    composer.gotoScene(“ballScene”, {effect = “slideLeft”, time = 1000})
end

local soundFallingBall = audio.loadStream( “sonidoambienteCortado.ogg”)
audio.play(soundFallingBall, { channel=2, loops=0})
audio.setVolume(1)

– create()
function scene:create( event )

    local sceneGroup = self.view
    – Code here runs when the scene is first created but has not yet appeared on screen
            
    backGround = display.newImageRect( “Imagenes/backGroundAro1.png”,320,480)
    backGround.x = 160
    backGround.y = 240
    sceneGroup:insert(backGround)
end

– show()
function scene:show(event)

    local sceneGroup = self.view
    local phase = event.phase

    if ( phase == “will” ) then
        – Code here runs when the scene is still off screen (but is about to come on screen)
        
    elseif ( phase == “did” ) then
            
            rollingBall = display.newImageRect(“Imagenes/pelotaBasketIntroConLogo2.png”, 50,50)
            rollingBall.x = 160
            rollingBall.y = 290

            transition.to( rollingBall, { xScale = 6.25, yScale = 6.25, rotation = 360 * 6, time = 2000} )
            
            sceneGroup:insert(rollingBall)

            rollingBall:addEventListener(“tap”,cambiarEscena)
            
    end
end

– hide()
function scene:hide( event )

    local sceneGroup = self.view
    local phase = event.phase

    if ( phase == “will” ) then
        – Code here runs when the scene is on screen (but is about to go off screen)

    elseif ( phase == “did” ) then
        – Code here runs immediately after the scene goes entirely off screen

    end
end

– destroy()
function scene:destroy( event )

    local sceneGroup = self.view
    – Code here runs prior to the removal of scene’s view
    
    composer.removeScene(“start”)

end


– Scene event function listeners


scene:addEventListener( “create”, scene )
scene:addEventListener( “show”, scene )
scene:addEventListener( “hide”, scene )
scene:addEventListener( “destroy”, scene )


return scene[/lua]

ballScene.lua

[lua]-- Ball Scene

display.setStatusBar(display.HiddenStatusBar)

local composer = require (“composer”)
local scene = composer.newScene()

local fisica

–fisica.setDrawMode(“hybrid”)

– backGround –
local backGround
– backGround –

– screen limits
local floor
local roof
local wallLeft
local wallRight
– screen limits

– ball Basket –
local ballB
– ball Basket –

– soccer balls –
local soccerBall1
local soccerBall2
local soccerBall3
local soccerBall4
local soccerBall5
local soccerBall6
local soccerBall7
local soccerBall8
local soccerBall9
local soccerBall10
– soccer balls

– basket –
local basket
– basket –

– vertical
local barV
– vertical

– horizontal
local barH
– horizontal

– littlebar collision dentro del basket –
local littlebarcollision
– littlebar collision dentro del basket –

– sounds –
local soundBackGround
– sounds –

local function soundsoccerBall()
    media.playEventSound(“soundball.wav”)
end

function moveBasket_Ball(event)
    local ball = event.target
    ball:applyLinearImpulse(0,-0.1,event.x,event.y)
end

local function moveSoccerBall(event)
    local ball2 = event.target
    ball2:applyLinearImpulse(0,-0.1,event.x,event.y)
end

local function soundBasket_Ball()
    media.playEventSound(“soundBasket_Ball.ogg”)
end

local function soundSoccerBallError()
    media.playEventSound(“sounderrorsoccerBall.ogg”)
end

local function soundBasket_BallIn()
    media.playEventSound(“ballBencestada.ogg”)
end

local function changeScene()
    composer.removeScene(“ballScene”)
    composer.gotoScene(“start”, {effect = “slideRight”, time = 1000})
end

local function removeSoccerBall1()
 if (soccerBall1 ~= nil) then
    soccerBall1:addEventListener(“collision”,soundSoccerBallError)
     soccerBall1:removeSelf()
 end
end

local function removeSoccerBall2()
 if (soccerBall2 ~= nil) then
    soccerBall2:addEventListener(“collision”,soundSoccerBallError)
     soccerBall2:removeSelf()
 end
end

local function removeSoccerBall3()
 if (soccerBall3 ~= nil) then
    soccerBall3:addEventListener(“collision”,soundSoccerBallError)
    soccerBall3:removeSelf()
 end
end

local function removeSoccerBall4()
 if (soccerBall4 ~= nil) then
    soccerBall4:addEventListener(“collision”,soundSoccerBallError)
    soccerBall4:removeSelf()
 end
end

local function removeSoccerBall5()
 if (soccerBall5 ~= nil) then
    soccerBall5:addEventListener(“collision”,soundSoccerBallError)
    soccerBall5:removeSelf()
 end
end

local function removeSoccerBall6()
 if (soccerBall6 ~= nil) then
    soccerBall6:addEventListener(“collision”,soundSoccerBallError)
    soccerBall6:removeSelf()
 end
end

local function removeSoccerBall7()
 if (soccerBall7 ~= nil) then
    soccerBall7:addEventListener(“collision”,soundSoccerBallError)
    soccerBall7:removeSelf()
 end
end

local function removeSoccerBall8()
 if (soccerBall8 ~= nil) then
    soccerBall18:addEventListener(“collision”,soundSoccerBallError)
    soccerBall8:removeSelf()
 end
end

local function removeSoccerBall9()
 if (soccerBall9 ~= nil) then
    soccerBall9:addEventListener(“collision”,soundSoccerBallError)
    soccerBall9:removeSelf()
 end
end

local function removeSoccerBall10()
 if (soccerBall10 ~= nil) then
    soccerBall10:addEventListener(“collision”,soundSoccerBallError)
    soccerBall10:removeSelf()
 end
end

local function endGame()
    ballB:addEventListener(“collision”,soundBasket_BallIn)
    ballB:removeSelf()
    changeScene()
end

local function onLocalCollision(self,event)
    if (event.phase == “began” ) then
        if (self.ID == “littlebar”)
        then if (event.other.ID == “ball1”) then
                removeSoccerBall1()
             end
             if (event.other.ID == “ball2”) then
                removeSoccerBall2()
             end
             if (event.other.ID == “ball3”) then
                removeSoccerBall3()
             end
             if (event.other.ID == “ball4”) then
                removeSoccerBall4()
             end
             if (event.other.ID == “ball5”) then
                removeSoccerBall5()
             end
             if (event.other.ID == “ball6”) then
                removeSoccerBall6()
             end
             if (event.other.ID == “ball7”) then
                removeSoccerBall7()
             end
             if (event.other.ID == “ball8”) then
                removeSoccerBall8()
             end
             if (event.other.ID == “ball9”) then
                removeSoccerBall9()
             end
             if (event.other.ID == “ball10”) then
                removeSoccerBall10()
             end
              if (event.other.ID == “ballB”) then
                endGame()
             end
        end
    end
end

– create()
function scene:create( event )

    local sceneGroup = self.view
    – Code here runs when the scene is first created but has not yet appeared on screen
    
    fisica = require(“physics”)
    fisica.start()

    fisica.setGravity(0,7)

    --fisica.setDrawMode(“hybrid”)
    
    backGround = display.newImageRect(“Imagenes/backGround3.jpg”,320,480)
    backGround.x = 160
    backGround.y = 240
    
    floor = display.newRect(0,480,1000,0)
    roof = display.newRect(0,0,1000,0)
    wallLeft = display.newRect(0,0,0,1000)
    wallRight = display.newRect(320,0,0,1000)
    fisica.addBody(floor,“static”,{friction = 1.0})
    fisica.addBody(roof,“static”,{friction = 1.0})
    fisica.addBody(wallLeft,“static”,{friction = 1.0})
    fisica.addBody(wallRight,“static”,{friction = 1.0})
    
    ballB = display.newImageRect(“Imagenes/ballbasket.png”,70,70)
    ballB.x = 160
    ballB.y = 300

    fisica.addBody(ballB, {bounce = 0.5, radius = 35, friction = 1.0})
    
    – ball Futbol 1 –
    soccerBall1 = display.newImageRect(“Imagenes/soccerBall.png”,60,60)
    soccerBall1.x = 30
    soccerBall1.y = 30
    fisica.addBody(soccerBall1, {radius = 30, friction = 0.1, bounce = 0.5})
    – ball Futbol 1 –

    – ball Futbol 2 –
    soccerBall2 = display.newImageRect(“Imagenes/soccerBall.png”,60,60)
    soccerBall2.x = 90
    soccerBall2.y = 30
    fisica.addBody(soccerBall2, {radius = 30, friction = 0.1, bounce = 0.5})
    – ball Futbol 2 –

    – ball Futbol 3 –
    soccerBall3 = display.newImageRect(“Imagenes/soccerBall.png”,60,60)
    soccerBall3.x = 30
    soccerBall3.y = 90
    fisica.addBody(soccerBall3, {radius = 30, friction = 0.1, bounce = 0.5})
    – ball Futbol 3 –

    – ball Futbol 4 –
    soccerBall4 = display.newImageRect(“Imagenes/soccerBall.png”,60,60)
    soccerBall4.x = 90
    soccerBall4.y = 90
    fisica.addBody(soccerBall4, {radius = 30, friction = 0.1, bounce = 0.5})
    – ball Futbol 4 –

    – ball Futbol 5 –
    soccerBall5 = display.newImageRect(“Imagenes/soccerBall.png”,60,60)
    soccerBall5.x = 30
    soccerBall5.y = 150
    fisica.addBody(soccerBall5, {radius = 30, friction = 0.1, bounce = 0.5})
    – ball Futbol 5 –

    – ball Futbol 6 –
    soccerBall6 = display.newImageRect(“Imagenes/soccerBall.png”,60,60)
    soccerBall6.x = 90
    soccerBall6.y = 150
    fisica.addBody(soccerBall6, {radius = 30, friction = 0.1, bounce = 0.5})
    – ball Futbol 6 –

    – ball Futbol 7 –
    soccerBall7 = display.newImageRect(“Imagenes/soccerBall.png”,60,60)
    soccerBall7.x = 30
    soccerBall7.y = 210
    fisica.addBody(soccerBall7, {radius = 30, friction = 0.1, bounce = 0.5})
    – ball Futbol 7 –

    – ball Futbol 8 –
    soccerBall8 = display.newImageRect(“Imagenes/soccerBall.png”,60,60)
    soccerBall8.x = 90
    soccerBall8.y = 210
    fisica.addBody(soccerBall8, {radius = 30, friction = 0.1, bounce = 0.5})
    – ball Futbol 8 –

    – ball Futbol 9 –
    soccerBall9 = display.newImageRect(“Imagenes/soccerBall.png”,60,60)
    soccerBall9.x = 30
    soccerBall9.y = 270
    fisica.addBody(soccerBall9, {radius = 30, friction = 0.1, bounce = 0.5})
    – ball Futbol 9 –

    – ball Futbol 10 –
    soccerBall10 = display.newImageRect(“Imagenes/soccerBall.png”,60,60)
    soccerBall10.x = 90
    soccerBall10.y = 270
    fisica.addBody(soccerBall10, {radius = 30, friction = 0.1, bounce = 0.5})
    – ball Futbol 10 –

    – basket –
    basket = display.newImageRect(“Imagenes/basket.png”,100,80)
    basket.x = 270
    basket.y = 160
    – basket –

    – bar vertical a la izquierda del basket –
    barV = display.newImageRect(“Imagenes/barV.png”,2,80)
    barV.x = 220
    barV.y = 160
    barV.isVisible = false
    fisica.addBody(barV,“static”,{friction = 1.0})
    – bar vertical a la izquierda del basket –
    
    – bar horizontal bajo el basket –
    barH = display.newImageRect(“Imagenes/barH.png”,80,2)
    barH.x = 260
    barH.y = 200
    barH.isVisible = false
    fisica.addBody(barH,“static”,{friction = 1.0})
    – bar horizontal bajo el basket –
    
    – littlebar collision dentro del basket –
    littlebarcollision = display.newImageRect(“Imagenes/littlebarcollision.png”,60,2)
    littlebarcollision.x = 270
    littlebarcollision.y = 190
    littlebarcollision.isVisible = false
    fisica.addBody(littlebarcollision,{friction = 1.0})
    – littlebar collision dentro del basket –
    
    soundBackGround = audio.loadStream( “soundambiente.ogg”)
    audio.play(soundBackGround, { channel=1, loops=-1})
    audio.setVolume(0.1)

    littlebarcollision.ID = “littlebar”

    soccerBall1.ID = “ball1”
    soccerBall2.ID = “ball2”
    soccerBall3.ID = “ball3”
    soccerBall4.ID = “ball4”
    soccerBall5.ID = “ball5”
    soccerBall6.ID = “ball6”
    soccerBall7.ID = “ball7”
    soccerBall8.ID = “ball8”
    soccerBall9.ID = “ball9”
    soccerBall10.ID = “ball10”
    ballB.ID = “ballB”
        
end

– show()
function scene:show( event )

    local sceneGroup = self.view
    local phase = event.phase

    if ( phase == “will” ) then
        – Code here runs when the scene is still off screen (but is about to come on screen)
        
    elseif ( phase == “did” ) then
        – Code here runs when the scene is entirely on screen
        
        ballB:addEventListener(“touch”,moveBasket_Ball)
                
        soccerBall1:addEventListener(“touch”,moveSoccerBall)
        soccerBall2:addEventListener(“touch”,moveSoccerBall)
        soccerBall3:addEventListener(“touch”,moveSoccerBall)
        soccerBall4:addEventListener(“touch”,moveSoccerBall)
        soccerBall5:addEventListener(“touch”,moveSoccerBall)
        soccerBall6:addEventListener(“touch”,moveSoccerBall)
        soccerBall7:addEventListener(“touch”,moveSoccerBall)
        soccerBall8:addEventListener(“touch”,moveSoccerBall)
        soccerBall9:addEventListener(“touch”,moveSoccerBall)
        soccerBall10:addEventListener(“touch”,moveSoccerBall)
        
        soccerBall1:addEventListener(“touch”,soundsoccerBall)
        soccerBall2:addEventListener(“touch”,soundsoccerBall)
        soccerBall3:addEventListener(“touch”,soundsoccerBall)
        soccerBall4:addEventListener(“touch”,soundsoccerBall)
        soccerBall5:addEventListener(“touch”,soundsoccerBall)
        soccerBall6:addEventListener(“touch”,soundsoccerBall)
        soccerBall7:addEventListener(“touch”,soundsoccerBall)
        soccerBall8:addEventListener(“touch”,soundsoccerBall)
        soccerBall9:addEventListener(“touch”,soundsoccerBall)
        soccerBall10:addEventListener(“touch”,soundsoccerBall)
        
        ballB:addEventListener(“touch”,soundBasket_Ball)

        littlebarcollision:addEventListener(“collision”,littlebarcollision)
        
        littlebarcollision.collision = onLocalCollision
    end
end

– hide()
function scene:hide( event )

    local sceneGroup = self.view
    local phase = event.phase

    if ( phase == “will” ) then
        – Code here runs when the scene is on screen (but is about to go off screen)

    elseif ( phase == “did” ) then
        – Code here runs immediately after the scene goes entirely off screen
        
        --ballB:removeEventListener(“touch”,moveBasket_Ball)
                
        --soccerBall1:removeEventListener(“touch”,moveSoccerBall)
        --soccerBall2:removeEventListener(“touch”,moveSoccerBall)
        --soccerBall3:removeEventListener(“touch”,moveSoccerBall)
        --soccerBall4:removeEventListener(“touch”,moveSoccerBall)
        --soccerBall5:removeEventListener(“touch”,moveSoccerBall)
        --soccerBall6:removeEventListener(“touch”,moveSoccerBall)
        --soccerBall7:removeEventListener(“touch”,moveSoccerBall)
        --soccerBall8:removeEventListener(“touch”,moveSoccerBall)
        --soccerBall9:removeEventListener(“touch”,moveSoccerBall)
        --soccerBall10:removeEventListener(“touch”,moveSoccerBall)
        
        --soccerBall1:removeEventListener(“touch”,soundsoccerBall)
        --soccerBall2:removeEventListener(“touch”,soundsoccerBall)
        --soccerBall3:removeEventListener(“touch”,soundsoccerBall)
        --soccerBall4:removeEventListener(“touch”,soundsoccerBall)
        --soccerBall5:removeEventListener(“touch”,soundsoccerBall)
        --soccerBall6:removeEventListener(“touch”,soundsoccerBall)
        --soccerBall7:removeEventListener(“touch”,soundsoccerBall)
        --soccerBall8:removeEventListener(“touch”,soundsoccerBall)
        --soccerBall9:removeEventListener(“touch”,soundsoccerBall)
        --soccerBall10:removeEventListener(“touch”,soundsoccerBall)
        
        --ballB:removeEventListener(“touch”,soundBasket_Ball)

        --littlebarcollision:removeEventListener(“collision”,littlebarcollision)
            

    end
end

– destroy()
function scene:destroy( event )

    local sceneGroup = self.view
    – Code here runs prior to the removal of scene’s view

end


– Scene event function listeners


scene:addEventListener( “create”, scene )
scene:addEventListener( “show”, scene )
scene:addEventListener( “hide”, scene )
scene:addEventListener( “destroy”, scene )


return scene[/lua]

I really appreciate that you or someone else could read until here :smiley:

I just took a quick look at this for you, so this is just a guess but try this :

‘cut’ line 56 in start.lua  

and ‘paste’ it in ballScene.lua  at line 317

then you should be able to return to start.lua

Good Luck

Bob

Thanks to all of you for your help!

Now, with the latest tip, I was able to “listen” the first scene (start.lua), but I’m still seeing the “ballScene” on the screen :frowning:

Do I have to remove the “ballScene” perhaps?

Thanks!!

You have to add your display objects to Composer’s “view” group if you want Composer to manage them.  In scene:create() this means:

sceneGroup:insert( soccerBall1 )

For what it’s worth, it would be better to see your real code rather than translated code. It will match your error messages.

Rob