Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Removing events listeners in functions
Started by alexfontanellas Sep 06 2018 06:37 PM

5 replies to this topic
function event remove
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

alexfontanellas

[GLOBAL: userInfoPane.html]
alexfontanellas
  • Observer

  • 6 posts
  • Corona SDK

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

 

 



[TOPIC: post.html]
#2

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 6,973 posts
  • Corona SDK

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")

  • alexfontanellas likes this

[TOPIC: post.html]
#3

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 6,973 posts
  • Corona SDK

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

Edited by roaminggamer, 06 September 2018 - 06:45 PM.

  • alexfontanellas likes this

[TOPIC: post.html]
#4

alexfontanellas

[GLOBAL: userInfoPane.html]
alexfontanellas
  • Observer

  • 6 posts
  • Corona SDK

 

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")

 

Thanks!



[TOPIC: post.html]
#5

alexfontanellas

[GLOBAL: userInfoPane.html]
alexfontanellas
  • Observer

  • 6 posts
  • Corona SDK

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?



[TOPIC: post.html]
#6

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 6,973 posts
  • Corona SDK

Why not? I always return an object if I make it, in case I should need to use the function that way later.




[topic_controls]
[/topic_controls]

Also tagged with one or more of these keywords: function, event, remove