Wondering if there is a way to trigger a callback on your custom object(table) when a “removeSelf()” is called upon it? Is there a way?
Background:
* Assume you have instantiated an object ( in an OO style )
* So let’s say in Main you go: inventory:removeSelf(), then a way to add a method to the class such that it would automatically pickup this callback and then you could put in here custom clean up code (e.g. removing listeners, removing ParticleCandy stuff etc)
* Idea would then be you could build you custom classes such that:
- each would have it’s own “cleanup” method
- in the parent you just call removeSelf(), without a need to call the class’s “cleanup” method first
- i.e. implying here from main you’d normally have to clean up via:
inventory:cleanup() – which you write
inventory:removeSelf()
Example of OO Style I was referring to
[code]
MAIN
Inventory = require(“inventory”)
inventory = Inventory:new()
inventory:printScore() – Works fine (using “self”)
CLASS
– OO Class called Inventory
Inventory = {}
function Inventory:new()
local inventory = display.newGroup()
– Class Contents: Start ---------------------
inventory.score = 6
local inventoryImage = display.newImage( “inventory_button.png”, 0, 0)
inventoryImage.y = display.contentHeight - inventoryImage.height/2
inventory:insert(inventoryImage)
function inventory:printScore()
print("In printScore: Score = " … self.score)
end
– NOTE: With listeners you can use “self” if “method you define via : and the event name have to match”
function inventory:touch(event)
if (event.phase == “began”) then
print("In test_Listener: Score = " … self.score)
end
end
inventory:addEventListener(“touch”, inventory)
– Class Contents: End ---------------------
return inventory
end
return Inventory
[/code] [import]uid: 140210 topic_id: 34532 reply_id: 334532[/import]