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

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!