hey hey…
I know this has been addressed before and I’ve tried this any which number of ways from what I’ve read on a number of other posts on the forums…
So I’ve got a simple little back and forth going on… character goes up an escalator, emerges in a scene… great, works a treat… goes back down and emerges in the original scene… looks great…
Do it twice and and the onCollision no longer functions… so all I can assume is that as there are two variables, they’ve fired twice and that’s the end of that…
I’ve read that using an interim scene may help this… so I’ve included a couple of these… pretty straight forward stuff… clear the scene, load the next one… This works well the first time around with a nice little fade… but the second time around there’s a sharp changing of scenes… like it’s not functioning correctly anymore…
What’s interesting is the console output I’m getting from the collision on the second time for both… first time, just one… second, multiple hits…
16:18:45.612 White square’s top-left position in screen coordinates: 50
16:18:46.156 hit!
16:18:49.388 White square’s top-left position in screen coordinates: 312
16:18:49.701 fish!!
16:18:53.042 White square’s top-left position in screen coordinates: 50
16:18:53.546 hit!
16:18:53.546 hit!
16:18:53.546 hit!
16:18:56.581 White square’s top-left position in screen coordinates: 312
16:18:56.947 fish!!
16:18:56.947 fish!!
16:18:56.947 fish!!
16:18:56.947 fish!!
16:18:56.947 fish!!
16:18:56.947 fish!!
Anyway, here’s the code… any thoughts would be greatly appreciated…
stationv1.lua
local composer = require( "composer" ) local manSprite = require "images.man" local physics = require("physics") physics.start() physics.setGravity( 0, 0 ) local alienSheet = graphics.newImageSheet( "images/man2sprites.png", manSprite.sheetData ) local sequences\_alienWalk = { -- consecutive frames sequence { name = "alienWalk", start = 1, count = 7, time = 800, loopCount = 0, loopDirection = "forward" } } local function characterWalk( event ) local thisSprite = event.target -- "event.target" references the sprite if ( event.phase == "ended" ) then thisSprite:setSequence( "alienWalk" ) -- switch to "fastRun" sequence thisSprite:play() -- play the new sequence end end local function onTouch(event) if ( event.phase == "began" ) then alienWalk:play() print( "White square's top-left position in screen coordinates: ", alienWalk.x ) elseif(event.phase == "ended" and event.x \> alienWalk.x) then alienWalk.xScale = 1 transition.to(alienWalk, { x=event.x, onComplete = function () alienWalk:pause() -- body end }) elseif(event.phase == "ended" and event.x \< alienWalk.x) then alienWalk.xScale = -1 transition.to(alienWalk, { x=event.x, onComplete = function () alienWalk:pause() -- body end }) end end local scene = composer.newScene() local function onCollision( event ) if ( event.phase == "began" ) then local obj1 = event.object1 local obj2 = event.object2 if ( ( obj1.myName == "escalator" and obj2.myName == "alienWalk" ) or ( obj1.myName == "alienWalk" and obj2.myName == "escalator" ) ) then transition.to(alienWalk, { time=1500, x=980, y=380, onComplete = function () composer.removeScene ( "scenes.stationv1" ) composer.gotoScene( "scenes.interim", { time=800, effect="crossFade" } ) end }) print("hit!") end end end function scene:create( event ) local sceneGroup = self.view local backGroup = display.newGroup() sceneGroup:insert( backGroup ) local trainGroup = display.newGroup() sceneGroup:insert( trainGroup ) local stationGroup = display.newGroup() sceneGroup:insert( stationGroup ) local characterGroup = display.newGroup() sceneGroup:insert( characterGroup ) local foreGroup = display.newGroup() -- Display group for the ship, asteroids, lasers, etc. sceneGroup:insert( foreGroup ) -- Code here runs when the scene is first created but has not yet appeared on screen local background = display.newImageRect( backGroup, "images/platform.png", display.contentWidth, display.contentHeight ) background.x = display.contentCenterX background.y = display.contentCenterY local escalator = display.newRect( characterGroup, 850, 640, 10, 150 ) escalator:setFillColor( 255,0,0 ) escalator.alpha = 0 escalator.isHitTestable = true escalator.id = 1 physics.addBody( escalator, { radius=10, isSensor=true } ) escalator.myName = "escalator" esco = display.newImageRect( foreGroup, "images/esco.png", 350, 380 ) esco.x = 860 esco.y = display.contentCenterY + 155 esco.alpha = 1 alienWalk = display.newSprite( characterGroup, alienSheet, sequences\_alienWalk ) alienWalk.x = 50 alienWalk.y = display.contentCenterY + 190 alienWalk.isVisible = true alienWalk.id = 61 physics.addBody( alienWalk, { radius=100, isSensor=true } ) alienWalk.xScale = 1 alienWalk.myName = "alienWalk" -- alienWalk:setLinearVelocity( 10, 4 ) alienWalk:addEventListener( "sprite", characterWalk ) 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 Runtime:addEventListener("touch", onTouch) Runtime:addEventListener( "collision", onCollision ) -- composer.removeScene( "scenes.stationv1") 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 Runtime:addEventListener("touch", onTouch) Runtime:addEventListener( "collision", onCollision ) physics.pause() composer.removeScene ("scenes.stationv1") -- 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 end -- ----------------------------------------------------------------------------------- -- Scene event function listeners -- ----------------------------------------------------------------------------------- scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) -- ----------------------------------------------------------------------------------- return scene
interim.lua
local composer = require( "composer" ) local scene = composer.newScene() function scene:create( event ) local sceneGroup = self.view 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 composer.removeScene ( "scenes.stationv1" ) composer.gotoScene( "scenes.fishhill", { time=400, effect="crossFade" } ) end end -- hide() function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then elseif ( phase == "did" ) then composer.removeScene ( "scenes.interim" ) end end
fishhill.lua
local composer = require( "composer" ) local manSprite = require "images.man" local physics = require("physics") local function onCollision( event ) if ( event.phase == "began" ) then local obj1 = event.object1 local obj2 = event.object2 if ( ( obj1.myName == "esco2" and obj2.myName == "alienWalk" ) or ( obj1.myName == "alienWalk" and obj2.myName == "esco2" ) ) then transition.to(alienWalk, { time=1500, x=20, y=display.contentCenterY +500, onComplete = function () -- composer.removeScene( "scenes.fishhill" ) composer.gotoScene( "scenes.interim2", { time=800, effect="crossFade" } ) end }) print("fish!!") end end end local alienSheet = graphics.newImageSheet( "images/man2sprites.png", manSprite.sheetData ) local sequences\_alienWalk = { -- consecutive frames sequence { name = "alienWalk", start = 1, count = 7, time = 800, loopCount = 0, loopDirection = "forward" } } local function characterWalk( event ) local thisSprite = event.target -- "event.target" references the sprite if ( event.phase == "ended" ) then thisSprite:setSequence( "alienWalk" ) -- switch to "fastRun" sequence thisSprite:play() -- play the new sequence end end local function onTouch(event) if ( event.phase == "began" ) then alienWalk:play() print( "White square's top-left position in screen coordinates: ", alienWalk.x ) elseif(event.phase == "ended" and event.x \> alienWalk.x) then alienWalk.xScale = 1 transition.to(alienWalk, { x=event.x, onComplete = function () alienWalk:pause() -- body end }) elseif(event.phase == "ended" and event.x \< alienWalk.x) then alienWalk.xScale = -1 transition.to(alienWalk, { x=event.x, onComplete = function () alienWalk:pause() -- body end }) end end local scene = composer.newScene() function scene:create( event ) local sceneGroup = self.view local backGroup = display.newGroup() sceneGroup:insert( backGroup ) local trainGroup = display.newGroup() sceneGroup:insert( trainGroup ) local stationGroup = display.newGroup() sceneGroup:insert( stationGroup ) local characterGroup = display.newGroup() sceneGroup:insert( characterGroup ) local foreGroup = display.newGroup() -- Display group for the ship, asteroids, lasers, etc. sceneGroup:insert( foreGroup ) -- Code here runs when the scene is first created but has not yet appeared on screen local background = display.newImageRect( backGroup, "images/fishhill.png", display.contentWidth, display.contentHeight ) background.x = display.contentCenterX background.y = display.contentCenterY local esco2 = display.newRect( characterGroup, display.contentCenterX - 365, display.contentCenterY + 215, 10, 100 ) esco2:setFillColor( 255,0,0 ) esco2.alpha = 1 esco2.isHitTestable = true esco2.id = 1 physics.addBody( esco2, { radius=10, isSensor=true } ) esco2.myName = "esco2" local subway1 = display.newImageRect( stationGroup, "images/subway1.png", 250, 600) subway1.x = display.contentCenterX - 385 subway1.y = display.contentCenterY + 95 local subway2 = display.newImageRect( foreGroup, "images/subway2.png", 250, 600) subway2.x = display.contentCenterX - 385 subway2.y = display.contentCenterY + 95 subway2.alpha = 1 alienWalk = display.newSprite( characterGroup, alienSheet, sequences\_alienWalk ) alienWalk.x = display.contentCenterX - 200 alienWalk.y = display.contentCenterY + 220 alienWalk.isVisible = true alienWalk.id = 61 physics.addBody( alienWalk, { radius=50, isSensor=true } ) alienWalk.xScale = 1 alienWalk.myName = "alienWalk" -- alienWalk:setLinearVelocity( 10, 4 ) alienWalk:addEventListener( "sprite", characterWalk ) 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 physics.start() physics.setGravity( 0, 0 ) Runtime:addEventListener("touch", onTouch) Runtime:addEventListener( "collision", onCollision ) -- composer.removeScene( "scenes.stationv1") 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 Runtime:addEventListener("touch", onTouch) Runtime:addEventListener( "collision", onCollision ) composer.removeScene( "scenes.fishhill" ) physics.pause() end end
interim2.lua
local composer = require( "composer" ) local scene = composer.newScene() function scene:create( event ) local sceneGroup = self.view -- Code here runs when the scene is first created but has not yet appeared on screen 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 composer.removeScene ( "scenes.fishhill" ) composer.gotoScene( "scenes.stationv1", { time=400, effect="crossFade" } ) 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 composer.removeScene ( "scenes.interim2" ) end end