Hi, I am creating a game and I have a conceptual question that I’d like to know.
I am using the composer library to change between scenes
This is part of my code.
local coin local function spawnCoins() local sceneGroup = scene.view coin = display.newImageRect("level1/bitcoin.png",58,59) coin.x = math.random(300,17\*480) coin.y = math.random(y/4,y\*2-20) coin.name = "coin" sceneGroup:insert(coin) coin.enterFrame = scrollSnowBackground Runtime:addEventListener("enterFrame", coin) physics.addBody(coin, "dynamic", {bounce = 0, filter = filtersTable.coinFilter ,friction = 0, isSensor = true, radius = 30}) coin.gravityScale = 0 coin:addEventListener("collision", onCollisionCoin) end
Then, at the beginning of the game I call the function as follows when the scene is shown
for i = 0,8,1 do spawnCoins() end
I didn’t include other functions since I don’t think they are important to my question.
I am creating the object as a local variable to the function spawnCoins() and I am adding event listeners to it.
Do I have to remove the event listener at any point? Like when changing the scene? Or I don’t have to worry about it?
Thanks
You must remove that enterFrame listener from every coin when the coin is destroyed.
You can do that this way:
function coin.finalize( self ) Runtime:removeEventListener( "enterFrame", self ) end coin:addEventListener("finalize")
The full code would look like this:
--local coin -- this is pointless since you are making multiple local function spawnCoins() local sceneGroup = scene.view local coin = display.newImageRect("level1/bitcoin.png",58,59) coin.x = math.random(300,17\*480) coin.y = math.random(y/4,y\*2-20) coin.name = "coin" sceneGroup:insert(coin) coin.enterFrame = scrollSnowBackground Runtime:addEventListener("enterFrame", coin) physics.addBody(coin, "dynamic", { bounce = 0, filter = filtersTable.coinFilter, friction = 0, isSensor = true, radius = 30 }) coin.gravityScale = 0 coin:addEventListener("collision", onCollisionCoin) function coin.finalize( self ) Runtime:removeEventListener( "enterFrame", self ) end coin:addEventListener("finalize") return coin end
Why do I need the return coin?
Why not? I always return an object if I make it, in case I should need to use the function that way later.
You must remove that enterFrame listener from every coin when the coin is destroyed.
You can do that this way:
function coin.finalize( self ) Runtime:removeEventListener( "enterFrame", self ) end coin:addEventListener("finalize")
The full code would look like this:
--local coin -- this is pointless since you are making multiple local function spawnCoins() local sceneGroup = scene.view local coin = display.newImageRect("level1/bitcoin.png",58,59) coin.x = math.random(300,17\*480) coin.y = math.random(y/4,y\*2-20) coin.name = "coin" sceneGroup:insert(coin) coin.enterFrame = scrollSnowBackground Runtime:addEventListener("enterFrame", coin) physics.addBody(coin, "dynamic", { bounce = 0, filter = filtersTable.coinFilter, friction = 0, isSensor = true, radius = 30 }) coin.gravityScale = 0 coin:addEventListener("collision", onCollisionCoin) function coin.finalize( self ) Runtime:removeEventListener( "enterFrame", self ) end coin:addEventListener("finalize") return coin end
Why do I need the return coin?
Why not? I always return an object if I make it, in case I should need to use the function that way later.