Removing event listener on collision

local function hasCollided(obj1, obj2)

if obj1 == nil then
        return false
    end
    if obj2 == nil then
        return false
    end
    local sqrt = math.sqrt
    local dx =  obj1.x - obj2.x; – error points to this line
    local dy =  obj1.y - obj2.y;
    local distance = sqrt(dx*dx + dy*dy);
    local objectSize = (obj2.contentWidth/2) + (obj1.contentWidth/2)
    if distance < objectSize then
        return true
    end
    return false
end

objects = {object1,object2,object3}

local function testCollisions()

  for i=1,3 do
    for j=1,3 do
      if i ~= j then
        if hasCollided(objects[i], objects[j]) then
          --do my collision stuff here
        end
      end       
    end
  end
end

Runtime:addEventListener(“enterFrame”, testCollisions)

In destroyScene, I do Runtime:removeEventListener(“enterFrame”, testCollisions).

When I try to exit the scene,  it returns me an error saying it’s attempting to return an arithmetic value on x (a nil value) on the line above. Is there any reason for this behaviour?

 

Probably its because of your if/else statement. If obj1 or obj2 is nil it will still pass through the next line of code.

Try something like this.

local function hasCollided(obj1, obj2) if obj1 == nil or obj2 == nil then -- either one is nil return false else -- obj1, obj2 both are not nil local sqrt = math.sqrt local dx = obj1.x - obj2.x; -- error points to this line local dy = obj1.y - obj2.y; local distance = sqrt(dx\*dx + dy\*dy); local objectSize = (obj2.contentWidth/2) + (obj1.contentWidth/2) if distance \< objectSize then return true end end

burhan

Probably because you removed the objects before you canceled the event listener. Try to cancel the event listener before you remove the objects. Note that destroyScene only gets called when you call removeScene or storyboard destroys it on low memory warning. You should cancel your event listeners in exitScene. Also when you remove the object set objects table entry for that object to nil so that your check will stop it from being used.

Thanks for the replies. I’ll try your suggestions.

EDIT: I found the culprit. I wasn’t manually clearing out the objects just yet so I thought it was weird that it would throw errors. I basically used a customized storyboard tutorial which had storyboard.removeAll() at the beginning of every scene. I removed it and now it works fine. Sorry for the bother.

Since we’re on the subject, how do you people handle the removal of objects, timers, transitions etc (since there are so many ways to do it)?

 

Probably its because of your if/else statement. If obj1 or obj2 is nil it will still pass through the next line of code.

Try something like this.

local function hasCollided(obj1, obj2) if obj1 == nil or obj2 == nil then -- either one is nil return false else -- obj1, obj2 both are not nil local sqrt = math.sqrt local dx = obj1.x - obj2.x; -- error points to this line local dy = obj1.y - obj2.y; local distance = sqrt(dx\*dx + dy\*dy); local objectSize = (obj2.contentWidth/2) + (obj1.contentWidth/2) if distance \< objectSize then return true end end

burhan

Probably because you removed the objects before you canceled the event listener. Try to cancel the event listener before you remove the objects. Note that destroyScene only gets called when you call removeScene or storyboard destroys it on low memory warning. You should cancel your event listeners in exitScene. Also when you remove the object set objects table entry for that object to nil so that your check will stop it from being used.

Thanks for the replies. I’ll try your suggestions.

EDIT: I found the culprit. I wasn’t manually clearing out the objects just yet so I thought it was weird that it would throw errors. I basically used a customized storyboard tutorial which had storyboard.removeAll() at the beginning of every scene. I removed it and now it works fine. Sorry for the bother.

Since we’re on the subject, how do you people handle the removal of objects, timers, transitions etc (since there are so many ways to do it)?