Can you prevent collision from specified objects?

Hi @mkjt88,
OK, got it. There are actually two solutions to this:

  1. You don’t place a collision listener on the rocket, and neither on the walls. You only place them on the missiles. That way, the only thing that would trigger a collision callback would be the missiles.

I do NOT recommend this approach!!! Here’s the much better method:

  1. You place a collision listener ONLY on the rocket. This is probably what you’re doing. In any case like this, you want to place the collision listener on the “common object”, that being the rocket. In any collision, the rocket is “common” because maybe later you might have missiles, asteroids, aliens, etc. that all collide with the rocket. If you followed method #1 above, you’d have to put a collision listener on ALL of those objects, which is cumbersome and awful for performance. Putting just one listener on the rocket handles them all.

But your issue is that you need to prevent the rocket from doing anything (suffering damage?) if it hits a boundary wall. For that, you can still trigger the collision event, but just use conditional logic to make sure nothing really happens. For example, apply a “name” property to everything that might collide, and just say:

--declare wall and then add name property  
--could also be "objectName", "thisThingName"... whatever you want  
wall1.name = "wall"  
  
local function rocketCollide( self, event )  
 local collidedWithName = event.other.name  
 if ( collidedWithName ~= "wall" ) then  
 --deal damage or whatever  
 end  
end  

Does that help?
Brent [import]uid: 200026 topic_id: 34859 reply_id: 138624[/import]

Sure it would I can’t seem to get it set up right… I believe I did it right but guess not… here is my code…

[lua]-----------------------------------------------------------------------------------------

– level1.lua


local storyboard = require (“storyboard”)
local scene = storyboard.newScene()

screenW = display.contentWidth
screenH = display.contentHeight
display.setStatusBar(display.HiddenStatusBar)

local phyics = require “physics” ; physics.setDrawMode( “normal” )
physics.start()

– Boundaries
ceiling = display.newRect (0, screenH - 350, screenW, 0)
ceiling.name = “ceiling”
floor = display.newRect (0, screenH + 30, screenW, 0)
floor.name = “floor”

– Make Walls Physical
physics.addBody (ceiling, “static”, {friction = .2, bounce = .1})
physics.addBody (floor, “static”, {friction = .2, bounce = .1})

function scene:createScene(event)

local screenGroup = self.view

– Background Images

backgroundKep = display.newImage(“bgKep.png”, true)
backgroundKep:setReferencePoint(display.BottomLeftReferencePoint)
backgroundKep.x = 0
backgroundKep.y = 320
backgroundKep.speed = 8
screenGroup:insert(backgroundKep)

backgroundKep2 = display.newImage(“bgKep.png”, true)
backgroundKep2:setReferencePoint(display.BottomLeftReferencePoint)
backgroundKep2.x = 6077
backgroundKep2.y = 320
backgroundKep2.speed = 8
screenGroup:insert(backgroundKep2)

– Rocket
rocket = display.newImage(“rocket.png”)
rocket.x = 100
rocket.y = 160
rocket.name = “rocket”
physics.addBody(rocket, “dynamic”, {density = .1, friction = .5, radius = 25})
screenGroup:insert(rocket)

– Missiles
missile1 = display.newImage(“missile.png”)
missile1.x = 500
missile1.y = 100
missile1.speed = math.random(4,12)
missile1.initY = missile1.y
missile1.amp = math.random(1,100)
missile1.angle = math.random(1,360)
missile1.name = “missile1”
physics.addBody(missile1, “static”, {density = .1, bounce = .1, friction = .2})
screenGroup:insert(missile1)

missile2 = display.newImage(“missile.png”)
missile2.x = 500
missile2.y = 250
missile2.speed = math.random(7,15)
missile2.initY = missile2.y
missile2.amp = math.random(1,100)
missile2.angle = math.random(1,360)
missile2.name = “missile2”
physics.addBody(missile2, “static”, {density = .1, bounce = .1, friction = .2})
screenGroup:insert(missile2)

end

local function rocketCollide(self,event)
local collideWithName = event.name
if (collideWithName ~= “ceiling”) then
onCollision()
end
end

local function scrollField(self,event)
if self.x < -6075 then
self.x = 6077
else
self.x = self.x - self.speed
end
end

local function moveMissile(self,event)
if self.x < -50 then
self.x = 500
self.y = math.random(20,300)
self.speed = math.random(2,5)
self.amp = math.random(20,50)
self.angle = math.random(1,200)
else
self.x = self.x - self.speed
self.angle = self.angle + .1
self.y = self.amp * math.sin(self.angle)+self.initY
end
end

function activateRocket(self,event)
self:applyForce(0, -12, self.x, self.y)
end

function onTouch(event)
if event.phase == “began” then
rocket.enterFrame = activateRocket
Runtime:addEventListener(“enterFrame”, rocket)
end

if event.phase == “ended” then
Runtime:removeEventListener(“enterFrame”, rocket)
end
end

function onCollision(event)
if event.phase == “began” then
storyboard.gotoScene(“restart”, “fade”, 400)
end
end
function scene:enterScene(event)

backgroundKep.enterFrame = scrollField
Runtime:addEventListener(“enterFrame”, backgroundKep)

backgroundKep2.enterFrame = scrollField
Runtime:addEventListener(“enterFrame”, backgroundKep2)

missile1.enterFrame = moveMissile
Runtime:addEventListener(“enterFrame”, missile1)

missile2.enterFrame = moveMissile
Runtime:addEventListener(“enterFrame”, missile2)

Runtime:addEventListener(“touch”, onTouch)

Runtime:addEventListener(“collision”, onCollision)

end

function scene:exitScene(event)
Runtime:removeEventListener(“touch”, onTouch)

Runtime:removeEventListener(“enterFrame”, backgroundKep)

Runtime:removeEventListener(“enterFrame”, backgroundKep2)

Runtime:removeEventListener(“enterFrame”, missile1)

Runtime:removeEventListener(“enterFrame”, missile2)

Runtime:removeEventListener(“collision”, onCollision)
end

function scene:destroyScene(event)

end

scene:addEventListener(“createScene”, scene)
scene:addEventListener(“enterScene”, scene)
scene:addEventListener(“exitScene”, scene)
scene:addEventListener(“destroyScene”, scene)

return scene [import]uid: 20272 topic_id: 34859 reply_id: 138629[/import]

Hello again,
I experimented with this and I’m getting collision reports. What isn’t working on your side? How are you loading the scene from “main.lua”?

Brent [import]uid: 200026 topic_id: 34859 reply_id: 138714[/import]

Ill post my code…

Main [lua]-----------------------------------------------------------------------------------------

– Main.lua


display.setStatusBar(display.HiddenStatusBar)

local storyboard = require “storyboard”
storyboard.gotoScene(“start”)



– start.lua


screenW = display.contentWidth
screenH = display.contentHeight
display.setStatusBar(display.HiddenStatusBar)

local storyboard = require (“storyboard”)
local scene = storyboard.newScene()

local phyics = require “physics” ; physics.setDrawMode( “normal” )
physics.start()
function scene:createScene(event)
local screenGroup = self.view
background = display.newImage(“startScreen.png”)
screenGroup:insert(background)
end

function start(event)
if event.phase == “began” then
storyboard.gotoScene(“tutorial”)
end
end

function scene:enterScene(event)
background:addEventListener(“touch”, start)
end

function scene:exitScene(event)
background:removeEventListener(“touch”, start)
end

function scene:destroyScene(event)

end

scene:addEventListener(“createScene”, scene)
scene:addEventListener(“enterScene”, scene)
scene:addEventListener(“exitScene”, scene)
scene:addEventListener(“destroyScene”, scene)

return scene [import]uid: 20272 topic_id: 34859 reply_id: 138721[/import]

Tutorial

[lua]-----------------------------------------------------------------------------------------

– tutorial.lua


local storyboard = require (“storyboard”)
local scene = storyboard.newScene()

screenW = display.contentWidth
screenH = display.contentHeight
display.setStatusBar(display.HiddenStatusBar)

local phyics = require “physics” ; physics.setDrawMode( “normal” )
physics.start()

– Boundaries
ceiling = display.newRect (0, screenH - 350, screenW, 0)
floor = display.newRect (0, screenH + 30, screenW, 0)

– Make Walls Physical
physics.addBody (ceiling, “static”, {friction = .2, bounce = .1, filter = ceilingCollisionFilter})
physics.addBody (floor, “static”, {friction = .2, bounce = .1, floorCollisionFilter})

function scene:createScene(event)

local rocketCollisionFilter = {categoryBits = 1, maskBits = 2}
local missile1CollisionFilter = {categoryBits = 2, maskBits = 1}
local missile2CollisionFilter = {categoryBits = 2, maskBits = 1}
local ceilingCollisionFilter = {categoryBits = 3, maskBits = 0}
local floorCollisionFilter = {categoryBits = 3, maskBits = 0}
local screenGroup = self.view

– Background Images
background1 = display.newImage(“background.png”)
background1:setReferencePoint(display.BottomLeftReferencePoint)
background1.x = 0
background1.y = 320
background1.speed = 10
screenGroup:insert(background1)
background2 = display.newImage(“background.png”)
background2:setReferencePoint(display.BottomLeftReferencePoint)
background2.x = 480
background2.y = 320
background2.speed = 10
screenGroup:insert(background2)

– Rocket
rocket = display.newImage(“rocket.png”)
rocket.x = 100
rocket.y = 160
physics.addBody(rocket, “dynamic”, {density = .1, friction = .5, radius = 25, filter = rocketCollisionFilter})
screenGroup:insert(rocket)

– Missiles
missile1 = display.newImage(“missile.png”)
missile1.x = 500
missile1.y = 100
missile1.speed = math.random(4,12)
missile1.initY = missile1.y
missile1.amp = math.random(1,100)
missile1.angle = math.random(1,360)
physics.addBody(missile1, “static”, {density = .1, bounce = .1, friction = .2, filter = missile1CollisionFilter})
screenGroup:insert(missile1)

missile2 = display.newImage(“missile.png”)
missile2.x = 500
missile2.y = 250
missile2.speed = math.random(7,15)
missile2.initY = missile2.y
missile2.amp = math.random(1,100)
missile2.angle = math.random(1,360)
physics.addBody(missile2, “static”, {density = .1, bounce = .1, friction = .2, filter = missile2CollisionFilter})
screenGroup:insert(missile2)

end
function scrollField(self,event)
if self.x < -475 then
self.x = 475
else
self.x = self.x - self.speed
end
end

function moveMissile(self,event)
if self.x < -50 then
self.x = 500
self.y = math.random(20,300)
self.speed = math.random(2,5)
self.amp = math.random(20,50)
self.angle = math.random(1,200)
else
self.x = self.x - self.speed
self.angle = self.angle + .1
self.y = self.amp * math.sin(self.angle)+self.initY
end
end

function activateRocket(self,event)
self:applyForce(0, -18, self.x, self.y)
end

function onTouch(event)
if event.phase == “began” then
rocket.enterFrame = activateRocket
Runtime:addEventListener(“enterFrame”, rocket)
end

if event.phase == “ended” then
Runtime:removeEventListener(“enterFrame”, rocket)
end
end

function onCollision(event)
if event.phase == “began” then
storyboard.gotoScene(“level1”, “fade”, 400)
end
end
function scene:enterScene(event)

background1.enterFrame = scrollField
Runtime:addEventListener(“enterFrame”, background1)

background2.enterFrame = scrollField
Runtime:addEventListener(“enterFrame”, background2)

missile1.enterFrame = moveMissile
Runtime:addEventListener(“enterFrame”, missile1)

missile2.enterFrame = moveMissile
Runtime:addEventListener(“enterFrame”, missile2)

Runtime:addEventListener(“touch”, onTouch)

Runtime:addEventListener(“collision”, onCollision)

end

function scene:exitScene(event)

Runtime:removeEventListener(“enterFrame”, background1)

Runtime:removeEventListener(“enterFrame”, background2)

Runtime:removeEventListener(“enterFrame”, missile1)

Runtime:removeEventListener(“enterFrame”, missile2)

Runtime:removeEventListener(“touch”, onTouch)

Runtime:removeEventListener(“collision”, onCollision)
end

function scene:destroyScene(event)

end

scene:addEventListener(“createScene”, scene)
scene:addEventListener(“enterScene”, scene)
scene:addEventListener(“exitScene”, scene)
scene:addEventListener(“destroyScene”, scene)

return scene [import]uid: 20272 topic_id: 34859 reply_id: 138722[/import]

[lua]-----------------------------------------------------------------------------------------

– level1.lua


local storyboard = require (“storyboard”)
local scene = storyboard.newScene()

screenW = display.contentWidth
screenH = display.contentHeight
display.setStatusBar(display.HiddenStatusBar)

local phyics = require “physics” ; physics.setDrawMode( “normal” )
physics.start()

– Boundaries
ceiling = display.newRect (0, screenH - 350, screenW, 0)
ceiling.name = “ceiling”
floor = display.newRect (0, screenH + 30, screenW, 0)
floor.name = “floor”

– Make Walls Physical
physics.addBody (ceiling, “static”, {friction = .2, bounce = .1})
physics.addBody (floor, “static”, {friction = .2, bounce = .1})

function scene:createScene(event)

local screenGroup = self.view

– Background Images

backgroundKep = display.newImage(“bgKep.png”, true)
backgroundKep:setReferencePoint(display.BottomLeftReferencePoint)
backgroundKep.x = 0
backgroundKep.y = 320
backgroundKep.speed = 8
screenGroup:insert(backgroundKep)

backgroundKep2 = display.newImage(“bgKep.png”, true)
backgroundKep2:setReferencePoint(display.BottomLeftReferencePoint)
backgroundKep2.x = 6077
backgroundKep2.y = 320
backgroundKep2.speed = 8
screenGroup:insert(backgroundKep2)

– Rocket
rocket = display.newImage(“rocket.png”)
rocket.x = 100
rocket.y = 160
rocket.name = “rocket”
physics.addBody(rocket, “dynamic”, {density = .1, friction = .5, radius = 25})
screenGroup:insert(rocket)

– Missiles
missile1 = display.newImage(“missile.png”)
missile1.x = 500
missile1.y = 100
missile1.speed = math.random(4,12)
missile1.initY = missile1.y
missile1.amp = math.random(1,100)
missile1.angle = math.random(1,360)
missile1.name = “missile1”
physics.addBody(missile1, “static”, {density = .1, bounce = .1, friction = .2})
screenGroup:insert(missile1)

missile2 = display.newImage(“missile.png”)
missile2.x = 500
missile2.y = 250
missile2.speed = math.random(7,15)
missile2.initY = missile2.y
missile2.amp = math.random(1,100)
missile2.angle = math.random(1,360)
missile2.name = “missile2”
physics.addBody(missile2, “static”, {density = .1, bounce = .1, friction = .2})
screenGroup:insert(missile2)

end

local function rocketCollide(self,event)
local collideWithName = event.name
if (collideWithName ~= “ceiling”) then
onCollision()
end
end

local function scrollField(self,event)
if self.x < -6075 then
self.x = 6077
else
self.x = self.x - self.speed
end
end

local function moveMissile(self,event)
if self.x < -50 then
self.x = 500
self.y = math.random(20,300)
self.speed = math.random(2,5)
self.amp = math.random(20,50)
self.angle = math.random(1,200)
else
self.x = self.x - self.speed
self.angle = self.angle + .1
self.y = self.amp * math.sin(self.angle)+self.initY
end
end

function activateRocket(self,event)
self:applyForce(0, -12, self.x, self.y)
end

function onTouch(event)
if event.phase == “began” then
rocket.enterFrame = activateRocket
Runtime:addEventListener(“enterFrame”, rocket)
end

if event.phase == “ended” then
Runtime:removeEventListener(“enterFrame”, rocket)
end
end

function onCollision(event)
if event.phase == “began” then
storyboard.gotoScene(“level2”, “fade”, 400)
end
end
function scene:enterScene(event)

backgroundKep.enterFrame = scrollField
Runtime:addEventListener(“enterFrame”, backgroundKep)

backgroundKep2.enterFrame = scrollField
Runtime:addEventListener(“enterFrame”, backgroundKep2)

missile1.enterFrame = moveMissile
Runtime:addEventListener(“enterFrame”, missile1)

missile2.enterFrame = moveMissile
Runtime:addEventListener(“enterFrame”, missile2)

Runtime:addEventListener(“touch”, onTouch)

Runtime:addEventListener(“collision”, onCollision)

end

function scene:exitScene(event)
Runtime:removeEventListener(“touch”, onTouch)

Runtime:removeEventListener(“enterFrame”, backgroundKep)

Runtime:removeEventListener(“enterFrame”, backgroundKep2)

Runtime:removeEventListener(“enterFrame”, missile1)

Runtime:removeEventListener(“enterFrame”, missile2)

Runtime:removeEventListener(“collision”, onCollision)
end

function scene:destroyScene(event)

end

scene:addEventListener(“createScene”, scene)
scene:addEventListener(“enterScene”, scene)
scene:addEventListener(“exitScene”, scene)
scene:addEventListener(“destroyScene”, scene)

return scene [import]uid: 20272 topic_id: 34859 reply_id: 138723[/import]

[lua]-----------------------------------------------------------------------------------------

– Restart.lua


screenW = display.contentWidth
screenH = display.contentHeight
display.setStatusBar(display.HiddenStatusBar)

local storyboard = require (“storyboard”)
local scene = storyboard.newScene()

local phyics = require “physics” ; physics.setDrawMode( “normal” )
physics.start()
function scene:createScene(event)
local screenGroup = self.view
background = display.newImage(“restart.png”)
screenGroup:insert(background)
end

function start(event)
if event.phase == “began” then
storyboard.gotoScene(“start”)
end
end

function scene:enterScene(event)
background:addEventListener(“touch”, start)
end

function scene:exitScene(event)
background:removeEventListener(“touch”, start)
end

function scene:destroyScene(event)

end

scene:addEventListener(“createScene”, scene)
scene:addEventListener(“enterScene”, scene)
scene:addEventListener(“exitScene”, scene)
scene:addEventListener(“destroyScene”, scene)

return scene [import]uid: 20272 topic_id: 34859 reply_id: 138725[/import]

[lua]-----------------------------------------------------------------------------------------

– level2.lua


local storyboard = require (“storyboard”)
local scene = storyboard.newScene()

screenW = display.contentWidth
screenH = display.contentHeight
display.setStatusBar(display.HiddenStatusBar)

local phyics = require “physics” ; physics.setDrawMode( “normal” )
physics.start()

– Boundaries
ceiling = display.newRect (0, screenH - 350, screenW, 0)
ceiling.name = “ceiling”
floor = display.newRect (0, screenH + 30, screenW, 0)
floor.name = “floor”

– Make Walls Physical
physics.addBody (ceiling, “static”, {friction = .2, bounce = .1})
physics.addBody (floor, “static”, {friction = .2, bounce = .1})

function scene:createScene(event)

local screenGroup = self.view

– Background Images

backgroundInc = display.newImage(“bgInc.png”, true)
backgroundInc:setReferencePoint(display.BottomLeftReferencePoint)
backgroundInc.x = 0
backgroundInc.y = 320
backgroundInc.speed = 8
screenGroup:insert(backgroundInc)

backgroundInc2 = display.newImage(“bgInc.png”, true)
backgroundInc2:setReferencePoint(display.BottomLeftReferencePoint)
backgroundInc2.x = 8190
backgroundInc2.y = 320
backgroundInc2.speed = 8
screenGroup:insert(backgroundInc2)

– Rocket
rocket = display.newImage(“rocket.png”)
rocket.x = 100
rocket.y = 160
rocket.name = “rocket”
physics.addBody(rocket, “dynamic”, {density = .1, friction = .5, radius = 25})
screenGroup:insert(rocket)

– Missiles
missile1 = display.newImage(“missile.png”)
missile1.x = 500
missile1.y = 100
missile1.speed = math.random(4,12)
missile1.initY = missile1.y
missile1.amp = math.random(1,100)
missile1.angle = math.random(1,360)
missile1.name = “missile1”
physics.addBody(missile1, “static”, {density = .1, bounce = .1, friction = .2})
screenGroup:insert(missile1)

missile2 = display.newImage(“missile.png”)
missile2.x = 500
missile2.y = 250
missile2.speed = math.random(7,15)
missile2.initY = missile2.y
missile2.amp = math.random(1,100)
missile2.angle = math.random(1,360)
missile2.name = “missile2”
physics.addBody(missile2, “static”, {density = .1, bounce = .1, friction = .2})
screenGroup:insert(missile2)

end

local function scrollBack(self,event)
if self.x < -8190 then
self.x = 8190
else
self.x = self.x - self.speed
end
end

local function moveMissile(self,event)
if self.x < -50 then
self.x = 500
self.y = math.random(20,300)
self.speed = math.random(2,5)
self.amp = math.random(20,50)
self.angle = math.random(1,200)
else
self.x = self.x - self.speed
self.angle = self.angle + .1
self.y = self.amp * math.sin(self.angle)+self.initY
end
end

function activateRocket(self,event)
self:applyForce(0, -12, self.x, self.y)
end

function onTouch(event)
if event.phase == “began” then
rocket.enterFrame = activateRocket
Runtime:addEventListener(“enterFrame”, rocket)
end

if event.phase == “ended” then
Runtime:removeEventListener(“enterFrame”, rocket)
end
end

function onCollision(event)
if event.phase == “began” then
storyboard.gotoScene(“restart”, “fade”, 400)
end
end
function scene:enterScene(event)

backgroundInc.enterFrame = scrollBack
Runtime:addEventListener(“enterFrame”, backgroundInc)

backgroundInc2.enterFrame = scrollBack
Runtime:addEventListener(“enterFrame”, backgroundInc2)

missile1.enterFrame = moveMissile
Runtime:addEventListener(“enterFrame”, missile1)

missile2.enterFrame = moveMissile
Runtime:addEventListener(“enterFrame”, missile2)

Runtime:addEventListener(“touch”, onTouch)

Runtime:addEventListener(“collision”, onCollision)

end

function scene:exitScene(event)
Runtime:removeEventListener(“touch”, onTouch)

Runtime:removeEventListener(“enterFrame”, backgroundInc)

Runtime:removeEventListener(“enterFrame”, backgroundInc2)

Runtime:removeEventListener(“enterFrame”, missile1)

Runtime:removeEventListener(“enterFrame”, missile2)

Runtime:removeEventListener(“collision”, onCollision)
end

function scene:destroyScene(event)

end

scene:addEventListener(“createScene”, scene)
scene:addEventListener(“enterScene”, scene)
scene:addEventListener(“exitScene”, scene)
scene:addEventListener(“destroyScene”, scene)

return scene [import]uid: 20272 topic_id: 34859 reply_id: 138724[/import]

Right Now I did something and don’t know whats wrong. RIght now my start works and goes to tutorial… it plays as supposed to then level 1 comes up and my rocket stays put at its starting position. I cant move it and I’m getting errors from lines 102 an 103 saying attempt to call method ‘applyForce’ (a nil value). Ill post my code…

Main [lua]-----------------------------------------------------------------------------------------

– Main.lua


display.setStatusBar(display.HiddenStatusBar)

local storyboard = require “storyboard”
storyboard.gotoScene(“start”)



– start.lua


screenW = display.contentWidth
screenH = display.contentHeight
display.setStatusBar(display.HiddenStatusBar)

local storyboard = require (“storyboard”)
local scene = storyboard.newScene()

local phyics = require “physics” ; physics.setDrawMode( “normal” )
physics.start()
function scene:createScene(event)
local screenGroup = self.view
background = display.newImage(“startScreen.png”)
screenGroup:insert(background)
end

function start(event)
if event.phase == “began” then
storyboard.gotoScene(“tutorial”)
end
end

function scene:enterScene(event)
background:addEventListener(“touch”, start)
end

function scene:exitScene(event)
background:removeEventListener(“touch”, start)
end

function scene:destroyScene(event)

end

scene:addEventListener(“createScene”, scene)
scene:addEventListener(“enterScene”, scene)
scene:addEventListener(“exitScene”, scene)
scene:addEventListener(“destroyScene”, scene)

return scene [import]uid: 20272 topic_id: 34859 reply_id: 138720[/import]