Change scene after collision of 2 objects

I am working on the structure for a game. I want a game level to end when a dynamic body collides with (in the game it passes through) a static body that is set as a sensor. I can’t figure out why it is not working. any help would be great, here is the code, lines 61-73 is where I am stuck:

[code]
module(…, package.seeall)

– Main function - MUST return a display.newGroup()
function new()
local localGroup = display.newGroup()

local physics = require(“physics”)
physics.start(true)
physics.setGravity( 0, 3 )

display.setStatusBar( display.HiddenStatusBar )
– Background
local background = display.newImage(“texture_sandstone.png”)
localGroup:insert(background)
– Title
local title = display.newText(“Play level x”, 37, 37, native.systemFontBold, 28)
title:setTextColor( 0,0,0)
title.x = 240
title.y = 20
title.name = “title”
localGroup:insert(title)
– Menu Buttons - Start

local backButton = nil
local function onPlay ( event )
if event.phase == “release” and backButton.isActive then
director:changeScene(“menu”, “fade”)
end
end
backButton = ui.newButton{
defaultSrc = “BackButton.png”,
defaultX = 60,
defaultY = 34,
overSrc = “backButtonover.png”,
overX = 60,
overY = 34,
onEvent = onPlay,
id = “backButton”,
text = “”,
font = “Helvetica”,
textColor = { 255, 255, 255, 255 },
emboss = false
}
backButton.x = 445
backButton.y = 20
backButton.isActive = true
localGroup:insert(backButton)

– Menu Buttons - End


local function onLocalCollision( event )

local phase = event.phase
if “ended” == phase then
director:changeScene(“playlevel2”, “fade”)
end
end
end

rect1:addEventListener( “collision”, onLocalCollision )
rect:addEventListener( “collision”, onLocalCollision )



local topshelf = display.newImage( “topshelf.png” )
topshelf.x = 320; topshelf.y = 300
physics.addBody( topshelf, “static”, { density=5.0, friction=0.3, bounce=0.1 } )
localGroup:insert(topshelf)

local rect1 = display.newRect( 300, 90, 25, 25 )
rect1:setFillColor( 125, 125, 125, 125 )
rect1.isVisible = true
physics.addBody( rect1, “dynamic” )
localGroup:insert(rect1)

local rect = display.newRect( 290, 160, 50, 50 )
rect:setFillColor( 255, 255, 255, 255 )
rect.isVisible = true
physics.addBody( rect, “static”, { isSensor = true } )
localGroup:insert(rect)


unloadMe = function()
end

– MUST return a display.newGroup()
return localGroup
end

[/code] [import]uid: 38341 topic_id: 8055 reply_id: 308055[/import]

I have the same exact question… Id love to hear solutions… [import]uid: 10355 topic_id: 8055 reply_id: 28772[/import]

Sorry, but I think you’ve got too many ‘end’ statements at line 68?

Also, if the function handling the collision event is not a table listener (which yours is not) then you need to specify the function name, though I think you’ve got a scope problem because of the extra ‘end’. Take it out and I think it should look like this:

[lua]local function onLocalCollision( event )
local phase = event.phase
if “ended” == phase then
director:changeScene(“playlevel2”, “fade”)
end
end

rect1:addEventListener( “collision”, onLocalCollision )
rect:addEventListener( “collision”, onLocalCollision )[/lua]

Or, if you want it as a table listener:

[lua] function myobj:collision( event )
local phase = event.phase
if “ended” == phase then
director:changeScene(“playlevel2”, “fade”)
end
end
end

rect1:addEventListener( “collision”, rect1 )
rect:addEventListener( “collision”, rect )[/lua]

In your case, for this to work the function would need to be defined lower down the listing. [import]uid: 8271 topic_id: 8055 reply_id: 28749[/import]

The first solution works… kind of… The scene changes when the first objects collides with with anything.

I want it to change only when the first object collides and comes to a rest on another specified object… how does this work? :stuck_out_tongue: [import]uid: 10355 topic_id: 8055 reply_id: 28785[/import]

For a table listener (where the collision function fires on the object being hit) the event arg will have a property ‘other’, this will be the other object involved in tge collision. Check the API docs for the regular args, though I believe both uses are documented. [import]uid: 8271 topic_id: 8055 reply_id: 28798[/import]

@horacebury

thanks for your help, :slight_smile:

D [import]uid: 38341 topic_id: 8055 reply_id: 28938[/import]