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]
[import]uid: 10355 topic_id: 8055 reply_id: 28785[/import]