I have a runtime listener and a collision listener for two objects. For the runtime listener the objects move to a point and as they arrive they are removed. For the collision listener as an object collides with another specific object then the first object is removed.
My issue is that with the collision it removes both objects even if only one has collided, but with the runtime it works correctly. What is the difference and why does this happen?
[code]
display.setStatusBar( display.HiddenStatusBar )
local physics = require(“physics”)
local side
local dudes = {}
math.randomseed(os.time())
physics.start()
physics.setGravity( 0, 0 )
local function makeDude()
side = math.random(2)
local dude = display.newRect(100, 100, 100, 100)
dudes[#dudes+1] = dude
physics.addBody(dude, { isSensor = true })
dude.myName = “dude”
end
local player = display.newRect(300,500,100,100)
player:setFillColor(255,0,0)
player.myName = “player”
local function touchlistener(touch)
local phase = touch.phase
local touchsquare
function removets()
touchsquare:removeSelf()
touchsquare = nil
end
if phase == “began” then
touchsquare = display.newRect(player.x - 75, player.y - 75, 150, 150)
touchsquare:setFillColor(0,0,255)
touchsquare.myName = “touchsquare”
physics.addBody(touchsquare, { density = 0.3 })
local resettimer = timer.performWithDelay(100, removets, 1)
end
end
player:addEventListener(“touch”, touchlistener)
local function listener(event)
for i, dude in pairs(dudes) do
if dude ~= nil then
if dude.x > player.x + 99 and dude.x < player.x + 101 and dude.y < player.y + 1 and dude.y > player.y - 1 then
dude:removeSelf()
dudes[i] = nil
elseif dude.x > player.x - 101 and dude.x < player.x - 99 and dude.y < player.y + 1 and dude.y > player.y - 1 then
dude:removeSelf()
dudes[i] = nil
elseif dude.x > player.x + 100 and dude.y > player.y then
dude.x = dude.x - 1
dude.y = dude.y - 1
elseif dude.x < player.x + 100 and dude.x >= player.x and dude.y > player.y then
dude.x = dude.x + 1
dude.y = dude.y - 1
elseif dude.x > player.x - 100 and dude.x < player.x and dude.y > player.y then
dude.x = dude.x - 1
dude.y = dude.y - 1
elseif dude.x <= player.x - 100 and dude.y > player.y then
dude.x = dude.x + 1
dude.y = dude.y - 1
elseif dude.x > player.x - 101 and dude.x < player.x - 99 and dude.y > player.y then
dude.y = dude.y - 1
elseif dude.x < player.x + 101 and dude.x > player.x + 99 and dude.y > player.y then
dude.y = dude.y - 1
elseif dude.x > player.x + 100 and dude.y < player.y then
dude.x = dude.x - 1
dude.y = dude.y + 1
elseif dude.x < player.x + 100 and dude.x >= player.x and dude.y < player.y then
dude.x = dude.x + 1
dude.y = dude.y + 1
elseif dude.x > player.x - 100 and dude.x < player.x and dude.y < player.y then
dude.x = dude.x - 1
dude.y = dude.y + 1
elseif dude.x <= player.x - 100 and dude.y < player.y then
dude.x = dude.x + 1
dude.y = dude.y + 1
elseif dude.x > player.x - 101 and dude.x < player.x - 99 and dude.y < player.y then
dude.y = dude.y + 1
elseif dude.x < player.x + 101 and dude.x > player.x + 99 and dude.y < player.y then
dude.y = dude.y + 1
elseif dude.y > player.y - 1 and dude.y < player.y + 1 and dude.x <= player.x - 100 then
dude.x = dude.x + 1
elseif dude.y > player.y - 1 and dude.y < player.y + 1 and dude.x > player.x - 100 and dude.x < player.x then
dude.x = dude.x - 1
elseif dude.y > player.y - 1 and dude.y < player.y + 1 and dude.x >= player.x + 100 then
dude.x = dude.x - 1
elseif dude.y > player.y -1 and dude.y < player.y + 1 and dude.x < player.x + 100 and dude.x >= player.x then
dude.x = dude.x + 1
end
end
end
end
Runtime:addEventListener(“enterFrame”, listener)
timer.performWithDelay(4000, makeDude, 2)
function onCollision( event )
for i, dude in pairs(dudes) do
if (event.object1.myName == “touchsquare” and event.object2.myName == “dude”) or (event.object2.myName == “touchsquare” and event.object1.myName == “dude”) then
print( "began: " … event.object1.myName … " & " … event.object2.myName )
dude:removeSelf()
dudes[i] = nil
end
end
end
Runtime:addEventListener( “collision”, onCollision )
[/code] [import]uid: 10903 topic_id: 7789 reply_id: 307789[/import]