Delete and Recreate Images

Hello.
I've been searching the internet on how to delete images and I think I've found some good info. What I'm doing with my game is when the game is over a couple buttons appear with options like "next level," "retry," and "menu." My problem is I want to get rid (or kind of hide them with out hogging memory) of the options, play the game, and have them reappear. I have some code of my own that i've been working on but I don't know how to make the options reappear with out doubling the code on each of the buttons.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
function touchHandler(e)
 menuGroup = display.newGroup()
if(e.phase == "began") then
n = n - 5
elseif(e.phase == "ended") then 
 
timer.performWithDelay(2000, spawnApple)
score = 0
scoreText.text = score
score = score + 1
--the delete function
display.remove( btn )
self = nil
end
end
btn:addEventListener("touch", touchHandler)

Why not simply set the isVisible property to false to hide the button, then set it to true when you want it show it again.

1
   btn.isVisible = false

I think its better to remove and recreate the image than hiding it.you can use object:removeSelf() to remove an object.

I'll give hiding them a shot and see what it does to the memory. Renvis: are you saying I should stick with my code of removing the options and use some code to make them reappear?

it depends on the trade-off between the size of the object loaded and how frequently it is shown and hidden...

@renvis hit it. Its about trade offs.

How much texture memory are you using? How big are the buttons? How often are the showing/hiding/loading/unloading?

consider loading/unloading: under the hood, they have to go open a file in the app bundle, load it, close it, decompress it, show it. When you delete it, garbage collection has to come along and remove it.

consider showing/hiding: only has to go through the above process once, but your memory foot print is larger and you have objects that may not be needed often taking up memory. So if memory is tight, load/unload, if its not, show/hide. Determine which beast you need to address: speed or memory.

well explained robmiracle !! :)

Yeah explanation Rob! Now I know what options I have. Two small question though, I decided to go with load/unload but I don't know the code to delete and clean them up so they aren't hanging out there. I know self:removeSelf() but I don't know how to code it so that it will get rid of it, I always get an error Runtime error
...kilkenny/Desktop/Today's Tutorials/acc_ball/main.lua:148: attempt to index global 'self' (a nil value)
stack traceback:
[C]: ?
...kilkenny/Desktop/Today's Tutorials/acc_ball/main.lua:148: in function <...kilkenny/Desktop/Today's Tutorials/acc_ball/main.lua:139>
?: in function <?:215>

The images are about 8kb each, about 126x30, and they show up and the end of every level do you think that the load/unload is a good idea? And I put some code I found into my game just to see how much memory I was using when I used the show/hide option and this was the result...

memUsage = 80.983 KB
memUsage = 81.917 KB
memUsage = 82.851 KB
memUsage = 83.503 KB
memUsage = 83.335 KB
memUsage = 84.093 KB
memUsage = 84.862 KB

Is that a lot to show/hide one image?

This is the code i'm using

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
local noMoreApples = function()
 
local btn = display.newImage("Menu.png")
btn.x = 105
btn.y = 27
 
function touchHandler(e)
 menuGroup = display.newGroup()
if(e.phase == "began") then
n = n - 5
elseif(e.phase == "ended") then
timer.performWithDelay(2000, spawnApple)
score = 0
scoreText.text = score
--This is where I'm not sure what to do
 self:removeSelf(btn)      
        -- print memory usage
collectgarbage( "collect" )
 
        local memUsage_str = string.format( "memUsage = %.3f KB", collectgarbage( "count" ) )
       print( memUsage_str )
end
end
btn:addEventListener("touch", touchHandler)
 
end

I suspect memUsage is returning "Lua" memory. This is your variables, tables and so on. 80Kbytes is next to nothing. There is a second block of memory called "Texture Memory". This is where all your images get allocated from (and probably audio too). There is an api call to get the texture memory. You should probably print that out as well. That will be in megabytes and is the big chunk of memory to have to worry about.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
local noMoreApples = function()
 
local btn = display.newImage("Menu.png")
btn.x = 105
btn.y = 27
 
function touchHandler(e)
    menuGroup = display.newGroup()
    if(e.phase == "began") then
        n = n - 5
    elseif(e.phase == "ended") then
        timer.performWithDelay(2000, spawnApple)
        score = 0
        scoreText.text = score
        --This is where I'm not sure what to do
         self:removeSelf(btn)      
        -- print memory usage
        collectgarbage( "collect" )
        local memUsage_str = string.format( "memUsage = %.3f KB", collectgarbage( "count" ) )
        print( memUsage_str )
    end
end
btn:addEventListener("touch", touchHandler)
 
end

Ok sounds good I got everything in place. I did put in the code to check for usage and these are the figures I came up with...

textureMemory = 100.736 MB
textureMemory = 98.032 MB
textureMemory = 95.268 MB
textureMemory = 99.134 MB
textureMemory = 111.757 MB
textureMemory = 189.430 MB
textureMemory = 180.513 MB

Now I don't know about you but that seems like a lot to me, but if you don't think that's bad then I'll leave it. The only thing is that's only one button, I have two more buttons of the same size and two lines of text that I have to delete and recreate.

Oh and this is the code to print the size

1
2
 local textureMemory_str = string.format( "textureMemory = %.3f MB", collectgarbage( "count" ) )
       print( textureMemory_str )

Given that an iPhone 3, 3G only have 128M of memory, a 3GS and first gen iPad only have 256M and the iPhone 4 and iPad 2 have 512M, yea its a lot.

My entire space shooter game, OmniBlaster with all the audio and most of the art loaded uses between 12-25mb

I just looked at your code to print memory. You're still printing the Lua memory ( garbagecollect("count") ). You need to be using:

system.getInfo("textureMemoryUsed")

Please forgive me for being a complete newbie but are you talking about the code looking like
print( system.getInfo( "textureMemoryUsed" ) )

Basically:

print("Texture Memory: " .. system.getInfo("textureMemoryUsed"))

it's going to return a big number (in bytes), so if you want to scale it in megabytes:

1
print("Texture Memory: " .. string.format("%.3f MB", system.getInfo("textureMemoryUsed") / (1024*1024) ) )

Ah thats better! Much better result
Texture Memory: 1.112 MB

I think that's the way to go then, loading/unloading, unless you think different. Thanks for the help and the explanations! I always like to learn along the way.

views:2025 update:2011/9/27 8:54:05
corona forums © 2003-2011