Is there a way to detect that the displayed object is being removed? Something like:
obj:addEventListeren("before_remove", function(ev)
-- ev.target will be removed soon
end);
Is there a way to detect that the displayed object is being removed? Something like:
obj:addEventListeren("before_remove", function(ev)
-- ev.target will be removed soon
end);
Do you want to check for the existence of an object before removing it? Then you can check for any of the major properties of that object with nil. Like below:
local rect = display.newRect(50,50,100,50) -- Creating an object
local function myFunction(e)
if(rect.x~=nil)then -- checking for its presence
print("Object exists. So, remove it...")
rect:removeSelf()
end
end
Runtime:addEventListener("tap",myFunction).
Keep Coding.............. :)
I have done it, but implementation relays on Corona SDK internal implementation and can stop working without notice. Looks something like this:
function AddDestructor(obj, func)
obj._isWidget = true;
if (not obj.originalRemove) then
obj.originalRemove = obj.removeSelf or (function() end);
obj.removeSelf = function(self)
for i = 1, #self.D do
self.D[i](self);
end
self:originalRemove();
end
obj.D = {};
end
table.insert(obj.D, func);
end
and you can use this code like this
local group = displsy.newGroup();
local r = display.newRect(group, 0, 0, 300, 300);
AddDestructor(r, function()
print("Tadaaaa I was called before rect died!");
end);
group:removeSelf();