Noob Math question.

Hey all. I’m trying to create a grid of 21 buttons on screen, each of which corresponds to a different level. At the moment, I just want to print the number of each button to the terminal.

The problem is, I’m struggling with what should be a pretty simple equation. Can someone take a look and offer any advice, (other than re-take maths!)

function new()  
 local localGroup = display.newGroup()  
local itemWidth=110  
local itemHeight=110  
local offset = 150   
   
itemsAcross = 7  
itemsDown = 3  
imgno = 1   
for y=1, itemsDown, 1 do  
   
   
 for x = 1, itemsAcross, 1 do  
  
 local img = display.newImage("lvlB.png")   
   
 img.x = (x-1) \* itemWidth + offset  
 img.y = (y-1) \* itemHeight + offset  
 img.xScale = 2  
 img.yScale = 2  
  
 imgno = y\*x  
  
 local function pressImg(event)  
 print(x+y-1)  
 end  
   
 img:addEventListener ("tap", pressImg)  
 localGroup:insert(img)  
 end  
end  

Thanks

Dan [import]uid: 67933 topic_id: 26106 reply_id: 326106[/import]

Try this:

[code]
function new()
local localGroup = display.newGroup()
local itemWidth=110
local itemHeight=110
local offset = 150

itemsAcross = 7
itemsDown = 3
imgno = 1

local images = {}

for y=1, itemsDown, 1 do
for x = 1, itemsAcross, 1 do

images[y][x] = display.newImage(“lvlB.png”)

images[y][x].x = (x-1) * itemWidth + offset
images[y][x].y = (y-1) * itemHeight + offset
images[y][x].xScale = 2
images[y][x].yScale = 2

local function pressImg(event)
print(x+y-1)
end

images[y][x]:addEventListener (“tap”, pressImg)
localGroup:insert(images[y][x])
end
end

print(#images)
[/code] [import]uid: 84637 topic_id: 26106 reply_id: 105883[/import]