I am looking forward to your Director class. Is it going to handle (remove from memory) tables, listeners and other data-related memory consumers created inside the lua files?
Many thanks! [import]uid: 7356 topic_id: 2447 reply_id: 7211[/import]
For now, I’m using the “tab bar” example for controlling that. Each lua file must have a display group and the functions new() and cleanUp() that are called from director. [import]uid: 8556 topic_id: 2447 reply_id: 7212[/import]
Ricardo, thanks for sharing! Excellent set of transitions…
Some notes:
Why don’t you replace the multiple fxEnded local functions with a single one out of the “if then elseif” statement.
Also, inside that fxEnded function you only remove the previous display group.
Consider adding a child=nil assignment in order to also delete other data the user has attached to the display group’s table. Same applies for the group’s object, so I would firstly cycle through the child’s children and remove + nulify them.
Thanks again [import]uid: 7356 topic_id: 2447 reply_id: 7253[/import]
Ricardo, from what I see you use fxEnded for two things:
a) reset vars
b) removals
So, b) could be isolated and delivered from a function outside your “if” statement. This would make the code cleaner and your future work on improving the function simpler, as you would only change it in one point instead of 10.
Having said that, I like your project very much. What I think is missing for consider it perfect is a better memory cleaner that would automate the whole process of “deleting” the previous screen and all of its memory consumers. I am a newbie in lua, but I am going to try helping on that…
Ok, let’s improve that! I putted the fxEnded out of the changeScene function, it’s now like this:
local function fxEnded ( event )
currentView.x = 0
currentView.y = 0
currentView.xScale = 1
currentView.yScale = 1
--
while (currentView.numChildren \> 0) do
currentView[currentView.numChildren] = nil
currentView:remove(currentView.numChildren)
end
--
currentScreen = nextScreen
currentView:insert(currentScreen)
nextView.x = display.contentWidth
nextView.y = 0
nextView.xScale = 1
nextView.yScale = 1
end
The problem is that because of that currentView[currentView.numChildren] = nil assignment, I’m getting the message WARNING: Attempting to set property(1) with nil at the Terminal. Is that right? Carlos? [import]uid: 8556 topic_id: 2447 reply_id: 7273[/import]
In this case you can only clean 2 levels. I’m thinking in something to go into the entire tree.
Imagine this:
local g1 = display.newGroup()
local g2 = display.newGroup()
local g3 = display.newGroup()
local g4 = display.newGroup()
g1:insert(g2)
g2:insert(g3)
g3:insert(g4)
local q1 = display.newImage("logo.png",100,100)
local q2 = display.newImage("logo.png",300,100)
local q3 = display.newImage("logo.png",100,300)
local q4 = display.newImage("logo.png",300,300)
g1:insert(q1)
g2:insert(q2)
g3:insert(q3)
g4:insert(q4)
How can I go though all four groups and clean them? [import]uid: 8556 topic_id: 2447 reply_id: 7276[/import]
Ok, I would first go deep inside the levels checking for the existance of .numChildren with an assert and increasing a “levelsnum” var by +1.
Then I would construct a while loop using .parent to climb up the levels removing+nulifying whatever exists.
Tricky! (but not impossible)
The biggest problem, however, is how to find where listeners and timers are stored to delete them too, climbing all the way up.
I am going to work on it tomorrow… [import]uid: 7356 topic_id: 2447 reply_id: 7278[/import]
[code]
local g1 = display.newGroup()
local g2 = display.newGroup()
local g3 = display.newGroup()
local g4 = display.newGroup()
g1:insert(g2)
g2:insert(g3)
g3:insert(g4)
local q1 = display.newImage(“logo.png”,100,100)
local q2 = display.newImage(“logo.png”,300,100)
local q3 = display.newImage(“logo.png”,100,300)
local q4 = display.newImage(“logo.png”,300,300)
local function cleanGroups ( curGroup )
print ("curGroup = " … tostring(curGroup.name) … " - children = " … tostring(curGroup.numChildren))
if curGroup.numChildren then
while curGroup.numChildren > 0 do
cleanGroups ( curGroup[curGroup.numChildren] )
end
curGroup:removeSelf()
else
curGroup:removeSelf()
curGroup = nil
return
end
end
Cleaning up doesn’t seem to work properly.
Try this, for two screens (“p1”,“p2”) that return a “lg” group
[lua]director:changeScene(“p1”,“moveFromRight”)
print(p1.lg==nil) – lg is the returning (global) group of p1 screen
local function onTap(event)
if “began” == event.phase then
director:changeScene(“p2”,“moveFromRight”)
print(p1.lg==nil) --should be nil, but it isn’t
end
end
Runtime:addEventListener(“touch”, onTap) – LISTENER --[/lua]
– Import director class
local director = require(“director”)
– Create a main group
local mainGroup = display.newGroup()
– Main function
local function main()
– Add the group from director class
mainGroup:insert(director.directorView)
– Change scene without effects
director:changeScene(“screen1”)
print(screen1.lg==nil)
local function onTap(event)
if “began” == event.phase then
director:changeScene(“screen2”,“moveFromRight”)
print(screen1.lg==nil) --should be nil, but it isn’t
end
end
Runtime:addEventListener(“touch”, onTap) – LISTENER –
Ricardo, you get always true (object==nil) because you have (properly) set your objects as *locals*, so you can not see them from main.lua. You would also get true if you checked for a non-existent variable (because it doesn’t exist, it is “nil”).
Try to set your returning group as a *global* one for it to be accessible from main. You will get false (group table exists after screen change!)
For some reason, function cleanGroups removes the display objects from screen but *do not* nullify the objects as tables. To understand why the latter is essential, imagine a module in which you download 5mb of data from the web and attach this data as a property to the group (group.data), so it can be deleted on screen change. If the group is removed graphically but not deleted as a table, there is no memory management at all. Display objects removal is just the first step. The hard part is tables, timers, listeners etc…
PS: How can I access the current screen’s name from main.lua? Thanks! [import]uid: 7356 topic_id: 2447 reply_id: 7335[/import]
Some variables aren’t accessible programmatically, like timers. I tried to clean all but for some you have to do it manually. If there’s a way to clean up this kind of variables, I will put it into the cleanGroups function! I’m not an expert, I’m still learning how to use Corona.