Scene Change Pleeeeease Help!

ADDED LINK TO STORYBOARD PROJECT WITH ERROR

I have tried changing scenes so many times, it either makes the player glitch and move around anywhere, or I have attempt to applyforce (blah blah blah (don’t remember)) a nil value, or bad argument expected proxy, and all this other crazy stuff. To keep things simpler for the both of us I’ve sent you the code without the scene changing as it is a complete mess. I’ve been trying to do this for weeks but can’t get it right. I know it feels like I’m not doing anything by asking you what seems like impossible code, but I can’t get the scene to change without tons of errors. Please tell me the code to do

local function onCollision( event )

        if event.phase == “began” then

                go to nex​t lua/scene completely obliterating this one as if I’m opening a whole new game

        end

        return true

end

If you really don’t want to do the code for me I’m willing to pay at this point

I just want to get past this… please

display.setStatusBar( display.HiddenStatusBar )

local physics = require( “physics” )

physics.start( )

physics.setGravity( 0, 15 ) – set x & y gravity to 0

local background = display.newImage( “background.jpg” )

background.x = display.contentCenterX

background.y = display.contentCenterY

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

ground.x = display.contentCenterX

ground.y = 480

physics.addBody( ground, “static”, {density=1, friction=1, bounce=0.3 } )

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

flag.x = display.contentCenterX

flag.y = 50

physics.addBody( flag, “static”, {density=0, friction=1, bounce=0.3 } )

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

player.x = display.contentCenterX

player.y = 425

physics.addBody( player, {density=0, friction=1, bounce=0.3 } )

player.isFixedRotation = true

system.setAccelerometerInterval( 100.0 )

local tiltSpeed = 2

local function onTilt( event )

        movementX = tiltSpeed * event.xGravity

        player.x = player.x + movementX

                – prevent player from moving offscreen

        if player.x <= 30 then 

                player.x = 30 

        elseif player.x >= 310 then

                player.x = 310

        end

end

local function onCollision( event )

        if event.phase == “began” then

                go to next lua/scene completely obliterating this one as if I’m opening a whole new game

        end

        return true

end

local function onScreenTouch( event )

        if event.phase == “began” then

                – make player jump

                player:applyForce( 0, -4, player.x, player.y )

        end

end

Runtime:addEventListener( “accelerometer”, onTilt )

Runtime:addEventListener( “touch”, onScreenTouch )

flag:addEventListener(“collision”, onCollision )

Umm … we’d probably need to see the storyboard code since the glitch is with the scene-change process. Having said that … if you put everything into the scene display group, then storyboard.gotoScene(…) should remove everything for you. If there is a “player glitch or move around”, that would suggest that the player-object is not in the right display group. Did you start with, e.g., one of the storyboard examples and then add in your game-logic? or are you starting from the game-logic and trying to add in storyboard scenes?

Hi @kylefigula,

Did you remove your event listeners before change scene?

e.g.

Runtime:removeEventListener( “accelerometer”, onTilt )

Runtime:removeEventListener( “touch”, onScreenTouch )

flag:removeEventListener(“collision”, onCollision )

 

You can call them after collision or game over with some delay before you change scene.

 timer.performWithDelay(100, removeListeners )  

Good Luck!

burhan

Yeah. Basically, if we can only see this code, then the most obvious problem is you are not using display groups. The flag, background, player, etc should all be added to the scene, so that when you do use gotoScene, those display objects are safely removed.

ie:

function scene:createScene(event) local group = self.view local background = display.newImage(group, "background.png") -- etc. Most displayObjects let you specify a parent group as the first parameter! end

Umm … we’d probably need to see the storyboard code since the glitch is with the scene-change process. Having said that … if you put everything into the scene display group, then storyboard.gotoScene(…) should remove everything for you. If there is a “player glitch or move around”, that would suggest that the player-object is not in the right display group. Did you start with, e.g., one of the storyboard examples and then add in your game-logic? or are you starting from the game-logic and trying to add in storyboard scenes?

Hi @kylefigula,

Did you remove your event listeners before change scene?

e.g.

Runtime:removeEventListener( “accelerometer”, onTilt )

Runtime:removeEventListener( “touch”, onScreenTouch )

flag:removeEventListener(“collision”, onCollision )

 

You can call them after collision or game over with some delay before you change scene.

 timer.performWithDelay(100, removeListeners )  

Good Luck!

burhan

Yeah. Basically, if we can only see this code, then the most obvious problem is you are not using display groups. The flag, background, player, etc should all be added to the scene, so that when you do use gotoScene, those display objects are safely removed.

ie:

function scene:createScene(event) local group = self.view local background = display.newImage(group, "background.png") -- etc. Most displayObjects let you specify a parent group as the first parameter! end