Event Listener being removed when reloading the scene.

Hi guys, I’m using the composer API to build my latest app.

Anyway, when I click the “replay button”, the scene itself gets reloaded as it should, but every EventListener gets removed, or at least I think, as -for instance- the background images won’t scroll anymore, or every single button doesn’t perform any action.

What can I do?

I’ll post some code in case you need it.

Thanks in advantage.

Yep, gonna need code.  Usually people have issues not removing listeners and such.

Rob

Thanks. For instance, in the following code, when I reload the scene, the event listener associated to “jumpBTN” dissapear.

local composer = require( "composer" ) local scene = composer.newScene() display.setStatusBar(display.HiddenStatusBar) \_W = display.contentWidth \_H = display.contentHeight local timeSprite = 800 local velocita = 1 local playerHasJumped = 0 require("physics") physics.start() physics.setDrawMode("normal") physics.setGravity( 0,140) local rect = display.newRect(\_W/2,\_H-40,\_W,100) rect:setFillColor(255,0,0) rect.myName = "rect" physics.addBody( rect, "static", { friction=0.5, bounce=0 } ) local sheet1 = graphics.newImageSheet( "immagini/sprite.png", { width=135, height=259, numFrames=14 } ) local instance1 = display.newSprite( sheet1, { name="cat", start=1, count=14, time=550 } ) instance1.x = \_W/2-400 instance1.y = \_H/2+180 instance1.myName = "instance1" instance1:play() physics.addBody( instance1, "dynamic", { friction=0.5, bounce=0 } ) local jumpBTN = display.newImage("immagini/jump.png"); jumpBTN.x = \_W/2-900; jumpBTN.y = \_H/2; jumpBTN.alpha = 0.01 local replayBTN = display.newImage("immagini/replayBTN.png"); replayBTN.x = \_W/2+200; replayBTN.y = \_H/2; replayBTN.xScale = 0.07; replayBTN.yScale = 0.07; replayBTN.alpha = 0 --replay local function replay() replayBTN.alpha = 0 composer.gotoScene("game") end replayBTN:addEventListener("touch", replay) local function up(event) if(event.phase == "began" and playerHasJumped == 0) then instance1:setLinearVelocity(0, -1700) instance1:pause() playerHasJumped = 1 elseif(event.phase == "began" and playerHasJumped == 1) then instance1:setLinearVelocity(0, -1000) playerHasJumped = 2 end end jumpBTN:addEventListener("touch",up) --collisioni local function onCollision( event ) if ( event.phase == "began" ) then playerHasJumped = 0 instance1:play() elseif ( event.phase == "ended" ) then print( "ended: " .. event.object1.myName .. " and " .. event.object2.myName ) end end Runtime:addEventListener( "collision", onCollision ) -- "scene:create()" function scene:create( event ) local sceneGroup = self.view sceneGroup:insert(rect) sceneGroup:insert(instance1) sceneGroup:insert(replayBTN) end -- "scene:show()" function scene:show( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then elseif ( phase == "did" ) then end end -- "scene:hide()" function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then instance1:pause() elseif ( phase == "did" ) then end end -- "scene:destroy()" function scene:destroy( event ) local sceneGroup = self.view rect:removeSelf() instance1:removeSelf() replayBTN:removeSelf() end -- ------------------------------------------------------------------------------- -- Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) -- ------------------------------------------------------------------------------- return scene

I would move lines 20-35 inside your scene:create() function.  Since you are trying to reference these items inside other functions, you will want to declare them local at the top:

local rect, instance1, jumpBTN, replayBTN

Then move lines: 43 55, and 67 into create() after the lines 20-35.

You don’t need lines 109-111.

You probably should have your instance1:play() in your scene:show() function’s “did” phase.  You probably also want to start physics there too (in addition to the physics.start() at the top).  You should probably pause or stop physics in the scene:hide()'s will phase.

Rob

Thank you so much Rob, but I haven’t understood what do I have to insert inside my “scene:create()” function. What do you you mean with “Since you are trying to reference these items inside other functions, you will want to declare them local at the top”

Do I have to insert

local rect = display.newRect(\_W/2,\_H-40,\_W,100) rect:setFillColor(255,0,0) rect.myName = "rect" physics.addBody( rect, "static", { friction=0.5, bounce=0 } ) bla bla bla...

 inside scene:create() or at the top BEFORE scene:create()?

Thanks again

Ahh ok, now I got it, anyway here’s my new code, I’ve added few things. I’ve inserted a pause button, that works when the scene is reloaded, but the jump button still won’t perform any action (it should works when “gameIsPaused” is equal to 0, but it just works the first time I run the app). In addition to that, everytime I replay the scene, the background speed increases, but if I try to print “velocita” it returns ‘1’ (which is correct, while the background scrolling speed gets faster time by time)

local composer = require( "composer" ) local scene = composer.newScene() display.setStatusBar(display.HiddenStatusBar) \_W = display.contentWidth \_H = display.contentHeight require("physics") physics.start() physics.setDrawMode("normal") physics.setGravity( 0,140) local timeSprite = 800 local velocita = 1 local playerHasJumped = 0 local gameIsPaused = 0 local jumpBTN = display.newImage("immagini/jump.png"); jumpBTN.x = \_W/2-900; jumpBTN.y = \_H/2; jumpBTN.alpha = 0.01 local pauseBTN = display.newImage("immagini/pauseBTN.png"); pauseBTN.x = \_W/2+660; pauseBTN.y = \_H/2-360; pauseBTN.xScale = 0.2; pauseBTN.yScale = 0.2 local replayBTN = display.newImage("immagini/replayBTN.png"); replayBTN.x = \_W/2+200; replayBTN.y = \_H/2; replayBTN.xScale = 0.07; replayBTN.yScale = 0.07; replayBTN.alpha = 0 local playBTN = display.newImage("immagini/playBtn.png"); playBTN.x = \_W/2; playBTN.y = \_H/2; playBTN.xScale = 1; playBTN.yScale = 1; playBTN.alpha = 0 local menuBTN = display.newImage("immagini/menuBtn.png"); menuBTN.x = \_W/2-200; menuBTN.y = \_H/2; menuBTN.xScale = 0.2; menuBTN.yScale = 0.2; menuBTN.alpha = 0 --background local bg1 = display.newImage("immagini/bg1.png"); bg1.x = \_W/2; bg1.y = \_H/2 local bg2 = display.newImage("immagini/bg2.png"); bg2.x = \_W/2+1423; bg2.y = \_H/2 local rect = display.newRect(\_W/2,\_H-40,\_W,100) rect:setFillColor(255,0,0) rect.myName = "rect" physics.addBody( rect, "static", { friction=0.5, bounce=0 } ) local sheet1 = graphics.newImageSheet( "immagini/sprite.png", { width=135, height=259, numFrames=14 } ) local instance1 = display.newSprite( sheet1, { name="cat", start=1, count=14, time=550 } ) instance1.x = \_W/2-400 instance1.y = \_H/2+180 instance1.myName = "instance1" instance1:play() physics.addBody( instance1, "dynamic", { friction=0.5, bounce=0 } ) local sheet2 = graphics.newImageSheet( "immagini/profDonna.png", { width=149, height=320, numFrames=8 } ) local instance2 = display.newSprite( sheet2, { name="profDonna", start=1, count=8, time=800 } ) instance2.x = \_W/2 instance2.y = \_H/2+150 instance2.myName = "instance2" instance2:play() physics.addBody( instance2, "dynamic", { friction=0.5, bounce=0 } ) --move background local function muoviSfondo() bg1.x = bg1.x-velocita bg2.x = bg2.x-velocita if(bg1.x\<-712)then bg1.x = bg2.x+1423 end if(bg2.x\<-712)then bg2.x = bg1.x+1423 end end --pausa local function pause(event) if(event.phase == "began" and gameIsPaused == 0) then velocita = 0 physics.pause() instance1:pause() instance2:pause() replayBTN.alpha = 1 playBTN.alpha = 1 menuBTN.alpha = 1 gameIsPaused = 1 jumpBTN.alpha = 0 end end --resume local function resume() velocita = 1 physics.start() instance1:play() instance2:play() velocita = 1 replayBTN.alpha = 0 playBTN.alpha = 0 menuBTN.alpha = 0 gameIsPaused = 0 jumpBTN.alpha = 0.01 end --replay local function replay() playBTN.alpha = 0 replayBTN.alpha = 0 menuBTN.alpha = 0 physics.pause() composer.gotoScene("game") end --menu local function gotoMenu() playBTN.alpha = 0 replayBTN.alpha = 0 menuBTN.alpha = 0 composer.gotoScene("menu") end --salto local function up(event) if(event.phase == "began" and playerHasJumped == 0) then instance1:setLinearVelocity(0, -1700) instance1:pause() playerHasJumped = 1 elseif(event.phase == "began" and playerHasJumped == 1) then instance1:setLinearVelocity(0, -1000) playerHasJumped = 2 end end --collisioni local function onCollision( event ) if ( event.phase == "began" ) then playerHasJumped = 0 instance1:play() elseif ( event.phase == "ended" ) then end end -- "scene:create()" function scene:create( event ) local sceneGroup = self.view sceneGroup:insert(bg1) sceneGroup:insert(bg2) sceneGroup:insert(rect) sceneGroup:insert(instance1) sceneGroup:insert(instance2) sceneGroup:insert(jumpBTN) sceneGroup:insert(pauseBTN) sceneGroup:insert(replayBTN) sceneGroup:insert(playBTN) sceneGroup:insert(menuBTN) Runtime:addEventListener("enterFrame", muoviSfondo) pauseBTN:addEventListener("touch", pause) playBTN:addEventListener("touch", resume) replayBTN:addEventListener("touch", replay) menuBTN:addEventListener("touch", gotoMenu) jumpBTN:addEventListener("touch",up) Runtime:addEventListener( "collision", onCollision ) end -- "scene:show()" function scene:show( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then elseif ( phase == "did" ) then bg1.x = \_W/2; bg1.y = \_H/2 bg2.x = \_W/2+1423; bg2.y = \_H/2 instance1:play() instance2:play() physics.start() velocita = 1 Runtime:addEventListener("enterFrame", muoviSfondo) instance1.x = \_W/2-400; instance1.y = \_H/2+180 gameIsPaused = 0 playerHasJumped = 0 end end -- "scene:hide()" function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then elseif ( phase == "did" ) then physics.stop() end end -- "scene:destroy()" function scene:destroy( event ) local sceneGroup = self.view end -- ------------------------------------------------------------------------------- -- Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) -- ------------------------------------------------------------------------------- return scene

Lines 19-50 are all “creating” your scene.  They should be part of the scene:create() function before you starting inserting them into groups.

You probably should google around to find the Corona “scope” tutorials to help you understand this better, but when you put something inside another function, it’s only visible to that function.  This is likely why you are creating our display objects out side of the scene:create() function and letting the scene’s main chunk do the work.  This is not the right place to do these actions.

But because of this visibility, when you make something local it can be seen in other functions.  The next logical step is to remove the “local” from every thing and make them all global.  But this is bad.  Globals cause more harm than good.  What you need is to be declare everything at the top:

local jumpBTN, pauseBTN

– etc.

But not do anything with them… Just the word “local” and the variable name.  This will give them visibility to the entire lua module.  Then in side of scene:create, move the code that I outline above into the create() function and remove the word “local” from them since you declared them local at the top.

Rob

Thanks again, but I cannot find out what do you want to say. Can you please make an example using the code I posted so I can reuse its logic again in the future?

Thanks in advantage.

[lua]
local composer = require( “composer” )
local scene = composer.newScene()
 
display.setStatusBar(display.HiddenStatusBar)
 
_W = display.contentWidth
_H = display.contentHeight
 
require(“physics”)
physics.start()
physics.setDrawMode(“normal”)
physics.setGravity( 0,140)
 
local timeSprite = 800
local velocita = 1
local playerHasJumped = 0
local gameIsPaused = 0
local replayBTN, playBTN, menuBTN, jumpBTN, instance1, instance2

–move background
local function muoviSfondo()
    bg1.x = bg1.x-velocita
    bg2.x = bg2.x-velocita
    if(bg1.x<-712)then
        bg1.x = bg2.x+1423
    end
    if(bg2.x<-712)then
        bg2.x = bg1.x+1423
    end
end
 
 
–pausa
local function pause(event)
    if(event.phase == “began” and gameIsPaused == 0) then
        velocita = 0
        physics.pause()
        instance1:pause()
        instance2:pause()
        replayBTN.alpha = 1
        playBTN.alpha = 1
        menuBTN.alpha = 1
        gameIsPaused = 1
        jumpBTN.alpha = 0
    end
end

–resume
local function resume()
    velocita = 1
    physics.start()
    instance1:play()
    instance2:play()
    velocita = 1
    replayBTN.alpha = 0
    playBTN.alpha = 0
    menuBTN.alpha = 0
    gameIsPaused = 0
    jumpBTN.alpha = 0.01    
end

–replay
local function replay()
    playBTN.alpha = 0
    replayBTN.alpha = 0
    menuBTN.alpha = 0
    physics.pause()
    composer.gotoScene(“game”)
end

–menu
local function gotoMenu()
    playBTN.alpha = 0
    replayBTN.alpha = 0
    menuBTN.alpha = 0
    composer.gotoScene(“menu”)
end
 
–salto
local function up(event)
    if(event.phase == “began” and playerHasJumped == 0) then
        instance1:setLinearVelocity(0, -1700)
        instance1:pause()
        playerHasJumped = 1
    elseif(event.phase == “began” and playerHasJumped == 1) then
        instance1:setLinearVelocity(0, -1000)
        playerHasJumped = 2
    end
end

–collisioni
local function onCollision( event )
    if ( event.phase == “began” ) then
        playerHasJumped = 0
        instance1:play()
    elseif ( event.phase == “ended” ) then
    end
end

– “scene:create()”
function scene:create( event )
    local sceneGroup = self.view

    jumpBTN = display.newImage(“immagini/jump.png”); jumpBTN.x = _W/2-900; jumpBTN.y = _H/2; jumpBTN.alpha = 0.01
    pauseBTN = display.newImage(“immagini/pauseBTN.png”); pauseBTN.x = _W/2+660; pauseBTN.y = _H/2-360; pauseBTN.xScale = 0.2; pauseBTN.yScale = 0.2
    replayBTN = display.newImage(“immagini/replayBTN.png”); replayBTN.x = _W/2+200; replayBTN.y = _H/2; replayBTN.xScale = 0.07; replayBTN.yScale = 0.07; replayBTN.alpha = 0
    playBTN = display.newImage(“immagini/playBtn.png”); playBTN.x = _W/2; playBTN.y = _H/2; playBTN.xScale = 1; playBTN.yScale = 1; playBTN.alpha = 0
    menuBTN = display.newImage(“immagini/menuBtn.png”); menuBTN.x = _W/2-200; menuBTN.y = _H/2; menuBTN.xScale = 0.2; menuBTN.yScale = 0.2; menuBTN.alpha = 0
     
    --background
    local bg1 = display.newImage(“immagini/bg1.png”); bg1.x = _W/2; bg1.y = _H/2
    local bg2 = display.newImage(“immagini/bg2.png”); bg2.x = _W/2+1423; bg2.y = _H/2

    local rect = display.newRect(_W/2,_H-40,_W,100)
    rect:setFillColor(255,0,0)
    rect.myName = “rect”
    physics.addBody( rect, “static”, { friction=0.5, bounce=0 } )

    local sheet1 = graphics.newImageSheet( “immagini/sprite.png”, { width=135, height=259, numFrames=14 } )
    instance1 = display.newSprite( sheet1, { name=“cat”, start=1, count=14, time=550 } )
    instance1.x = _W/2-400
    instance1.y = _H/2+180
    instance1.myName = “instance1”
    instance1:play()
    physics.addBody( instance1, “dynamic”, { friction=0.5, bounce=0 } )
     
    local sheet2 = graphics.newImageSheet( “immagini/profDonna.png”, { width=149, height=320, numFrames=8 } )
    instance2 = display.newSprite( sheet2, { name=“profDonna”, start=1, count=8, time=800 } )
    instance2.x = _W/2
    instance2.y = _H/2+150
    instance2.myName = “instance2”
    instance2:play()
    physics.addBody( instance2, “dynamic”, { friction=0.5, bounce=0 } )

    sceneGroup:insert(bg1)
    sceneGroup:insert(bg2)
    sceneGroup:insert(rect)
    sceneGroup:insert(instance1)
    sceneGroup:insert(instance2)
    sceneGroup:insert(jumpBTN)
    sceneGroup:insert(pauseBTN)
    sceneGroup:insert(replayBTN)
    sceneGroup:insert(playBTN)
    sceneGroup:insert(menuBTN)
    Runtime:addEventListener(“enterFrame”, muoviSfondo)
    pauseBTN:addEventListener(“touch”, pause)
    playBTN:addEventListener(“touch”, resume)
    replayBTN:addEventListener(“touch”, replay)
    menuBTN:addEventListener(“touch”, gotoMenu)
    jumpBTN:addEventListener(“touch”,up)
    Runtime:addEventListener( “collision”, onCollision )
end
 
 
 
– “scene:show()”
function scene:show( event )
 
    local sceneGroup = self.view
    local phase = event.phase
     
    if ( phase == “will” ) then
    elseif ( phase == “did” ) then
        bg1.x = _W/2; bg1.y = _H/2
        bg2.x = _W/2+1423; bg2.y = _H/2
        instance1:play()
        instance2:play()
        physics.start()
        velocita = 1
        Runtime:addEventListener(“enterFrame”, muoviSfondo)
        instance1.x = _W/2-400; instance1.y = _H/2+180
        gameIsPaused = 0
        playerHasJumped = 0
    end
end

– “scene:hide()”
function scene:hide( event )
 
local sceneGroup = self.view
    local phase = event.phase
 
    if ( phase == “will” ) then
    elseif ( phase == “did” ) then
        physics.stop()
    end
end

– “scene:destroy()”
function scene:destroy( event )
    local sceneGroup = self.view
 
end


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


 
return scene

[/lua]

Or something like that…

Rob

Thanks a lot for your help. Anyway I’m looking at several tutorial with the composer class. Sorry but now I’m at my first experience with this new API. 

Yep, gonna need code.  Usually people have issues not removing listeners and such.

Rob

Thanks. For instance, in the following code, when I reload the scene, the event listener associated to “jumpBTN” dissapear.

local composer = require( "composer" ) local scene = composer.newScene() display.setStatusBar(display.HiddenStatusBar) \_W = display.contentWidth \_H = display.contentHeight local timeSprite = 800 local velocita = 1 local playerHasJumped = 0 require("physics") physics.start() physics.setDrawMode("normal") physics.setGravity( 0,140) local rect = display.newRect(\_W/2,\_H-40,\_W,100) rect:setFillColor(255,0,0) rect.myName = "rect" physics.addBody( rect, "static", { friction=0.5, bounce=0 } ) local sheet1 = graphics.newImageSheet( "immagini/sprite.png", { width=135, height=259, numFrames=14 } ) local instance1 = display.newSprite( sheet1, { name="cat", start=1, count=14, time=550 } ) instance1.x = \_W/2-400 instance1.y = \_H/2+180 instance1.myName = "instance1" instance1:play() physics.addBody( instance1, "dynamic", { friction=0.5, bounce=0 } ) local jumpBTN = display.newImage("immagini/jump.png"); jumpBTN.x = \_W/2-900; jumpBTN.y = \_H/2; jumpBTN.alpha = 0.01 local replayBTN = display.newImage("immagini/replayBTN.png"); replayBTN.x = \_W/2+200; replayBTN.y = \_H/2; replayBTN.xScale = 0.07; replayBTN.yScale = 0.07; replayBTN.alpha = 0 --replay local function replay() replayBTN.alpha = 0 composer.gotoScene("game") end replayBTN:addEventListener("touch", replay) local function up(event) if(event.phase == "began" and playerHasJumped == 0) then instance1:setLinearVelocity(0, -1700) instance1:pause() playerHasJumped = 1 elseif(event.phase == "began" and playerHasJumped == 1) then instance1:setLinearVelocity(0, -1000) playerHasJumped = 2 end end jumpBTN:addEventListener("touch",up) --collisioni local function onCollision( event ) if ( event.phase == "began" ) then playerHasJumped = 0 instance1:play() elseif ( event.phase == "ended" ) then print( "ended: " .. event.object1.myName .. " and " .. event.object2.myName ) end end Runtime:addEventListener( "collision", onCollision ) -- "scene:create()" function scene:create( event ) local sceneGroup = self.view sceneGroup:insert(rect) sceneGroup:insert(instance1) sceneGroup:insert(replayBTN) end -- "scene:show()" function scene:show( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then elseif ( phase == "did" ) then end end -- "scene:hide()" function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then instance1:pause() elseif ( phase == "did" ) then end end -- "scene:destroy()" function scene:destroy( event ) local sceneGroup = self.view rect:removeSelf() instance1:removeSelf() replayBTN:removeSelf() end -- ------------------------------------------------------------------------------- -- Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) -- ------------------------------------------------------------------------------- return scene

I would move lines 20-35 inside your scene:create() function.  Since you are trying to reference these items inside other functions, you will want to declare them local at the top:

local rect, instance1, jumpBTN, replayBTN

Then move lines: 43 55, and 67 into create() after the lines 20-35.

You don’t need lines 109-111.

You probably should have your instance1:play() in your scene:show() function’s “did” phase.  You probably also want to start physics there too (in addition to the physics.start() at the top).  You should probably pause or stop physics in the scene:hide()'s will phase.

Rob

Thank you so much Rob, but I haven’t understood what do I have to insert inside my “scene:create()” function. What do you you mean with “Since you are trying to reference these items inside other functions, you will want to declare them local at the top”

Do I have to insert

local rect = display.newRect(\_W/2,\_H-40,\_W,100) rect:setFillColor(255,0,0) rect.myName = "rect" physics.addBody( rect, "static", { friction=0.5, bounce=0 } ) bla bla bla...

 inside scene:create() or at the top BEFORE scene:create()?

Thanks again

Ahh ok, now I got it, anyway here’s my new code, I’ve added few things. I’ve inserted a pause button, that works when the scene is reloaded, but the jump button still won’t perform any action (it should works when “gameIsPaused” is equal to 0, but it just works the first time I run the app). In addition to that, everytime I replay the scene, the background speed increases, but if I try to print “velocita” it returns ‘1’ (which is correct, while the background scrolling speed gets faster time by time)

local composer = require( "composer" ) local scene = composer.newScene() display.setStatusBar(display.HiddenStatusBar) \_W = display.contentWidth \_H = display.contentHeight require("physics") physics.start() physics.setDrawMode("normal") physics.setGravity( 0,140) local timeSprite = 800 local velocita = 1 local playerHasJumped = 0 local gameIsPaused = 0 local jumpBTN = display.newImage("immagini/jump.png"); jumpBTN.x = \_W/2-900; jumpBTN.y = \_H/2; jumpBTN.alpha = 0.01 local pauseBTN = display.newImage("immagini/pauseBTN.png"); pauseBTN.x = \_W/2+660; pauseBTN.y = \_H/2-360; pauseBTN.xScale = 0.2; pauseBTN.yScale = 0.2 local replayBTN = display.newImage("immagini/replayBTN.png"); replayBTN.x = \_W/2+200; replayBTN.y = \_H/2; replayBTN.xScale = 0.07; replayBTN.yScale = 0.07; replayBTN.alpha = 0 local playBTN = display.newImage("immagini/playBtn.png"); playBTN.x = \_W/2; playBTN.y = \_H/2; playBTN.xScale = 1; playBTN.yScale = 1; playBTN.alpha = 0 local menuBTN = display.newImage("immagini/menuBtn.png"); menuBTN.x = \_W/2-200; menuBTN.y = \_H/2; menuBTN.xScale = 0.2; menuBTN.yScale = 0.2; menuBTN.alpha = 0 --background local bg1 = display.newImage("immagini/bg1.png"); bg1.x = \_W/2; bg1.y = \_H/2 local bg2 = display.newImage("immagini/bg2.png"); bg2.x = \_W/2+1423; bg2.y = \_H/2 local rect = display.newRect(\_W/2,\_H-40,\_W,100) rect:setFillColor(255,0,0) rect.myName = "rect" physics.addBody( rect, "static", { friction=0.5, bounce=0 } ) local sheet1 = graphics.newImageSheet( "immagini/sprite.png", { width=135, height=259, numFrames=14 } ) local instance1 = display.newSprite( sheet1, { name="cat", start=1, count=14, time=550 } ) instance1.x = \_W/2-400 instance1.y = \_H/2+180 instance1.myName = "instance1" instance1:play() physics.addBody( instance1, "dynamic", { friction=0.5, bounce=0 } ) local sheet2 = graphics.newImageSheet( "immagini/profDonna.png", { width=149, height=320, numFrames=8 } ) local instance2 = display.newSprite( sheet2, { name="profDonna", start=1, count=8, time=800 } ) instance2.x = \_W/2 instance2.y = \_H/2+150 instance2.myName = "instance2" instance2:play() physics.addBody( instance2, "dynamic", { friction=0.5, bounce=0 } ) --move background local function muoviSfondo() bg1.x = bg1.x-velocita bg2.x = bg2.x-velocita if(bg1.x\<-712)then bg1.x = bg2.x+1423 end if(bg2.x\<-712)then bg2.x = bg1.x+1423 end end --pausa local function pause(event) if(event.phase == "began" and gameIsPaused == 0) then velocita = 0 physics.pause() instance1:pause() instance2:pause() replayBTN.alpha = 1 playBTN.alpha = 1 menuBTN.alpha = 1 gameIsPaused = 1 jumpBTN.alpha = 0 end end --resume local function resume() velocita = 1 physics.start() instance1:play() instance2:play() velocita = 1 replayBTN.alpha = 0 playBTN.alpha = 0 menuBTN.alpha = 0 gameIsPaused = 0 jumpBTN.alpha = 0.01 end --replay local function replay() playBTN.alpha = 0 replayBTN.alpha = 0 menuBTN.alpha = 0 physics.pause() composer.gotoScene("game") end --menu local function gotoMenu() playBTN.alpha = 0 replayBTN.alpha = 0 menuBTN.alpha = 0 composer.gotoScene("menu") end --salto local function up(event) if(event.phase == "began" and playerHasJumped == 0) then instance1:setLinearVelocity(0, -1700) instance1:pause() playerHasJumped = 1 elseif(event.phase == "began" and playerHasJumped == 1) then instance1:setLinearVelocity(0, -1000) playerHasJumped = 2 end end --collisioni local function onCollision( event ) if ( event.phase == "began" ) then playerHasJumped = 0 instance1:play() elseif ( event.phase == "ended" ) then end end -- "scene:create()" function scene:create( event ) local sceneGroup = self.view sceneGroup:insert(bg1) sceneGroup:insert(bg2) sceneGroup:insert(rect) sceneGroup:insert(instance1) sceneGroup:insert(instance2) sceneGroup:insert(jumpBTN) sceneGroup:insert(pauseBTN) sceneGroup:insert(replayBTN) sceneGroup:insert(playBTN) sceneGroup:insert(menuBTN) Runtime:addEventListener("enterFrame", muoviSfondo) pauseBTN:addEventListener("touch", pause) playBTN:addEventListener("touch", resume) replayBTN:addEventListener("touch", replay) menuBTN:addEventListener("touch", gotoMenu) jumpBTN:addEventListener("touch",up) Runtime:addEventListener( "collision", onCollision ) end -- "scene:show()" function scene:show( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then elseif ( phase == "did" ) then bg1.x = \_W/2; bg1.y = \_H/2 bg2.x = \_W/2+1423; bg2.y = \_H/2 instance1:play() instance2:play() physics.start() velocita = 1 Runtime:addEventListener("enterFrame", muoviSfondo) instance1.x = \_W/2-400; instance1.y = \_H/2+180 gameIsPaused = 0 playerHasJumped = 0 end end -- "scene:hide()" function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then elseif ( phase == "did" ) then physics.stop() end end -- "scene:destroy()" function scene:destroy( event ) local sceneGroup = self.view end -- ------------------------------------------------------------------------------- -- Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) -- ------------------------------------------------------------------------------- return scene

Lines 19-50 are all “creating” your scene.  They should be part of the scene:create() function before you starting inserting them into groups.

You probably should google around to find the Corona “scope” tutorials to help you understand this better, but when you put something inside another function, it’s only visible to that function.  This is likely why you are creating our display objects out side of the scene:create() function and letting the scene’s main chunk do the work.  This is not the right place to do these actions.

But because of this visibility, when you make something local it can be seen in other functions.  The next logical step is to remove the “local” from every thing and make them all global.  But this is bad.  Globals cause more harm than good.  What you need is to be declare everything at the top:

local jumpBTN, pauseBTN

– etc.

But not do anything with them… Just the word “local” and the variable name.  This will give them visibility to the entire lua module.  Then in side of scene:create, move the code that I outline above into the create() function and remove the word “local” from them since you declared them local at the top.

Rob

Thanks again, but I cannot find out what do you want to say. Can you please make an example using the code I posted so I can reuse its logic again in the future?

Thanks in advantage.

[lua]
local composer = require( “composer” )
local scene = composer.newScene()
 
display.setStatusBar(display.HiddenStatusBar)
 
_W = display.contentWidth
_H = display.contentHeight
 
require(“physics”)
physics.start()
physics.setDrawMode(“normal”)
physics.setGravity( 0,140)
 
local timeSprite = 800
local velocita = 1
local playerHasJumped = 0
local gameIsPaused = 0
local replayBTN, playBTN, menuBTN, jumpBTN, instance1, instance2

–move background
local function muoviSfondo()
    bg1.x = bg1.x-velocita
    bg2.x = bg2.x-velocita
    if(bg1.x<-712)then
        bg1.x = bg2.x+1423
    end
    if(bg2.x<-712)then
        bg2.x = bg1.x+1423
    end
end
 
 
–pausa
local function pause(event)
    if(event.phase == “began” and gameIsPaused == 0) then
        velocita = 0
        physics.pause()
        instance1:pause()
        instance2:pause()
        replayBTN.alpha = 1
        playBTN.alpha = 1
        menuBTN.alpha = 1
        gameIsPaused = 1
        jumpBTN.alpha = 0
    end
end

–resume
local function resume()
    velocita = 1
    physics.start()
    instance1:play()
    instance2:play()
    velocita = 1
    replayBTN.alpha = 0
    playBTN.alpha = 0
    menuBTN.alpha = 0
    gameIsPaused = 0
    jumpBTN.alpha = 0.01    
end

–replay
local function replay()
    playBTN.alpha = 0
    replayBTN.alpha = 0
    menuBTN.alpha = 0
    physics.pause()
    composer.gotoScene(“game”)
end

–menu
local function gotoMenu()
    playBTN.alpha = 0
    replayBTN.alpha = 0
    menuBTN.alpha = 0
    composer.gotoScene(“menu”)
end
 
–salto
local function up(event)
    if(event.phase == “began” and playerHasJumped == 0) then
        instance1:setLinearVelocity(0, -1700)
        instance1:pause()
        playerHasJumped = 1
    elseif(event.phase == “began” and playerHasJumped == 1) then
        instance1:setLinearVelocity(0, -1000)
        playerHasJumped = 2
    end
end

–collisioni
local function onCollision( event )
    if ( event.phase == “began” ) then
        playerHasJumped = 0
        instance1:play()
    elseif ( event.phase == “ended” ) then
    end
end

– “scene:create()”
function scene:create( event )
    local sceneGroup = self.view

    jumpBTN = display.newImage(“immagini/jump.png”); jumpBTN.x = _W/2-900; jumpBTN.y = _H/2; jumpBTN.alpha = 0.01
    pauseBTN = display.newImage(“immagini/pauseBTN.png”); pauseBTN.x = _W/2+660; pauseBTN.y = _H/2-360; pauseBTN.xScale = 0.2; pauseBTN.yScale = 0.2
    replayBTN = display.newImage(“immagini/replayBTN.png”); replayBTN.x = _W/2+200; replayBTN.y = _H/2; replayBTN.xScale = 0.07; replayBTN.yScale = 0.07; replayBTN.alpha = 0
    playBTN = display.newImage(“immagini/playBtn.png”); playBTN.x = _W/2; playBTN.y = _H/2; playBTN.xScale = 1; playBTN.yScale = 1; playBTN.alpha = 0
    menuBTN = display.newImage(“immagini/menuBtn.png”); menuBTN.x = _W/2-200; menuBTN.y = _H/2; menuBTN.xScale = 0.2; menuBTN.yScale = 0.2; menuBTN.alpha = 0
     
    --background
    local bg1 = display.newImage(“immagini/bg1.png”); bg1.x = _W/2; bg1.y = _H/2
    local bg2 = display.newImage(“immagini/bg2.png”); bg2.x = _W/2+1423; bg2.y = _H/2

    local rect = display.newRect(_W/2,_H-40,_W,100)
    rect:setFillColor(255,0,0)
    rect.myName = “rect”
    physics.addBody( rect, “static”, { friction=0.5, bounce=0 } )

    local sheet1 = graphics.newImageSheet( “immagini/sprite.png”, { width=135, height=259, numFrames=14 } )
    instance1 = display.newSprite( sheet1, { name=“cat”, start=1, count=14, time=550 } )
    instance1.x = _W/2-400
    instance1.y = _H/2+180
    instance1.myName = “instance1”
    instance1:play()
    physics.addBody( instance1, “dynamic”, { friction=0.5, bounce=0 } )
     
    local sheet2 = graphics.newImageSheet( “immagini/profDonna.png”, { width=149, height=320, numFrames=8 } )
    instance2 = display.newSprite( sheet2, { name=“profDonna”, start=1, count=8, time=800 } )
    instance2.x = _W/2
    instance2.y = _H/2+150
    instance2.myName = “instance2”
    instance2:play()
    physics.addBody( instance2, “dynamic”, { friction=0.5, bounce=0 } )

    sceneGroup:insert(bg1)
    sceneGroup:insert(bg2)
    sceneGroup:insert(rect)
    sceneGroup:insert(instance1)
    sceneGroup:insert(instance2)
    sceneGroup:insert(jumpBTN)
    sceneGroup:insert(pauseBTN)
    sceneGroup:insert(replayBTN)
    sceneGroup:insert(playBTN)
    sceneGroup:insert(menuBTN)
    Runtime:addEventListener(“enterFrame”, muoviSfondo)
    pauseBTN:addEventListener(“touch”, pause)
    playBTN:addEventListener(“touch”, resume)
    replayBTN:addEventListener(“touch”, replay)
    menuBTN:addEventListener(“touch”, gotoMenu)
    jumpBTN:addEventListener(“touch”,up)
    Runtime:addEventListener( “collision”, onCollision )
end
 
 
 
– “scene:show()”
function scene:show( event )
 
    local sceneGroup = self.view
    local phase = event.phase
     
    if ( phase == “will” ) then
    elseif ( phase == “did” ) then
        bg1.x = _W/2; bg1.y = _H/2
        bg2.x = _W/2+1423; bg2.y = _H/2
        instance1:play()
        instance2:play()
        physics.start()
        velocita = 1
        Runtime:addEventListener(“enterFrame”, muoviSfondo)
        instance1.x = _W/2-400; instance1.y = _H/2+180
        gameIsPaused = 0
        playerHasJumped = 0
    end
end

– “scene:hide()”
function scene:hide( event )
 
local sceneGroup = self.view
    local phase = event.phase
 
    if ( phase == “will” ) then
    elseif ( phase == “did” ) then
        physics.stop()
    end
end

– “scene:destroy()”
function scene:destroy( event )
    local sceneGroup = self.view
 
end


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


 
return scene

[/lua]

Or something like that…

Rob

Thanks a lot for your help. Anyway I’m looking at several tutorial with the composer class. Sorry but now I’m at my first experience with this new API.