change a scene after a collision

Hello guys, I have to change the scene after a collision, and I did it using the following code:

 function onCollision( event )  
 if ( event.phase == "began" ) then  
 if event.object1.myName == "coin" and event.object2.myName == "player" then  
   
 event.object1.alpha = 0.001  
  
 director:changeScene("second")  
  
 player:removeSelf()  
 player = nil  
 bgSpeed = 0  
  
 local fade = display.newImage("immagini/fade.png")  
 fade.alpha = 0  
 transition.to( fade, { alpha=1, time=1000 } )  
  
  
  
 media.playSound( "suoni/newworld.wav" )  
  
  
  
  
 end  
   
 elseif ( event.phase == "ended" ) then  
   
 if event.object1.myName == "coin" and event.object2.myName == "player" then  
 event.object1:removeSelf()  
 event.object1 = nil  
  
  
  
 end  
   
 end  
 end  
  
 Runtime:addEventListener( "collision", onCollision )  

Well, the scene change, the problem is that it remain static with all the object fixed on the screen, while they should move.
I thought it was a problem of the scene I changed, so I created a very simple scene with a transition, but unfortunately it still won’t work.

Here’s the simple scene I created for test purposes:

local director = require "director"  
  
r = display.newRect( 0, 0, display.contentWidth, display.contentHeight )  
r:setFillColor( 255, 255, 255 )  
  
local function repeatFade (event)  
 r.alpha = 1  
 transition.to( r, { alpha=0, time=1000 } )  
end  
  
timer.performWithDelay(1000, repeatFade, 20 )  

any ideas on I can I fix my problem?

Thanks! :wink: [import]uid: 122056 topic_id: 34188 reply_id: 334188[/import]

All your code after: director:changeScene(“second”) will never execute. Think of that gotoScene as a “goto” statement, it breaks the flow of your program and branches somewhere else as opposed to typical function calls which go do something and come back and finish up.

If you can’t do that code before the director:changeScene() call then you should do it in the optional director’s Clean function (which I don’t know if you’ve written yet). Director will call back to that function after it changes scenes to clean up after itself, .i.e things it still needs while it’s changing scenes.

[import]uid: 199310 topic_id: 34188 reply_id: 135974[/import]

Thanks for your help!

Yes sorry but I forgot to cut and paste “director:changeScene(“second”)” at the end of my function…at the beginning it was there.

anyway now my function looks like:

function onCollision( event )  
 if ( event.phase == "began" ) then  
 if event.object1.myName == "coin" and event.object2.myName == "player" then  
   
 event.object1.alpha = 0.001  
  
  
  
 player:removeSelf()  
 player = nil  
 bgSpeed = 0  
  
 local fade = display.newImage("immagini/fade.png")  
 fade.alpha = 0  
 transition.to( fade, { alpha=1, time=1000 } )  
  
  
  
 media.playSound( "suoni/newworld.wav" )  
  
 storyboard.removeScene( "game" )  
 storyboard.gotoScene( "second" , "fade", 500 )  
  
  
 end  
   
 elseif ( event.phase == "ended" ) then  
   
 if event.object1.myName == "coin" and event.object2.myName == "player" then  
 event.object1:removeSelf()  
 event.object1 = nil  
  
  
  
 end  
   
 end  
 end  
  
 Runtime:addEventListener( "collision", onCollision )  

But unfortunately I’m still getting the same error :frowning: [import]uid: 122056 topic_id: 34188 reply_id: 135988[/import]

I’m really confused now. Are you using director or Storyboard?

[import]uid: 199310 topic_id: 34188 reply_id: 135992[/import]

I’m currently using director, so don’t take care of what I wrote before :slight_smile:

In the end, my code is:

function onCollision( event )  
 if ( event.phase == "began" ) then  
 if event.object1.myName == "coin" and event.object2.myName == "player" then  
   
 event.object1.alpha = 0.001  
  
  
  
 player:removeSelf()  
 player = nil  
 bgSpeed = 0  
  
 local fade = display.newImage("immagini/fade.png")  
 fade.alpha = 0  
 transition.to( fade, { alpha=1, time=1000 } )  
  
  
  
 media.playSound( "suoni/newworld.wav" )  
  
 director:changeScene("second")  
  
  
 end  
   
 elseif ( event.phase == "ended" ) then  
   
 if event.object1.myName == "coin" and event.object2.myName == "player" then  
 event.object1:removeSelf()  
 event.object1 = nil  
  
  
  
 end  
   
 end  
 end  
  
 Runtime:addEventListener( "collision", onCollision )  

[import]uid: 122056 topic_id: 34188 reply_id: 136012[/import]

All your code after: director:changeScene(“second”) will never execute. Think of that gotoScene as a “goto” statement, it breaks the flow of your program and branches somewhere else as opposed to typical function calls which go do something and come back and finish up.

If you can’t do that code before the director:changeScene() call then you should do it in the optional director’s Clean function (which I don’t know if you’ve written yet). Director will call back to that function after it changes scenes to clean up after itself, .i.e things it still needs while it’s changing scenes.

[import]uid: 199310 topic_id: 34188 reply_id: 135974[/import]

Thanks for your help!

Yes sorry but I forgot to cut and paste “director:changeScene(“second”)” at the end of my function…at the beginning it was there.

anyway now my function looks like:

function onCollision( event )  
 if ( event.phase == "began" ) then  
 if event.object1.myName == "coin" and event.object2.myName == "player" then  
   
 event.object1.alpha = 0.001  
  
  
  
 player:removeSelf()  
 player = nil  
 bgSpeed = 0  
  
 local fade = display.newImage("immagini/fade.png")  
 fade.alpha = 0  
 transition.to( fade, { alpha=1, time=1000 } )  
  
  
  
 media.playSound( "suoni/newworld.wav" )  
  
 storyboard.removeScene( "game" )  
 storyboard.gotoScene( "second" , "fade", 500 )  
  
  
 end  
   
 elseif ( event.phase == "ended" ) then  
   
 if event.object1.myName == "coin" and event.object2.myName == "player" then  
 event.object1:removeSelf()  
 event.object1 = nil  
  
  
  
 end  
   
 end  
 end  
  
 Runtime:addEventListener( "collision", onCollision )  

But unfortunately I’m still getting the same error :frowning: [import]uid: 122056 topic_id: 34188 reply_id: 135988[/import]

I’m really confused now. Are you using director or Storyboard?

[import]uid: 199310 topic_id: 34188 reply_id: 135992[/import]

I’m currently using director, so don’t take care of what I wrote before :slight_smile:

In the end, my code is:

function onCollision( event )  
 if ( event.phase == "began" ) then  
 if event.object1.myName == "coin" and event.object2.myName == "player" then  
   
 event.object1.alpha = 0.001  
  
  
  
 player:removeSelf()  
 player = nil  
 bgSpeed = 0  
  
 local fade = display.newImage("immagini/fade.png")  
 fade.alpha = 0  
 transition.to( fade, { alpha=1, time=1000 } )  
  
  
  
 media.playSound( "suoni/newworld.wav" )  
  
 director:changeScene("second")  
  
  
 end  
   
 elseif ( event.phase == "ended" ) then  
   
 if event.object1.myName == "coin" and event.object2.myName == "player" then  
 event.object1:removeSelf()  
 event.object1 = nil  
  
  
  
 end  
   
 end  
 end  
  
 Runtime:addEventListener( "collision", onCollision )  

[import]uid: 122056 topic_id: 34188 reply_id: 136012[/import]