First thing is your setting grid[i][j]=0 then your overwriting it with newRect so first you need to correct that. Then let’s say you pick a random starting column and it’s 4 so you will check grid[4][1] to see if its 0 if it is then you move block to that location then check grid[4][2] and repeat until you get a 1 or reach grid[4][11] and where the block stops you change grid[][] =1
.
So , I don’t need this part
grid[i][j] = display.newRect(0,0,36,36) grid[i][j].x = i\*37 + 22 grid[i][j].y = j\*37 + 22 grid[i][j].alpha = 0.1
I still don’t know how to check 0 and 1 , can you give me a example code for the move and set the position be 1 function please ?
At work now will late
FALLING BLOCK EXAMPLE
here’s something i threw together quick hope this helps you
display.setStatusBar(display.HiddenStatusBar) -- setup variables local cols = 10 -- number of columns local rows = 5 -- number of rows local blockSize = 32 -- size of blocks local blocks = {} -- table of blocks on screen local gridTable = {} -- table of open slots local ready = true -- ready for next block local count = 1 -- just using this for example -- setup gridTable and draw grid function setupGrid() for c = 1, cols do gridTable[c] = {} vL = display.newLine((c-1)\*blockSize,0, (c-1)\*blockSize, blockSize\*rows) vL.width = 2 for r = 0, rows do gridTable[c][r] = 0 vH = display.newLine(0,r\*blockSize, cols\*blockSize, blockSize\*r) vH.width = 2 end end vL = display.newLine((cols)\*blockSize,0, (cols)\*blockSize, blockSize\*rows) vL.width = 2 end -- moves blocks down function moveBlockDown( arg ) local c,r = arg.loc[1], arg.loc[2] local newy = arg.y + blockSize arg.loc[1] = c arg.loc[2] = r+1 if gridTable[c+1][r+1] == 0 then transition.to(arg, {time = 250, y = newy, onComplete = function() arg.y = newy moveBlockDown(arg) end}) else -- add a little bounce newy = arg.y - blockSize/10 transition.to( arg, { time = 100, y = newy, onComplete = function() newy = arg.y+blockSize/10 transition.to( arg, { time = 100, y = newy }) end}) -- set gridTable to 1 gridTable[c+1][r] = 1 ready = true end end -- releases new block function newBlock() pick = math.random(0, cols-1) if gridTable[pick+1][1] == 0 then ready = false -- prevents new block until done with prev. block blocks[#blocks+1] = display.newRect( pick\*blockSize, 0-blockSize, blockSize, blockSize ) blocks[#blocks]:setFillColor(math.random(255), math.random(255), math.random(255), 255) blocks[#blocks].loc = { pick, 0 } arg = blocks[#blocks] print("move "..#blocks) moveBlockDown(blocks[#blocks]) elseif gridTable[pick+1][1] == 1 then print("picking new start") newBlock() end end -- call setupGrid setupGrid() -- this is just here to produce new blocks function callNewBlock() if ready == true and count \< rows\*cols/2 then count = count + 1 newBlock() callNewBlock() elseif ready == false and count \< rows\*cols/2 then timer.performWithDelay(250, callNewBlock) else print("done") end end -- start calling new block callNewBlock()
Thanks , can I change the transition.to to enterframe and how to make two block move down together in same column .
Same as one but you have to check under both if horizontal
Why my blocks overlap together ?
local cols = 6 -- number of columns local rows = 10 -- number of rows local blockSize = 35 -- size of blocks local blocks = {} -- table of blocks on screen local gridTable = {} -- table of open slots local ready = true -- ready for next block local count = 1 local grid ={} function setupGrid() for c = 0, cols do gridTable[c] = {} for r = 0, rows do gridTable[c][r] = 0 end end end function moveBlockDown( arg ) local c,r = arg.loc[1], arg.loc[2] local newy = arg.y + blockSize arg.loc[1] = c arg.loc[2] = r+1 if gridTable[c+1][r+1] == 0 then transition.to(arg, {time = 500, y = newy, onComplete = function() arg.y = newy moveBlockDown(arg) end}) else -- add a little bounce newy = arg.y - blockSize/10 transition.to( arg, { time = 100, y = newy, onComplete = function() newy = arg.y+blockSize/10 transition.to( arg, { time = 100, y = newy }) end}) --set gridTable to 1 gridTable[c+1][r] = 1 ready = true end end function newBlock() pick = 3 --math.random(0, 6) if gridTable[1][1] == 0 then ready = false -- prevents new block until done with prev. block block = display.newRect(pick\*blockSize+60 , 0-blockSize+100 , blockSize , blockSize) block:setFillColor(math.random(255), math.random(255), math.random(255), 255) block2 = display.newRect( pick\*blockSize+60 , 0-blockSize+100+blockSize , blockSize , blockSize ) block2:setFillColor(math.random(255), math.random(255), math.random(255), 255) blocks[#blocks+1] = block blocks[#blocks+1] = block2 block.loc = {pick ,0} block2.loc = {pick , 0} arg = blocks[#blocks] print("move "..#blocks) moveBlockDown(block) moveBlockDown(block2) elseif gridTable[1][1] == 1 then print("picking new start") newBlock() end end -- call setupGrid setupGrid() -- this is just here to produce new blocks function callNewBlock() if ready == true and count \< 6 then --rows\*cols then count = count + 1 newBlock() callNewBlock() elseif ready == false and count \< 6 then--rows\*cols then timer.performWithDelay(250, callNewBlock) else print("done") end end -- start calling new block callNewBlock()
At what point do they overlap
The block2 overlap with the next first block .
couple issues
-
you not handling both blocks in the move down function
-
when blocks are created you are checking to see if the first grid position is open instead of checking under both blocks
heres some things that may help you with coding
a good lua crash course
and code exchange
and some other helpful stuff
Thanks for your help . What is the function for arg.loc[1], arg.loc[2] arg.loc[1] = c arg.loc[2] = r+1 and do you know where have LUA array tutorial ?
Thanks for your help . What is the function for arg.loc[1], arg.loc[2] arg.loc[1] = c arg.loc[2] = r+1 and do you know where have LUA array tutorial ?