attempt to call method 'respondsToEvent' (a nil value)

I think it is happening because of ‘removeEventListener’ line

How I can fix it? Maybe I need some delay before removing listener?

   local function pickUpAntiAlien0( event )
      if event.phase == "began" then
        print("touch")
        antiAlien.removeEventListener("touch", pickUpAntiAlien0)
        if hero.weapon1 == "empty" then
          print("1 slot empty")
          hero.weapon1 = "anti-alien"
        end
      end
    end

    local function pickUpAntiAlien( event )
      if event.phase == "began" then
        print("start")
        antiAlien:addEventListener("touch", pickUpAntiAlien0)
      elseif event.phase == "ended" then
        print("end")
        antiAlien:removeEventListener("touch", pickUpAntiAlien0)
      end
    end

    antiAlien:addEventListener("collision", pickUpAntiAlien)
  1. Is respondsToEvent() your method? I’ve never heard of it.

  2. Add a check before the call to ‘respondsToEvent()’ to verify that obj is valid.

  3. That code above is very bad practice. IMHO. Adding touch events and removing them on the fly like that can result in you calling add multiple times unless you know exactly what you are doing.

It is better to add the touch event by default, then add a field ‘enableTouches’ which you examine in the listener. If it is set to ‘false’ exit the listener immediately and return false.

You can then set this field in your code (above) and it is much safer.

local function objIsValid ( obj )
	return ( obj and obj.removeSelf and type(obj.removeSelf) == "function" )
end
  1. I didn’t call any ‘respondsToEvent()’.
  2. That ‘enableTouches’ is built in into engine or I need to check it manually?

#2 - I answered that already. Not built in, something you check in your listener code.

You can make a field named anything you want and use it as a flag in your own logic. e.g. ignoreTouches might be a different way of thinking about it.