Im trying to figure out how to use a table to display an image “randomly” in certain locations. What I am trying to do is have an image 160x160 display once per row and randomly in the columns. Is a table the best way to do this? Does anyone know where I can find a sample of what I’m trying to do?
I’m not sure what you mean by “use a table”. Maybe you can expand a bit.
Here is some code that diplays a rect once per row in a random column.
[lua]local rowCount = 10
local columnCount = 6
local imgSize = 40
for row = 0, rowCount do – Loop through the rows
local col = math.random(columnCount) – Choose a column randomly
local x = (col * imgSize) - imgSize – subtracting imgSize to start from 0
local y = row * imgSize
local img = display.newRect(x,y,imgSize,imgSize)
img.anchorX = 0
img.anchorY = 0
print(x,y)
end[/lua]
Ill try that out and see how it works. What I meant by a table was using a table to set up a grid and then having a image placed randomly in one of the spots.
When I input that code with the image in it, it displays all the images in the same spot but different sizes.
You are probably putting the x and y values in the width and height then.
[lua]local rowCount = 10
local columnCount = 6
local imgSize = 40
for row = 0, rowCount do – Loop through the rows
local col = math.random(columnCount) – Choose a column randomly
local x = (col * imgSize) - imgSize – subtracting imgSize to start from 0
local y = row * imgSize
local img = display.newImageRect(“filename.jpg”, imgSize, imgSize)
img.x = x
img.y = y
img.anchorX = 0
img.anchorY = 0
print(x,y)
end[/lua]
what I’m trying to do is make it look like this…
That fixed it. Thanks man!
I’m not sure what you mean by “use a table”. Maybe you can expand a bit.
Here is some code that diplays a rect once per row in a random column.
[lua]local rowCount = 10
local columnCount = 6
local imgSize = 40
for row = 0, rowCount do – Loop through the rows
local col = math.random(columnCount) – Choose a column randomly
local x = (col * imgSize) - imgSize – subtracting imgSize to start from 0
local y = row * imgSize
local img = display.newRect(x,y,imgSize,imgSize)
img.anchorX = 0
img.anchorY = 0
print(x,y)
end[/lua]
Ill try that out and see how it works. What I meant by a table was using a table to set up a grid and then having a image placed randomly in one of the spots.
When I input that code with the image in it, it displays all the images in the same spot but different sizes.
You are probably putting the x and y values in the width and height then.
[lua]local rowCount = 10
local columnCount = 6
local imgSize = 40
for row = 0, rowCount do – Loop through the rows
local col = math.random(columnCount) – Choose a column randomly
local x = (col * imgSize) - imgSize – subtracting imgSize to start from 0
local y = row * imgSize
local img = display.newImageRect(“filename.jpg”, imgSize, imgSize)
img.x = x
img.y = y
img.anchorX = 0
img.anchorY = 0
print(x,y)
end[/lua]
what I’m trying to do is make it look like this…
That fixed it. Thanks man!