Mitra0000
Mitra0000

Reputation: 182

How to go to a new scene as a collision event in Lua and Corona SDK?

Leading on from my last question, I have some obstacles which move across the screen. I need my app to go to the end game screen when my car (called "car") collides with one of the obstacles. Here is my code so far.

    function obstacles()
        local function onCollision( event )
            if ( event.phase == "began" ) then
                composer.gotoScene( "end", "fade", 500 )
            end
        end
        local obstacle = display.newLine( display.contentWidth, display.contentHeight - 72, display.contentWidth, display.contentHeight - 102 )
        obstacle:addEventListener( "collision", onCollision )
        obstacle:setStrokeColor( 1, 0, 0, 1 )
        obstacle.strokeWidth = 18
        transition.to( obstacle, { time = 3000, x=-70, onComplete=obstacles } )
    end

Whenever I seem to run the script and the car hits the obstacles nothing happens. Can anyone help please?

Upvotes: 0

Views: 81

Answers (1)

Koormo
Koormo

Reputation: 166

I don't know if your collision event is working, but your gotoScene function is not. You need to check the docs: gotoScene() function

You only need 2 parameters, the first is the name of the scene, the second is an optional table with parameters for the transition.

Try first to execute composer.gotoScene ("end")

If it works you can try:

local options = {
    effect = "fade",
    time = 500
}
composer.gotoScene( "end", options )

Upvotes: 1

Related Questions