Hi everybody!
I’m having a performance iusse in my game. The game starts smooth but after a few minutes of playing it starts lagging, and it keeps doing it even if the game change the composer scene (i.e. game finished and started again). It’s clear that there’s something wrong in my code and I’m trying to find out what.
There are two thing I’m not sure about: the first is the use I’m making of the enterFrame event applied on a display object. Example:
local function create\_my\_object() local my\_object = display.newImageRect(...); --inizialize useful variables applied to my\_object function my\_object:enterFrame() --object behaviour... end function my\_object:finalize() Runtime:removeEventListener("enterFrame", myObject); end Runtime:addEventListener("enterFrame", my\_object); my\_object:addEventListener("finalize"); end
If i call this function many times, to create different instances of this object, is it ok? Or I’m forgetting to clean up something after the object is removed?
Note that the object my_object can be removed in many different ways, inside and outside this function. For example it can be removed with a onComplete function inside a transition.to, or it can be removed by another function that detects collisions, or the game can just change the composer scene removing all objects linked to the display group.
The second question, more important, is about cleaning up variable reference to objects.
For example, if I call the function create_my_object from another function, the reference to the object my_object is a local variable inside the create_my_object function.
If the code that removes the object is inside the create_my_object function, I can write display.remove(my_object); my_object = nil;
But if the object is removed by another function, i.e. collision detection:
local function on\_collision(event) if (event.object1.myName == "enemy") and (event.object2.myName == "bullet") or (.....) then local what\_object; if (event.object1.myName == "enemy") then what\_object = event.object1; else what\_object = event.object2; end display.remove(what\_object); what\_object = nil; end end
In this case I cannot set to nil the original variable associated with the enemy object.
Should I write in every function that generates object a finalize function that sets to nil the local reference, or it is done automatically?