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.
-- 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
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 )
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
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
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
-- 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
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 )