Hi there, I’m having an error when removing an object from a custom event raised in a touch handler, in this event I’m only destroying the object by obj:removeself() and obj = nil.
If I double click fast enough ( two touches ) on the area that is listening touches, I get the error "attempt to call method ‘dispatchEvent’ (a nil value).
Here’s my code simplified:
function playArea:spawn()
local obj = Objeto:new()
obj.id = "obj\_" .. lastObjNum
playArea:insert(obj)
obj:setReferencePoint ( display.CenterReferencePoint )
obj.x = playArea.xMiddle
transition.to (obj, {time=3000, y = \_H + 50})
obj:addEventListener ( "objetoDestruido", self )
currentObj = obj
lastObjNum = self.lastObjNum +1
end
function playArea:objetoDestruido(event)
print("event objetoDestruido for" .. event.target.id)
Score:updateScore(self.nombre, "normal")
print("Before destroying")
print(event.target)
event.target:removeSelf()
event.target = nil
print("After destroying")
print(event.target)
end
function playArea:touch(event)
...
if event.phase == "ended" then
print(currentObj)
if (currentObj ~= nil) and (ultimoObj.gesture == gesture) then
currentObj:dispatchEvent( { name="objetoDestruido", target=currentObj } )
end
end
end
And here the messages logged:
1.- touch <2.- table: 0x535af0 <3.- Before destroying --> table: 0x535af0 (event.target)
4.- event objetoDestruido for obj_27
5.- After destroying --> nil (event.target)
6.- touch <7.- table: 0x535af0 <
But this time I get the error above mentioned, and I wonder why is the object on step 7 still not cleansed… Do I have to set some delay in the custom event handler in order to give some time for the object to be removed?.
Runtime error
…an/Dropbox/Desarrollo/CoronaSDK/test2App/PlayArea.lua:183: attempt to call method ‘dispatchEvent’ (a nil value)
stack traceback:
Thanks in advance. [import]uid: 8933 topic_id: 23299 reply_id: 323299[/import]