easy grid for beginners

hi, 

When i begin i have a lot of difficulties to set a correctly grid. With this snippet, no problem.

Have a good time.

--here enter your value local row = 7 --number of element in x local line = 10 --number of element in y local space = 10 --space between each cell local spaceLeftAndRight =30 --space between the left side and the first cell --the code -------------------------------------------------------------------------------- local grid = {} local imx=(display.contentWidth-((row-1)\*space+(spaceLeftAndRight\*2)))/row local imy=(display.contentWidth-((row-1)\*space+(spaceLeftAndRight\*2)))/row local spacetop=(display.contentHeight-((space\*(line-1))+(imx\*line)))/2 --calculate space bottom and top for i = 1, line do grid[i] = {}; for k = 1, row do grid[i][k] = display.newRect(0,0,imx,imy) grid[i][k].yScale=1 grid[i][k].xScale=1 grid[i][k].alpha = 1 grid[i][k].x = (k - 1) \* imx+ imx\*.5 + (k-1)\*space + spaceLeftAndRight grid[i][k].y = (i - 1) \* imx+ imx\*.5 + (i-1)\*space + spacetop end end

@bexphones real quick edit: don’t forget to include the myGroupObstacle group.

 local row = 7 --number of element in x local line = 10 --number of element in y local space = 10 --space between each cell local myGroupObstacle = display.newGroup() local spaceLeftAndRight =30 --space between the left side and the first cell

i believed that i’ve deleted before posting :wink: edited

i add some things for the beginners:

when you would like to have a different cell from another,just put this :

local grid = {} local imx=(display.contentWidth-((row-1)\*space+(spaceLeftAndRight\*2)))/row local imy=(display.contentWidth-((row-1)\*space+(spaceLeftAndRight\*2)))/row local spacetop=(display.contentHeight-((space\*(line-1))+(imx\*line)))/2 --calculate space bottom and top for i = 1, line do grid[i] = {}; for k = 1, row do grid[i][k] = display.newRect(0,0,imx,imy) grid[i][k].yScale=1 grid[i][k].xScale=1 grid[i][k].alpha = 1 grid[i][k].x = (k - 1) \* imx+ imx\*.5 + (k-1)\*space + spaceLeftAndRight grid[i][k].y = (i - 1) \* imx+ imx\*.5 + (i-1)\*space + spacetop end end grid[2][5]=display.newCircle(100,100,100,100)--to acces a value from grid it's first the y position [2] and next the x position [5]

you can also add some feature to each cell

grid[i][k].shadow=display.....etc

 bye 

hi, 

when i try what you say to have a different value it don’t work. I would have a circle at position 2,1 but that doesn’t work ???

local row = 6 --number of element in xlocal line = 10 --number of element in y local space = 5 --space between each cell local line=10 local spaceLeftAndRight =30 --space between the left side and the first cell  local imx=(display.contentWidth-((row-1)\*space+(spaceLeftAndRight\*2)))/row     local imy=(display.contentWidth-((row-1)\*space+(spaceLeftAndRight\*2)))/row  local spacetop=(display.contentHeight-((space\*(line-1))+(imx\*line)))/2  --calculate space bottom and top for i = 1, line do             grid[i] = {};             for k = 1, row do                 grid[i][k] = display.newRect(0,0,imx,imy)                 grid[i][k].yScale=1                 grid[i][k].xScale=1                 grid[i][k].alpha = 0.1                 grid[i][k].x = (k - 1) \* imx+ imx\*.5 + (k-1)\*space + spaceLeftAndRight                 grid[i][k].y = (i - 1) \* imx+ imx\*.5 + (i-1)\*space + spacetop               end end grid[1][2]=display.newCircle(100,100,100) --i have always a rectangle a this position !!!

hi,

and why i try with a text with optiondItem2 go to 0,0 …why ? 

 local optionsItem1={text="100",font=fontLuckiest,fontSize=10,align="center"} local optionsItem2={text="500",font=fontLuckiest,fontSize=10,align="center"} local gridText = {} for i = 1, line2 do gridText[i] = {}; for k = 1, row2 do gridText[i][k]=display.newText(optionsItem1) gridText[i][k].yScale=1 gridText[i][k].xScale=1 gridText[i][k].alpha = 1 gridText[i][k].x = (k - 1) \* imx2+ imx2\*.5 + (k-1)\*space2 + spaceLeftAndRight2 gridText[i][k].y = (i - 1) \* imx2+ imx2\*.5 + (i-1)\*space2 + spacetop2 +30 end end gridText[1][2]=display.newText(optionsItem2)

@bexphones real quick edit: don’t forget to include the myGroupObstacle group.

 local row = 7 --number of element in x local line = 10 --number of element in y local space = 10 --space between each cell local myGroupObstacle = display.newGroup() local spaceLeftAndRight =30 --space between the left side and the first cell

i believed that i’ve deleted before posting :wink: edited

i add some things for the beginners:

when you would like to have a different cell from another,just put this :

local grid = {} local imx=(display.contentWidth-((row-1)\*space+(spaceLeftAndRight\*2)))/row local imy=(display.contentWidth-((row-1)\*space+(spaceLeftAndRight\*2)))/row local spacetop=(display.contentHeight-((space\*(line-1))+(imx\*line)))/2 --calculate space bottom and top for i = 1, line do grid[i] = {}; for k = 1, row do grid[i][k] = display.newRect(0,0,imx,imy) grid[i][k].yScale=1 grid[i][k].xScale=1 grid[i][k].alpha = 1 grid[i][k].x = (k - 1) \* imx+ imx\*.5 + (k-1)\*space + spaceLeftAndRight grid[i][k].y = (i - 1) \* imx+ imx\*.5 + (i-1)\*space + spacetop end end grid[2][5]=display.newCircle(100,100,100,100)--to acces a value from grid it's first the y position [2] and next the x position [5]

you can also add some feature to each cell

grid[i][k].shadow=display.....etc

 bye 

hi, 

when i try what you say to have a different value it don’t work. I would have a circle at position 2,1 but that doesn’t work ???

local row = 6 --number of element in xlocal line = 10 --number of element in y local space = 5 --space between each cell local line=10 local spaceLeftAndRight =30 --space between the left side and the first cell  local imx=(display.contentWidth-((row-1)\*space+(spaceLeftAndRight\*2)))/row     local imy=(display.contentWidth-((row-1)\*space+(spaceLeftAndRight\*2)))/row  local spacetop=(display.contentHeight-((space\*(line-1))+(imx\*line)))/2  --calculate space bottom and top for i = 1, line do             grid[i] = {};             for k = 1, row do                 grid[i][k] = display.newRect(0,0,imx,imy)                 grid[i][k].yScale=1                 grid[i][k].xScale=1                 grid[i][k].alpha = 0.1                 grid[i][k].x = (k - 1) \* imx+ imx\*.5 + (k-1)\*space + spaceLeftAndRight                 grid[i][k].y = (i - 1) \* imx+ imx\*.5 + (i-1)\*space + spacetop               end end grid[1][2]=display.newCircle(100,100,100) --i have always a rectangle a this position !!!

hi,

and why i try with a text with optiondItem2 go to 0,0 …why ? 

 local optionsItem1={text="100",font=fontLuckiest,fontSize=10,align="center"} local optionsItem2={text="500",font=fontLuckiest,fontSize=10,align="center"} local gridText = {} for i = 1, line2 do gridText[i] = {}; for k = 1, row2 do gridText[i][k]=display.newText(optionsItem1) gridText[i][k].yScale=1 gridText[i][k].xScale=1 gridText[i][k].alpha = 1 gridText[i][k].x = (k - 1) \* imx2+ imx2\*.5 + (k-1)\*space2 + spaceLeftAndRight2 gridText[i][k].y = (i - 1) \* imx2+ imx2\*.5 + (i-1)\*space2 + spacetop2 +30 end end gridText[1][2]=display.newText(optionsItem2)