Reputation: 1390
I have initialized the object named jet to collide when it interacts with other objects, But when i move the jet and touch the ceiling or floor they are also exploding. How to avoid getting collision function with ceiling & Floor? Here is the code.
Ceiling Object
ceiling = display.newImage("invisibleTile.png")
ceiling:setReferencePoint(display.BottomLeftReferencePoint)
ceiling.x = 0
ceiling.y = 0
physics.addBody(ceiling, "static", {density=.1, bounce=0.1, friction=.2})
screenGroup:insert(ceiling)
local groundShape = { -280,-20, 280,-20, 280,20, -280,20 }
Floor Object
theFloor = display.newImage("invisibleTile.png")
theFloor:setReferencePoint(display.BottomLeftReferencePoint)
theFloor.x = 0
theFloor.y = 300
physics.addBody(theFloor, "static", {density=.1, bounce=0.1, friction=.2, shape=groundShape })
screenGroup:insert(theFloor)
Jet Object
jetSpriteSheet = sprite.newSpriteSheet("greenman.png", 128, 128)
jetSprites = sprite.newSpriteSet(jetSpriteSheet, 1, 4)
sprite.add(jetSprites, "jets", 1, 15, 500, 0)
jet = sprite.newSprite(jetSprites)
jet.x = 180
jet.y = 280
jet:prepare("jets")
jet:play()
**jet.collided = false**
physics.addBody(jet, {density=0.1, bounce=0.5, friction=1, radius=12})
explosive method
function explode()
explosion.x = jet.x
explosion.y = jet.y
explosion.isVisible = true
explosion:play()
jet.isVisible = false
timer.performWithDelay(3000, gameOver, 1)
end
Collision method
function onCollision(event)
if event.phase == "began" then
if jet.collided == false then
jet.collided = true
jet.bodyType = "static"
explode()
storyboard.gotoScene("restart", "fade", 400)
end
end
end
Where exactly need to specify the condition to avoid colloids objects ?please help me to resolve this issue
Upvotes: 1
Views: 191
Reputation: 1819
Note, if you want to not detect collisions all by themselves, you should filter collisions. This link. contains a helpful table and tutorial to help you determine the maskbits and categorybits. Another alternative is group indexing, which you can read more on the first link. at the end of the document.
But, from what i see on your code, your ceiling and floor might be exploding as well. is happening is that your collision function onCollision
is treating all objects the same. You would need to assign a name to your jet object jet.name = "jet"
, then, on your collision function check if the object really is the jet:
function onCollision(event)
if event.phase == "began" and "jet" == event.object1.name then
if jet.collided == false then
jet.collided = true
jet.bodyType = "static"
explode()
storyboard.gotoScene("restart", "fade", 400)
end
end
end
Note that you should also assign a name to all other physics objects, in case you would like to do something special with each body, and don't forget to reset jet.collided
once you restart your game, if it implements it.
Read more about collision detection on the Corona Docs.
Upvotes: 3