How to drag object like tetris and check grid is 0 or 1 ? If the grid is 0 then the object can drag left or right else cannot drag and set the grid be 1 when the object stop falling down .
display.setStatusBar(display.HiddenStatusBar) -- setup variables local cols = 10 -- number of columns local rows = 10 -- 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 function dragBlock(event) local t = event.target local phase = event.phase if "began" == phase then -- Make target the top-most object local parent = t.parent parent:insert( t ) display.getCurrentStage():setFocus( t ) t.isFocus = true elseif t.isFocus then if "moved" == phase then local gridx = math.floor(event.x/32) local gridxpos = gridx\*32+16 if gridx \> -1 and gridx \< 10 then t.x = gridxpos end elseif "ended" == phase then display.getCurrentStage():setFocus( nil ) event.target.isFocus = false end end return true 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 = 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 -- 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] arg:addEventListener("touch",dragBlock) 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()