array

local boxArray ={{0,0,0,0}, {0,0,0,0}, {0,0,0,0}, {0,0,0,0}} local box = {} local isEmpty = function(row, col) if boxArray[row][col] == 0 then return true else return false end end local function onTouch(e) if e.phase == "began" then e.target.xScale = 0.8 e.target.yScale = 0.8 elseif e.phase == "ended" then id = e.target.id id2= e.target.id2 print(id,id2) e.target.xScale = 1 e.target.yScale = 1 e.target.alpha = 1 end end for i =1 ,4 do box[i]={} for j = 1,4 do box[i][j]=display.newRect(0,0,35,35) box[i][j].x = j \*40+ 60 box[i][j].y = i \* 40 + 50 box[i][j].id = i box[i][j].id2 = j if isEmpty(i,j) then box[i][j]:setFillColor(50,50,50) box[i][j].alpha = 0.6 box[i][j]:addEventListener("touch",onTouch) end end end

When i touch the box[1][1] the box[1][1].alpha = 1 then only the box[1][2] or box[2][1] can be touch .After that if box[1][2] is touched box[1][2].alpha become 1 then only box[2][2] or box[1][3] can be touch . How can I do this ?

So, is it that when you touch box[i][j] then only box[i][j+1] and box[i+1][j] can be touched ? It’s not clear what you want to do ; does this mean the last touch, or any previous touch ?

Anyway, the best way of doing it is to filter out the effects of the onTouch handler before you change the alpha etc ; and the boxArray appears to be pointless ?

local boxArray ={{1,0,0,0}, {0,0,0,0}, {0,0,0,0}, {0,0,0,0}} local box = {} local isEmpty = function(row, col) if boxArray[row][col] == 0 then return true else return false end end local function onTouch(e) if e.phase == "began" then e.target.xScale = 0.8 e.target.yScale = 0.8 elseif e.phase == "ended" then id = e.target.id id2 = e.target.id2 for i = 1 , 4 do for j = 1,4 do box[i][j]:removeEventListener("touch",onTouch) end end if box[id+1][id2] == nil then else box[id+1][id2]:addEventListener("touch",onTouch) end if box[id][id2+1] == nil then else box[id][id2+1]:addEventListener("touch",onTouch) end if box[id][id2-1] == nil then else box[id][id2-1]:addEventListener("touch",onTouch) end if box[id-1][id2] == nil then else box[id-1][id2]:addEventListener("touch",onTouch) end e.target.xScale = 1 e.target.yScale = 1 e.target.alpha = 1 e.target:setFillColor(1,0,0) end end for i =1 ,4 do --row box[i]={} for j = 1,4 do -- col box[i][j]=display.newRect(0,0,50,50) box[i][j].x = j \*55+ 30 box[i][j].y = i \* 55 + 50 box[i][j].id = i box[i][j].id2 = j if isEmpty(i,j) then box[i][j]:setFillColor(50,50,50) box[i][j].alpha = 0.6 else box[i][j]:addEventListener("touch",onTouch) box[i][j]:setFillColor(1,0,0) end end end

This is what I want , but the onTouch function have error . Can you check my code ?

You’re trying to add a listener to the four neighbours of the touched square, right? The problem is that the corner square only has two neighbours, so you end up trying to add listeners to two non-existent boxes. You can sort out the problem easily enough with conditionals.  In these situations I like to use a function that finds the neighbours first, and then use those as desired. I’ve written this code that should sort you out:

local function fourNeighbours(row, col) local neighbourList = {} if row \> 1 then neighbourList[#neighbourList + 1] = {row - 1, col} end if row \< #box then neighbourList[#neighbourList + 1] = {row + 1, col} end if col \> 1 then neighbourList[#neighbourList + 1] = {row, col - 1} end if col \< #box then neighbourList[#neighbourList + 1] = {row, col + 1} end return neighbourList end local neighbours = fourNeighbours(id, id2) for i = 1, #neighbours do box[neighbours[i][1]][neighbours[i][2]]:addEventListener("touch",onTouch) end

Note that this code assumes that your grid is going to be a square one, but expanding it for non-square grids should be easy enough.

[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]

leet,

I posted the code just above.

If I correctly understood what you want to do, this code should work. If nothing else, it should get you pointed in the right direction.

Sorry for the formatting of the code, I am not sure why the indentations do not show correctly. 

Can anyone reading this advise what I have wrong that the indentation does not carry over to this post from my Text Wrangler text editor?

I used the suggested code block wrapped in ‘lua’ tags and tried it wrapped in ‘code’ tags, but lost the formatting.

So, is it that when you touch box[i][j] then only box[i][j+1] and box[i+1][j] can be touched ? It’s not clear what you want to do ; does this mean the last touch, or any previous touch ?

Anyway, the best way of doing it is to filter out the effects of the onTouch handler before you change the alpha etc ; and the boxArray appears to be pointless ?

local boxArray ={{1,0,0,0}, {0,0,0,0}, {0,0,0,0}, {0,0,0,0}} local box = {} local isEmpty = function(row, col) if boxArray[row][col] == 0 then return true else return false end end local function onTouch(e) if e.phase == "began" then e.target.xScale = 0.8 e.target.yScale = 0.8 elseif e.phase == "ended" then id = e.target.id id2 = e.target.id2 for i = 1 , 4 do for j = 1,4 do box[i][j]:removeEventListener("touch",onTouch) end end if box[id+1][id2] == nil then else box[id+1][id2]:addEventListener("touch",onTouch) end if box[id][id2+1] == nil then else box[id][id2+1]:addEventListener("touch",onTouch) end if box[id][id2-1] == nil then else box[id][id2-1]:addEventListener("touch",onTouch) end if box[id-1][id2] == nil then else box[id-1][id2]:addEventListener("touch",onTouch) end e.target.xScale = 1 e.target.yScale = 1 e.target.alpha = 1 e.target:setFillColor(1,0,0) end end for i =1 ,4 do --row box[i]={} for j = 1,4 do -- col box[i][j]=display.newRect(0,0,50,50) box[i][j].x = j \*55+ 30 box[i][j].y = i \* 55 + 50 box[i][j].id = i box[i][j].id2 = j if isEmpty(i,j) then box[i][j]:setFillColor(50,50,50) box[i][j].alpha = 0.6 else box[i][j]:addEventListener("touch",onTouch) box[i][j]:setFillColor(1,0,0) end end end

This is what I want , but the onTouch function have error . Can you check my code ?

You’re trying to add a listener to the four neighbours of the touched square, right? The problem is that the corner square only has two neighbours, so you end up trying to add listeners to two non-existent boxes. You can sort out the problem easily enough with conditionals.  In these situations I like to use a function that finds the neighbours first, and then use those as desired. I’ve written this code that should sort you out:

local function fourNeighbours(row, col) local neighbourList = {} if row \> 1 then neighbourList[#neighbourList + 1] = {row - 1, col} end if row \< #box then neighbourList[#neighbourList + 1] = {row + 1, col} end if col \> 1 then neighbourList[#neighbourList + 1] = {row, col - 1} end if col \< #box then neighbourList[#neighbourList + 1] = {row, col + 1} end return neighbourList end local neighbours = fourNeighbours(id, id2) for i = 1, #neighbours do box[neighbours[i][1]][neighbours[i][2]]:addEventListener("touch",onTouch) end

Note that this code assumes that your grid is going to be a square one, but expanding it for non-square grids should be easy enough.

[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]

leet,

I posted the code just above.

If I correctly understood what you want to do, this code should work. If nothing else, it should get you pointed in the right direction.

Sorry for the formatting of the code, I am not sure why the indentations do not show correctly. 

Can anyone reading this advise what I have wrong that the indentation does not carry over to this post from my Text Wrangler text editor?

I used the suggested code block wrapped in ‘lua’ tags and tried it wrapped in ‘code’ tags, but lost the formatting.