How to tag a number of crates in an array with text

Currently, I have an array inside an array that creates 25 crates when the game starts. I want to have a text with a number that has a different value on each crate. For example: each of the crates would say 1, 2, 3, 4, 5, etc, in their center. 

Right now this is my code:

local crates = {} for i = 1, 5 do for j = 1, 5 do crates[i] = display.newImage( "crate.png", 300 + (i\*90), -5000 - (j\*90) ) physics.addBody(crates[i], {density = 0.1, friction = 1, bounce = 0}) end end

Also, how can I print some text into the logs that says if a crate has disappeared past a certain point? Something like: “Crate … has disappeared”.  Thank you.

Also, I want the number to be centered within each individual crate. 

Something like this:

-- After you make the crates, do this local function enterFrame( self ) self.x = self.crate.x self.y = self.crate.y self.rotation = self.crate.rotation end local function finalize( self ) Runtime:removeEventListener( "enterFrame", self.label ) end   for i = 1, #crates do local crate = crates[i] -- https://docs.coronalabs.com/api/library/display/newText.html#syntax-legacy local label = display.newText( crate.parent, i ) label.crate = crate label.enterFrame = enterFrame Runtime:addEventListener( "enterFrame", label )   -- to help you remove the label later when removing the crate crate.label = label crate.finalize = finalize crate:addEventListener( "finalize" ) end

Fixed code and improved to auto-remove enterFrame listener

Ugh… so many typos.  Should be correct now.

So, I placed it and now I get this error:

 ERROR: Runtime error level1.lua:93: bad argument #3 to 'newText' (number expected, got no value) stack traceback: [C]: in function 'newText' level1.lua:93: in function \<level1.lua:32\> ?: in function 'dispatchEvent' ?: in function 'gotoScene' menu.lua:22: in function '\_onRelease' ?: in function '?' ?: in function \<?:1381\> ?: in function \<?:169\> --This is line 93: local label = display.newText( crate.parent, i )
display.newText( crate.parent, i, 0, 0 )

I have it working for only one row, but is does not work with the j values:

for i = 1, 3 do -- works fine for j = 1, 3 do -- does not work crates[i] = display.newImage( "crate.png", 300 + (i\*120), -5000 - (j\*120) ) physics.addBody(crates[i], {density = 0.1, friction = 1, bounce = 0}) end end

It looks like this:

Your loop is wrong.

for i = 1, 3 do -- works fine -- Does the following three times for j = 1, 3 do -- does not work -- Adds three crates at crates[] index 1, 2, and 3 over and over... crates[i] = display.newImage( "crate.png", 300 + (i\*120), -5000 - (j\*120) ) physics.addBody(crates[i], {density = 0.1, friction = 1, bounce = 0}) end end

Do something like this instead:

local count = 1 for i = 1, 3 do for j = 1, 3 do crates[count] = display.newImage( "crate.png", 300 + (i\*120), -5000 - (j\*120) ) physics.addBody(crates[count], {density = 0.1, friction = 1, bounce = 0}) count = count+1 end end

Thanks, it works fine, also I have given the user the ability to type a number and spawn a crate. When it spawns the first time, I want it to have the number of the amount of crates (9) plus 1. Every time it spawns again it was the same value but 1 more. Do you kind of understand? Also, thank you for all the help.

This is my createCrate code:

local function createCrate(x,y) local spawnedCrate = display.newImageRect("crate.png", 90, 90) spawnedCrate.x = x spawnedCrate.y = y physics.addBody(spawnedCrate, "dynamic", {density = 0.1, friction = 1, bounce = 0}) amountOfCrates = amountOfCrates + 1 end
local spawnedCrate = ... crates[#crates+1] = spawnedCrate

#crates == number of entries in table crates

**Updated Post - Removed some incorrectly pasted items.**

You might want to give this a read, it should help clarify any further questions you have re: tables:

https://coronalabs.com/blog/2011/06/21/understanding-lua-tables-in-corona-sdk/

I would also read the PIL:

https://www.lua.org/pil/contents.html

I suggest you read and type in the example code for these chapters and sub-chapters:

  • 1 … 5 - Fundamentals
  • 11.1 - Arrays
  • 18 - math lib
  • 19 - table lib
  • 20 - string. library (would have helped with this question)
  • 21 - io.* library

I made the above suggestions because I’m detecting some confusion about tables and some other things like loops-in-loops.

A little reading about these topics will help and you’ll be able to code you games much faster and more easily.

I tried doing this: 

function createLabels() for x = 1, #crates do crate = crates[x] -- https://docs.coronalabs.com/api/library/display/newText.html#syntax-legacy label = display.newText( crate.parent, x, 0, 0 ) label.crate = crate label.enterFrame = enterFrame Runtime:addEventListener( "enterFrame", label ) -- to help you remove the label later when removing the crate crate.label = label crate.finalize = finalize crate:addEventListener( "finalize" ) end end Runtime:addEventListener("enterFrame", createLabels)

It works just how I want it, but it is very laggy. What could this be? I see that the text is unusually thick.

I fixed this so that the lag is gone, but every time I spawn a new crate, the font gets thicker, as if more and more labels are spawning inside the same crate. How can I check if each crate has a label in the createLabels() function and if they do it does not spawn another label on them? 

function createLabels() for x = 1, #crates do crate = crates[x] -- https://docs.coronalabs.com/api/library/display/newText.html#syntax-legacy label = display.newText( crate.parent, x, 0, 0 ) label.crate = crate label.enterFrame = enterFrame Runtime:addEventListener( "enterFrame", label ) -- to help you remove the label later when removing the crate crate.label = label crate.finalize = finalize crate:addEventListener( "finalize" ) end end

I want to do something like: 

if crate.label ~= nil then --don't create any more labels for this crate end

Never mind, I ended up figuring all of it out, thanks for all the help!

Also, I want the number to be centered within each individual crate. 

Something like this:

-- After you make the crates, do this local function enterFrame( self ) self.x = self.crate.x self.y = self.crate.y self.rotation = self.crate.rotation end local function finalize( self ) Runtime:removeEventListener( "enterFrame", self.label ) end &nbsp; for i = 1, #crates do local crate = crates[i] -- https://docs.coronalabs.com/api/library/display/newText.html#syntax-legacy local label = display.newText( crate.parent, i ) label.crate = crate label.enterFrame = enterFrame Runtime:addEventListener( "enterFrame", label ) &nbsp; -- to help you remove the label later when removing the crate crate.label = label crate.finalize = finalize crate:addEventListener( "finalize" ) end

Fixed code and improved to auto-remove enterFrame listener

Ugh… so many typos.  Should be correct now.

So, I placed it and now I get this error:

 ERROR: Runtime error level1.lua:93: bad argument #3 to 'newText' (number expected, got no value) stack traceback: [C]: in function 'newText' level1.lua:93: in function \<level1.lua:32\> ?: in function 'dispatchEvent' ?: in function 'gotoScene' menu.lua:22: in function '\_onRelease' ?: in function '?' ?: in function \<?:1381\> ?: in function \<?:169\> --This is line 93: local label = display.newText( crate.parent, i )
display.newText( crate.parent, i, 0, 0 )