Adding runtime listeners from a touch response

Hi everyone,

I’m attempting to add a runtime listener to a ninja star object after the user touches a button in the lower right hand corner of the screen.  What I’m trying to do is add an enterFrame listener to the object so that when it reaches a certain point on the screen it will remove itself.  I have made the object have its own attribute such as ninjaStar.type = “weapon” so that the runtime function looks for this and processes the removal.

Also I’m creating the object from two different external modules.  One module contains the code to make the ninja star and the other is just a place for the object to be placed into for tracking purposes.

Here’s the chunk of code that is supposed to add the listener.

local function attack(event)

    local startX, startY = character.x + 18, character.y

        

        globalSpawn.weapons[1] = weapons.star(startX, startY)

        Runtime:addEventListener(“enterFrame”, globalSpawn.weapons[1])

end

What I need to know is whether Corona has stipulations on when you can add runtime listeners.

Any help would be greatly appreciated!!!

Hi @r.barber07,

From a basic design standpoint, there aren’t really “limits” on when you can add a Runtime listener… but in your case, if there might be several ninja stars being “tracked” at any one time, it’s probably better if you create one Runtime listener to watch all stars, looping through all of the stars on the screen and checking for their position (then if one reaches a limit, it is destroyed or whatever). While you can technically have as many Runtime listeners as you want, it’s better to consolidate the ones that share a common purpose.

Also, do not forget to remove your Runtime listeners when you’re done with them! These exist in the global scope, so Lua will not auto-clean them. It’s your responsibility to cancel them when changing scenes or when their service is no longer required.

Best of luck!

Brent Sorrentino

I went through and reorganized my listeners the way you stated and created a new one and it solved everything.

Thanks so much Brent!

Hi @r.barber07,

From a basic design standpoint, there aren’t really “limits” on when you can add a Runtime listener… but in your case, if there might be several ninja stars being “tracked” at any one time, it’s probably better if you create one Runtime listener to watch all stars, looping through all of the stars on the screen and checking for their position (then if one reaches a limit, it is destroyed or whatever). While you can technically have as many Runtime listeners as you want, it’s better to consolidate the ones that share a common purpose.

Also, do not forget to remove your Runtime listeners when you’re done with them! These exist in the global scope, so Lua will not auto-clean them. It’s your responsibility to cancel them when changing scenes or when their service is no longer required.

Best of luck!

Brent Sorrentino

I went through and reorganized my listeners the way you stated and created a new one and it solved everything.

Thanks so much Brent!