Can you prevent collision from specified objects?

I’ve got a little side scroller game going and have my rocket and a few enemies flying around. I have phyics also on my ceiling and floor so the rocket can rest on the ground or touch the top. Now I have an onCollision function that I would like to have work when I hit an enemy but not the ceiling or floors… Right now the game restarts if I touch anything. I want to be able to touch the ceiling and floor with no actions taken. I can’t figure a way to do this. If you guys need more info or need to see some code let me know. Thanks [import]uid: 20272 topic_id: 34859 reply_id: 334859[/import]

Hi @mkjt88,
The solution is collision filters. You can read a full walkthrough here:
http://developer.coronalabs.com/forum/2010/10/25/collision-filters-helper-chart

Let me know if you have any other questions,
Best regards,
Brent
[import]uid: 200026 topic_id: 34859 reply_id: 138521[/import]

Hi mkjt88,

If you need help setting up collision filters then you might want to check out the collision editor in Level Director (www.retrofitproductions.com) .
It has made my life so much easier! [import]uid: 158620 topic_id: 34859 reply_id: 138531[/import]

Hi Brent that works perfectly as described but I still have one problem. Unless I’m missing something I still need to make my walls able to collide wiith my rocket so to speak to where I stop when I touch them but I do not do my onCollision function when I touch them. I want to collide wiht the walls but don’t do anything besides keep me in bounds but when I collide with missiles etc I want to act on the onCollision function I have… Is there a way to use this collision filter technique to do this? Thanks a lot I’ve learned much since being here. [import]uid: 20272 topic_id: 34859 reply_id: 138544[/import]

Hi @mkjt88,
Glad to help… but not sure what you mean by the last request. Do you mean, the “rocket” should collide with the walls, but only for the purpose of keeping it “in bounds”? But for colliding with missiles, it suffers some kind of damage?

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

Yes exactly. I made my walls ewRect objects and got them about 10 px off screen on my ceiling and floor. I added physics to them and had it working fine until I added my enemy missiles. Once everything had physics it was still working but after I added my onCollision function my rocket calls the function rather I collide with the walls or missiles. I just want the walls to be boundries so the rocket wont fly out of the screen. I’m away from the computer right now I’ll post my code if you need it when I’m around it. [import]uid: 20272 topic_id: 34859 reply_id: 138583[/import]

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]

Hi @mkjt88,
The solution is collision filters. You can read a full walkthrough here:
http://developer.coronalabs.com/forum/2010/10/25/collision-filters-helper-chart

Let me know if you have any other questions,
Best regards,
Brent
[import]uid: 200026 topic_id: 34859 reply_id: 138521[/import]

Hi mkjt88,

If you need help setting up collision filters then you might want to check out the collision editor in Level Director (www.retrofitproductions.com) .
It has made my life so much easier! [import]uid: 158620 topic_id: 34859 reply_id: 138531[/import]

Hi Brent that works perfectly as described but I still have one problem. Unless I’m missing something I still need to make my walls able to collide wiith my rocket so to speak to where I stop when I touch them but I do not do my onCollision function when I touch them. I want to collide wiht the walls but don’t do anything besides keep me in bounds but when I collide with missiles etc I want to act on the onCollision function I have… Is there a way to use this collision filter technique to do this? Thanks a lot I’ve learned much since being here. [import]uid: 20272 topic_id: 34859 reply_id: 138544[/import]

Hi @mkjt88,
Glad to help… but not sure what you mean by the last request. Do you mean, the “rocket” should collide with the walls, but only for the purpose of keeping it “in bounds”? But for colliding with missiles, it suffers some kind of damage?

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

Yes exactly. I made my walls ewRect objects and got them about 10 px off screen on my ceiling and floor. I added physics to them and had it working fine until I added my enemy missiles. Once everything had physics it was still working but after I added my onCollision function my rocket calls the function rather I collide with the walls or missiles. I just want the walls to be boundries so the rocket wont fly out of the screen. I’m away from the computer right now I’ll post my code if you need it when I’m around it. [import]uid: 20272 topic_id: 34859 reply_id: 138583[/import]