Help with Game and Memory Managment

So I cannot figure this out for the life of me…  The program is simply a starter and its a test with manageing multiple instances of one object.  So I want to remove the objects(Boxes in this case) that are being displayed individually but I dont know how to stick these objects into tables and then manipulate them to destroy them without removing the variable and causing an error.

Any Ideas?

Or any really easy solutions to handling this problem?

xAxis = 100  --Variable initiaions
yAxis = 100
local boxStorage = {}

function onObjectTouch( event )
    if event.phase == “began” then
        print( “Hello”)
        table.remove(boxStorage)
        box:removeSelf()
        box = nil
    end
    return true
end

for i = 1, 20, 1 do
  print(i)
  if xAxis <= 668 then
    box = display.newRect(xAxis , yAxis , 50 , 50)
    box:addEventListener( “touch”, onObjectTouch )–Allows the object to be removed…
    table.insert(boxStorage , box)
    xAxis = xAxis + 100
  end
  if xAxis >= 669 then
    xAxis = 100
    yAxis = yAxis + 200
    box = display.newRect(xAxis , yAxis , 50 , 50)
    box:addEventListener( “touch”, onObjectTouch )
    table.insert(boxStorage , box)
  end

    
end

Hi.  The first two parts of my answer may seem mean or grumpy, but bear with me:

  1. Please format your code when posting to the forums.  Put it in a code box and give it a little love to make it legible.  Yours is pretty good, but could benefit from being in a code format block.

formatyourcode.jpg

  1. Do you already know how to code in Lua?  If not, please do some reading here.  I ask, because you’re making globals all over the place which is a sign that you’re new and missing some critical knowledge.  You need this foundation or your whole effort will be many time more difficult.  i.e. You will struggle with errors caused by fundamental coding issues and not Corona.

  2.  Here is your code, slightly re-written:

    –Variable initialization local xAxis = 100 local yAxis = 100 local boxStorage = {} function onObjectTouch( self, event ) if( event.phase == “began” ) then print( “Hello”) boxStorage[self] = nil – remove from list – self:removeSelf() – NOT SAFE; Crashes if called twice by accident display.remove( self ) – SAFER end return true end for i = 1, 20 do print(i) local box – empty box variable – Make the box if xAxis <= 668 then box = display.newRect( xAxis , yAxis , 50 , 50 ) xAxis = xAxis + 100 elseif xAxis >= 669 then xAxis = 100 yAxis = yAxis + 200 box = display.newRect(xAxis , yAxis , 50 , 50) end – Apply all common steps in one place. box.touch = onObjectTouch box:addEventListener( “touch” ) boxStorage[box] = box – Notice, we’re using the box (ID) as the index too. end

Differences:

  • Got rid of all globals.  
  • Used table listener instead of function listener for touch.
  • Stored (and removed) objects in table using the object IDs as indexes.
    • Numerically indexed tables are fast to iterate over, but suck for adding and removing objects in random order.  
    • Object ID indexes are the way to go for most random creation and removal scenarios.

PS - I didn’t run this code, so I may have typed something incorrectly, but the logic is right.

Much Appreciated.  Yeah sorry about the code.  First post on the forums and didn’t realize they had a code forum…

As for the globals…

It was all under a single main file and not going to be transitioned at all.  Just a simple test and it was quicker to type the variable itself rather than add a local.  I typically am pretty good about scope in my projects but this is just a test to be transitioned later.

Regardless Thanks for all your help!

Hi.  The first two parts of my answer may seem mean or grumpy, but bear with me:

  1. Please format your code when posting to the forums.  Put it in a code box and give it a little love to make it legible.  Yours is pretty good, but could benefit from being in a code format block.

formatyourcode.jpg

  1. Do you already know how to code in Lua?  If not, please do some reading here.  I ask, because you’re making globals all over the place which is a sign that you’re new and missing some critical knowledge.  You need this foundation or your whole effort will be many time more difficult.  i.e. You will struggle with errors caused by fundamental coding issues and not Corona.

  2.  Here is your code, slightly re-written:

    –Variable initialization local xAxis = 100 local yAxis = 100 local boxStorage = {} function onObjectTouch( self, event ) if( event.phase == “began” ) then print( “Hello”) boxStorage[self] = nil – remove from list – self:removeSelf() – NOT SAFE; Crashes if called twice by accident display.remove( self ) – SAFER end return true end for i = 1, 20 do print(i) local box – empty box variable – Make the box if xAxis <= 668 then box = display.newRect( xAxis , yAxis , 50 , 50 ) xAxis = xAxis + 100 elseif xAxis >= 669 then xAxis = 100 yAxis = yAxis + 200 box = display.newRect(xAxis , yAxis , 50 , 50) end – Apply all common steps in one place. box.touch = onObjectTouch box:addEventListener( “touch” ) boxStorage[box] = box – Notice, we’re using the box (ID) as the index too. end

Differences:

  • Got rid of all globals.  
  • Used table listener instead of function listener for touch.
  • Stored (and removed) objects in table using the object IDs as indexes.
    • Numerically indexed tables are fast to iterate over, but suck for adding and removing objects in random order.  
    • Object ID indexes are the way to go for most random creation and removal scenarios.

PS - I didn’t run this code, so I may have typed something incorrectly, but the logic is right.

Much Appreciated.  Yeah sorry about the code.  First post on the forums and didn’t realize they had a code forum…

As for the globals…

It was all under a single main file and not going to be transitioned at all.  Just a simple test and it was quicker to type the variable itself rather than add a local.  I typically am pretty good about scope in my projects but this is just a test to be transitioned later.

Regardless Thanks for all your help!