How to remove whole display at once?

I’m developing a game which uses a matrix of square elements.

It’s something like this:

    1   2   3   4   5 …

1  X   -    -    A   -

2  -   X    X   O   X

3  X  X    A   A   O

4  O  -    -    -    X

5  A  A    A   A   A

I represent the map as a matrix (actualy 20x20)

Depending on the value, the update screen function show the correct image:

local function update_screen()

   for j=1,20 do

        for k=1,20 do

             if map[j][k]==1 then

                display.newImage(“image_X.png”, (j-1)*size, (k-1)*size)

             elseif map[j][k]==2 then

                display.newImage(“image_O.png”, (j-1)*size, (k-1)*size)

             elseif map[j][k]==2 then

                display.newImage(“image_A.png”, (j-1)*size, (k-1)*size)

             elseif map[j][k]==4 then

                display.newImage(“image_-.png”, (j-1)*size, (k-1)*size)

             end

        end

end

Of course this function creates 20x20=400 image objects even I don’t use variables pointing to them.

As the game advance, the map fields change and I call again and again the update_screen().

So each time, there are more 400 image objects being created, which stay in memory.

I observe that at start point the game is fast and then slow down its speed. (maybe because the raise amount of memory used).

So, I want to remove the whole display in the beginning of the update_screen() function.

How can I do that?

I tried display.remove() and display.remove(display) but don’t work.

Maybe you will tell me to use variables pointing to all 400 objects and then remove them all one by one but I think this is nonsense in this case.

So what can I do to perform what I want?

Help me.

Thanks!

Something like this? I’ve streamlined your function a little and I’d also recommend looking into using display.newImageRect instead so your game will look right across all devices, and if you are using storyboard make sure you add the images group in to the storyboard view so it is cleaned up when you move to a new scene.

[lua]

local images = display.newGroup()

local files = {“image_X.png”,“image_O.png”, “image_A.png”, “image_-.png”}

local function update_screen()

    if images ~= nil then

        display.remove(images)

        images = display.newGroup()

    end

   for j=1,20 do

        for k=1,20 do

            local id = map[j][k]

            local i = display.newImage(files[id], (j-1)*size, (k-1)*size)

            images:insert(i)

        end

   end

end

[/lua]

It works! Thanks!

Something like this? I’ve streamlined your function a little and I’d also recommend looking into using display.newImageRect instead so your game will look right across all devices, and if you are using storyboard make sure you add the images group in to the storyboard view so it is cleaned up when you move to a new scene.

[lua]

local images = display.newGroup()

local files = {“image_X.png”,“image_O.png”, “image_A.png”, “image_-.png”}

local function update_screen()

    if images ~= nil then

        display.remove(images)

        images = display.newGroup()

    end

   for j=1,20 do

        for k=1,20 do

            local id = map[j][k]

            local i = display.newImage(files[id], (j-1)*size, (k-1)*size)

            images:insert(i)

        end

   end

end

[/lua]

It works! Thanks!