Order matters, watch the craziness. I’ve dealt with this and I’ve been all over the map with it.
First, let’s try this
#1 run this
-- Require in our storyboard, scene, and physics local physics = require("physics") physics.start(true) physics.setDrawMode("normal") --physics.setContinuous( true ) --Not sure if this will be needed, after a thorough testing on devices might take out. physics.setGravity(0, 10) physics.setPositionIterations(8) local group = display.newGroup() --====================================================================================== -- Set up display objects --====================================================================================== local alien = display.newCircle(100,100,20) -- bs values, this will change and be proper later. alien:setFillColor(255,0,0) physics.addBody( alien, "dynamic", {radius = 20} ) group:insert(alien) alien.type = "alien" local planet = display.newCircle(100,400,100) -- bs values, this will change and be proper later. physics.addBody( planet, "static",{radius = 20} ) group:insert(planet) planet.type = "planet" --====================================================================================== -- COLLISION DETECTION --======================================================================================- local function onCollision(event) if event.phase == "began" then local agro = event.object1 local hit = event.object2 --print("agro.type, hit.type", agro.type, hit.type) if agro.type == "alien" and hit.type == "planet" then display.remove(hit) hit = nil print ("alien hitting planet") elseif agro.type == "planet" and hit.type == "alien" then display.remove(hit) hit = nil print ("planet hitting alien") end end end Runtime:addEventListener("collision", onCollision)
#2 Then try this.
-- Require in our storyboard, scene, and physics local physics = require("physics") physics.start(true) physics.setDrawMode("normal") --physics.setContinuous( true ) --Not sure if this will be needed, after a thorough testing on devices might take out. physics.setGravity(0, 10) physics.setPositionIterations(8) local group = display.newGroup() --====================================================================================== -- Set up display objects --====================================================================================== local planet = display.newCircle(100,400,100) -- bs values, this will change and be proper later. physics.addBody( planet, "static",{radius = 20} ) group:insert(planet) planet.type = "planet" local alien = display.newCircle(100,100,20) -- bs values, this will change and be proper later. alien:setFillColor(255,0,0) physics.addBody( alien, "dynamic", {radius = 20} ) group:insert(alien) alien.type = "alien" --====================================================================================== -- COLLISION DETECTION --======================================================================================- local function onCollision(event) if event.phase == "began" then local agro = event.object1 local hit = event.object2 --print("agro.type, hit.type", agro.type, hit.type) if agro.type == "alien" and hit.type == "planet" then display.remove(hit) hit = nil print ("alien hitting planet") elseif agro.type == "planet" and hit.type == "alien" then display.remove(hit) hit = nil print ("planet hitting alien") end end end Runtime:addEventListener("collision", onCollision)
Notice which one prints to console. The order declared, is the order the collision happens - so it appears. Also notice that I even put 2 different collision pieces in one for alien then planet, and then planet and alien.
Collision function:
local function onCollision(event) if event.phase == "began" then local agro = event.object1 local hit = event.object2 --print("agro.type, hit.type", agro.type, hit.type) if agro.type == "alien" and hit.type == "planet" then display.remove(hit) hit = nil print ("alien hitting planet") elseif agro.type == "planet" and hit.type == "alien" then display.remove(hit) hit = nil print ("planet hitting alien") end end end Runtime:addEventListener("collision", onCollision)
THEN I got crazy, and lets put your code snippet in :
**Note I changed .name to .type to keep the same as my code above
local function onCollision(event) if event.phase == "began" then local agro = event.object1 local hit = event.object2 --print("agro.type, hit.type", agro.type, hit.type) if event.object1.type == "alien" and event.object2.type == "planet" --print("alien and planet collide") or event.object1.type == "planet" and event.object2.type == "alien" ---print ("planet and alien collide") then display.remove(hit) hit = nil end end end Runtime:addEventListener("collision", onCollision)
That removes the "alien. But wait, lets switch around the alien and planet!
-- Require in our storyboard, scene, and physics local physics = require("physics") physics.start(true) physics.setDrawMode("normal") --physics.setContinuous( true ) --Not sure if this will be needed, after a thorough testing on devices might take out. physics.setGravity(0, 10) physics.setPositionIterations(8) local group = display.newGroup() --====================================================================================== -- Set up display objects --====================================================================================== local alien = display.newCircle(100,100,20) -- bs values, this will change and be proper later. alien:setFillColor(255,0,0) physics.addBody( alien, "dynamic", {radius = 20} ) group:insert(alien) alien.type = "alien" local planet = display.newCircle(100,400,100) -- bs values, this will change and be proper later. physics.addBody( planet, "static",{radius = 20} ) group:insert(planet) planet.type = "planet" --====================================================================================== -- COLLISION DETECTION --======================================================================================- local function onCollision(event) if event.phase == "began" then local agro = event.object1 local hit = event.object2 --print("agro.type, hit.type", agro.type, hit.type) if event.object1.type == "alien" and event.object2.type == "planet" --print("alien and planet collide") or event.object1.type == "planet" and event.object2.type == "alien" ---print ("planet and alien collide") then display.remove(hit) hit = nil end end end Runtime:addEventListener("collision", onCollision) --[[local function onCollision(event) if event.phase == "began" then local agro = event.object1 local hit = event.object2 --print("agro.type, hit.type", agro.type, hit.type) if agro.type == "alien" and hit.type == "planet" then display.remove(hit) hit = nil print ("alien hitting planet") elseif agro.type == "planet" and hit.type == "alien" then display.remove(hit) hit = nil print ("planet hitting alien") end end end Runtime:addEventListener("collision", onCollision) --]]
Now the dang planet got removed, even using your logic.
I know I know, insane. I need a drink