Composer: scene:show() is being called multiple times?

hey so watch this video and help me out on fixing the problem!? thanks!

https://youtu.be/b1FPKTHXIUA

heres the code in that game.lua file

local composer = require( "composer" ) local scene = composer.newScene() local physics = require( "physics" ) physics.start( ) physics.setDrawMode( "hybrid" ) function scene:create( event ) local sceneGroup = self.view local BackGround = display.newImageRect("BackGround.png", 1080, 1920) end function scene:show( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then print("test") elseif ( phase == "did" ) then print("testtest") local floor = display.newImageRect("floor.png", 351, 32) floor.x = display.contentWidth \* 0.5 floor.y = 510 --physics.addBody( floor, "static" ) ----------------------------------------------- local circle = {} local spawnCircles = function () local FallDown = math.random(display.contentWidth \* 0.2, display.contentWidth \* 0.8 ) circle = display.newImageRect("circle.png", 31, 31 ) circle.x = FallDown circle.y = -70 physics.addBody( circle, "dynamic", {density = .01, friction = 0.5, bounce = 0.1 }) circle.gravityScale = 0.1 physics.setGravity( 0, 10 ) --circle:addEventListener( "touch", dragCircle ) --circle:addEventListener( "collision", circle ) end myTimer = timer.performWithDelay( 2000, spawnCircles, -1) --1000 is the time end return scene end -- Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) -- ------------------------------------------------------------------------------- return scene

Can you post your code that goes to the game scene, probably your menu.lua?

ya sure

local composer = require( "composer" ) local scene = composer.newScene() local physics = require "physics" physics.start() physics.setGravity(0,0) local widget = require( "widget" ) function scene:create( event ) local sceneGroup = self.view background = display.newImageRect("BackGround.png",1080,1920) background.anchorX = 0.7 background.anchorY = 0.8 background.x = display.contentCenterX background.y = display.contentHeight sceneGroup:insert(background) ---------------------------------------------------------------------- local PlayBtn = display.newImageRect("PlayBtn.png", 73, 39 ) PlayBtn.x = display.contentWidth \* 0.5 PlayBtn.y = 200 PlayBtn.isVisible = false physics.addBody(PlayBtn, "static") ------------------------------------------------------ ------------------------------------------------- local MyButton = widget.newButton { left = 110, top = 285, width = 100, height = 103, defaultFile = "selector.png", onEvent = handleButtonEvent, } MyButton.anchorX = 0 MyButton.anchorY = 0 physics.addBody(MyButton, "dynamic", { halfWidth=5, halfHeight=10, x=10, y=0}) --------------------------------------------------------------------------------- function MyButton:touch( event ) if event.phase == "began" then display.getCurrentStage():setFocus( event.target ) self.markX = self.x -- store x location of object self.markY = self.y -- store y location of object PlayBtn.isVisible = true OptionsBtn.isVisible = true elseif event.phase == "moved" then local x = (event.x - event.xStart) + self.markX local y = (event.y - event.yStart) + self.markY self.x, self.y = x, y elseif event.phase == "cancelled" then elseif event.phase == "ended" then display.getCurrentStage():setFocus(nil) PlayBtn.isVisible = false OptionsBtn.isVisible = false ----- --transition.to(MyButton, {time=0, x= display.contentWidth \* 0.5, y= 500} ) ----- end return true end function onCollision(event) --print("collide!") display.remove(backround) display.remove(PlayBtn) display.remove(MyButton) composer.gotoScene ("game2") end Runtime:addEventListener("collision", onCollision) ------------------------------------------------------- sceneGroup:insert( background ) sceneGroup:insert( PlayBtn ) sceneGroup:insert( MyButton ) end -- Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) --------------------------------------------------------------------------------- return scene

maybe the on collision function in the menu.lua is messing with the game.lua?

@Rob Miracle

I see one thing that may cause you issues.

Change this:

function onCollision(event) --print("collide!") display.remove(backround) display.remove(PlayBtn) display.remove(MyButton) composer.gotoScene ("game2") end

to this:

function onCollision(event) if( event.phase ~= "began" ) then return true end display.remove(backround) display.remove(PlayBtn) display.remove(MyButton) composer.gotoScene ("game2") return true end

Your original listener would get called for “began” and “ended”, thus calling gotoScene() twice.

sad to say but i tried it and it still does the same thing… any other suggestions?

its really pissing me off haha holding me back on my game a whole lot any help would be awesome  :)  :slight_smile:

Well @roaminggamer suggested what I was going to. Collisions generate multiple events. Your original code could trigger game2 to be loaded multiple times.   Since you are not putting your objects in the composer sceneGroup, you won’t see the scene change multiple times as things just stack on top of each other.  This is also why you have to remove your background and other elements yourself.  You are not doing a:

sceneGroup:insert( background )

in scene:create().  You also make the background a global which means if you do this in another scene you’re going to write over other objects.  @roaminggamer’s suggested code should stop your duplication.  I recommend putting in some print statements to see if your collision event is happening multiple times. 

what do you mean? i put a print statment in the menu collision and it printed once correctly

actually its print infinity the collision in the menu…

im posting a video in 1 min 

https://youtu.be/txPjVEd03zs

i sent the file… tell me if you got it…? thanks

@RoamingGamer helped me out thanks!

Can you post your code that goes to the game scene, probably your menu.lua?

ya sure

local composer = require( "composer" ) local scene = composer.newScene() local physics = require "physics" physics.start() physics.setGravity(0,0) local widget = require( "widget" ) function scene:create( event ) local sceneGroup = self.view background = display.newImageRect("BackGround.png",1080,1920) background.anchorX = 0.7 background.anchorY = 0.8 background.x = display.contentCenterX background.y = display.contentHeight sceneGroup:insert(background) ---------------------------------------------------------------------- local PlayBtn = display.newImageRect("PlayBtn.png", 73, 39 ) PlayBtn.x = display.contentWidth \* 0.5 PlayBtn.y = 200 PlayBtn.isVisible = false physics.addBody(PlayBtn, "static") ------------------------------------------------------ ------------------------------------------------- local MyButton = widget.newButton { left = 110, top = 285, width = 100, height = 103, defaultFile = "selector.png", onEvent = handleButtonEvent, } MyButton.anchorX = 0 MyButton.anchorY = 0 physics.addBody(MyButton, "dynamic", { halfWidth=5, halfHeight=10, x=10, y=0}) --------------------------------------------------------------------------------- function MyButton:touch( event ) if event.phase == "began" then display.getCurrentStage():setFocus( event.target ) self.markX = self.x -- store x location of object self.markY = self.y -- store y location of object PlayBtn.isVisible = true OptionsBtn.isVisible = true elseif event.phase == "moved" then local x = (event.x - event.xStart) + self.markX local y = (event.y - event.yStart) + self.markY self.x, self.y = x, y elseif event.phase == "cancelled" then elseif event.phase == "ended" then display.getCurrentStage():setFocus(nil) PlayBtn.isVisible = false OptionsBtn.isVisible = false ----- --transition.to(MyButton, {time=0, x= display.contentWidth \* 0.5, y= 500} ) ----- end return true end function onCollision(event) --print("collide!") display.remove(backround) display.remove(PlayBtn) display.remove(MyButton) composer.gotoScene ("game2") end Runtime:addEventListener("collision", onCollision) ------------------------------------------------------- sceneGroup:insert( background ) sceneGroup:insert( PlayBtn ) sceneGroup:insert( MyButton ) end -- Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) --------------------------------------------------------------------------------- return scene

maybe the on collision function in the menu.lua is messing with the game.lua?

@Rob Miracle

I see one thing that may cause you issues.

Change this:

function onCollision(event) --print("collide!") display.remove(backround) display.remove(PlayBtn) display.remove(MyButton) composer.gotoScene ("game2") end

to this:

function onCollision(event) if( event.phase ~= "began" ) then return true end display.remove(backround) display.remove(PlayBtn) display.remove(MyButton) composer.gotoScene ("game2") return true end

Your original listener would get called for “began” and “ended”, thus calling gotoScene() twice.

sad to say but i tried it and it still does the same thing… any other suggestions?

its really pissing me off haha holding me back on my game a whole lot any help would be awesome  :)  :slight_smile: