[lua]
local TOT_COLS = 4
local TOT_ROWS = 4
local TOT_BLOCKS = TOT_COLS * TOT_ROWS
local blocks = {}
– START GROUP OF BLOCKS; TOP LEFT CORNER AT 150,150
local xCursor = 150
local yCursor = 150
local xCnt = 1
local rowNum = 1
local colNum = 1
– FIND THE BLOCKS, ABOVE-BELOW-LEFT_RIGHT OF SELECTED BLOCK …
– MARK THOSE BLOCKS … ‘ALLOWED TO BE TOUCHED’, IF THEY ARE NOT LIT UP
local function setABLR(_id)
local above
local below
local left
local right
above = _id - TOT_COLS
below = _id + TOT_COLS
left = _id - 1
right = _id + 1
– BE SURE BLOCKS ARE TRULY NEXT TO THE SELECTED BLOCK
if above < 1 then
above = 0
end
if below > TOT_BLOCKS then
below = 0
end
if math.mod(left, TOT_COLS) == 0 and left > 1 then
left = 0
end
– MARK ANY BLOCK (NOT ALREADY LIT UP) AS ALLOWED TO BE TOUCHED
if above > 0 and blocks[above].isOn == false then
blocks[above].isAllowed = true
end
if below > 0 and blocks[below].isOn == false then
blocks[below].isAllowed = true
end
if left > 0 and blocks[left].isOn == false then
blocks[left].isAllowed = true
end
if right > 0 and right <= TOT_BLOCKS and blocks[right].isOn == false then
blocks[right].isAllowed = true
end
end
local function onTouch(e)
local function scaleUp()
transition.to(blocks[e.target.id], {time = 100, xScale = 1, yScale = 1})
end
if e.phase == “began” then
transition.to(blocks[e.target.id], {time = 100, xScale = .8, yScale = .8, onComplete=scaleUp})
elseif e.phase == “ended” then
if e.target.isOn == false and e.target.isAllowed == true then
– THIS BLOCK WAS A VALID TOUCH … MARK IT
e.target.alpha = 1
e.target.isOn = true
– assuming once touched and lit up, no longer allowed to be touched
e.target.isAllowed = false
– loop thru all the blocks and see if the block is lit, if it is
– check it’s block above,below,left, and right and if those are not
– lit, then mark them allowed to be touched
setABLR(e.target.id)
for i = 1, TOT_BLOCKS do
if blocks[i].isOn then
setABLR(i)
end
end
else
– BLOCK IS ALREADY ON
– MAKE A BUZZ SOUND OR FEEDBACK TO USER TO LET THEM KNOW TOUCH WAS NOT ALLOWED
print(" block " … e.target.id … " not allowed to be touched ")
end
end
end
– CREATE THE BLOCKS
for i = 1, TOT_BLOCKS do
table.insert(blocks, display.newRect(0,0,50,50))
– PLACE BLOCK IN A COLUMN
blocks[i].x = xCursor + (xCnt * 55)
– PLACE BLOCK IN A ROW
blocks[i].y = yCursor
xCnt = xCnt + 1
if math.mod(i,TOT_COLS) == 0 then
xCnt = 1
yCursor = yCursor + 55
colNum = 1
rowNum = rowNum + 1
end
– ASIGN BLOCK SEQUENTIAL ID
blocks[i].id = i
– ALL BLOCKS WHEN CREATED ARE SET TO ‘NOT ON’ - ‘NOT LIT UP’.
blocks[i].isOn = false
blocks[i].isAllowed = false
– DIM EACH BLOCK
blocks[i].alpha = .5
blocks[i]:addEventListener(“touch”, onTouch)
colNum = colNum + 1
end
– set first first block to on
blocks[1].isOn = true
blocks[1].alpha = 1
– set block below it and block to right to ‘allowed to be touched’
blocks[2].isAllowed = true
– just add tot_cols to get to the block directly below any block in a row
blocks[1 + TOT_COLS].isAllowed = true
[/lua]