I haven’t used Lua before now and have a quick question with regards to arrays of objects.
I have some arrays which look like this:
g.group[i][1] = Text
g.group[i][2] = Text
g.group[i][3] = Animation
g.group[i][4] = Image
g.group[i][5] = Text
The animation is essentially a loading animation that’s displayed while information is still being downloaded, however, the user can still click around while the info is being downloaded.
The problem is when I want the animation to be unloaded if I use display.remove or removeSelf it then readjusts the array making g.group[i][4] = g.group[i][3] (and 5 equal to 4), which then breaks my references to those elements in other code.
Is there are correct way to unload the animation from memory without removing the array entry entirely. I’ve tried setting it to nil but that errors. I can set it to a static image (which is what I want to do anyway), but I don’t think it’s then correctly unloading the animation. At the moment I’m just setting it to isVisible = false, loading an image in it’s place and carrying on. That works fine, but I’m assuming that is going to cause a pretty major memory leak over time.
Any help would be appreciated.
One other quick question too, I’ve found it easier to load specific things whether the app is loaded on iPhone or iPhone 4 as I don’t like the scaling features, however, this means that code below fails as I cannot declare the variables as local within an if statement, whats the best way to handle this?:
if display.contentWidth == 320 then
local imgArrow = display.newImage(“rightarrow_iphone.png”, true)
else
local imgArrow = display.newImage(“rightarrow.png”, true)
end
Cheers,
Will [import]uid: 61273 topic_id: 10641 reply_id: 310641[/import]
What you can do is create an additional reference to the objects you’re adding to array at the same time as you add them. This reference is also attached to the array.
local groupa = display.newGroup();
local Texta = display.newText("a", 0, 0, native.systemFont, 32);
local Anim = display.newText("Anim", 0, 0, native.systemFont, 32);
local Textb = display.newText("b", 0, 0, native.systemFont, 32);
local Textc = display.newText("c", 0, 0, native.systemFont, 32);
groupa:insert(Texta); -- add to array
groupa.Texta = Texta; -- create extra reference
groupa:insert(Anim);
groupa.Anim = Anim;
groupa:insert(Textb);
groupa.Textc = Textc;
groupa:insert(Textc);
groupa.Textb = Textb;
-- prints:
-- [3].text=b
-- Textb.text=b
print("[3].text=" .. groupa[3].text);
print("Textb.text=" .. groupa.Textb.text);
groupa:remove(2);
-- prints:
-- [3].text=c
-- Textb.text=b (so you can still get to Textb even though array index has moved)
print("[3].text=" .. groupa[3].text);
print("Textb.text=" .. groupa.Textb.text);
I’m also new to lua but I’ve seen this used elsewhere successfully by grown-ups!
Thanks for that Mark…not a bad way of doing things, although not sure of the extra variables. Ended up just creating placeholders e.g.
g.group[i][2].remove
g.group[i]:insert(2, “”)
So as long as as soon as I removed the element I created the placeholder until I was ready to insert the element again it worked beautifully without costing extra memory.
Cheers mate,
Will [import]uid: 61273 topic_id: 10641 reply_id: 38699[/import]