Hi,
I am a novice programmer and for practice I am trying to make a Tetris like game. I have pasted part of my code below. So far I have gotten to a point where blocks appear on the screen and move up (they move up instead of down). I have created some loops to stop the blocks movement if other blocks are in the way, this is also working. There is one part that seems to be giving me quite a bit of trouble…
I have a block the user moves across the screen, I call this shuffle. The shuffle is used to catch blocks and then moves them horizontally. I thought a good way of accomplishing this would be to take the block approaching the shuffle out of the blockGroup (the group that goes through the movement loop) and put it in the shuffleGroup (the movement group the user controls). Each block has a specific color so I created blockTable to save the block’s color information. The problem is, when I remove the block from blockGroup and add it back to shuffleGroup the color changes.
I have used print() to follow what is happening with the blockTable property, and it seems to assign the correct color when the block is created, but when it gets to blockMove function it is all out of whack. I can’t even find a pattern on how it is assigning the color. Please help.
Thank you
Chase
–Creates block movement behaviors
local blockMove = function ()
for i = 1, blockGroup.numChildren do
if ( blockGroup[i] ~= nil ) then
blockXStart = blockGroup[i].x
blockYStart = blockGroup[i].y
occupied = 0
–This changes occupied to 1, if occupied =1 move won’t happen
for j = 1, blockGroup.numChildren do
if ( blockGroup[j] ~= nil and blockXStart == blockGroup[j].x and ( blockYStart -26 ) == blockGroup[j].y ) then
occupied = 1
end
end
–This checks to see if block is moving into space of shuffle, if so remove from blockGroup and add to shuffleGroup
if ( blockXStart == shuffleX - 40 and blockYStart == 642 ) then
local shuffleBlock
blockGroup:remove(blockGroup[i])
shuffleBlock = display.newImage( blockTable[i], shuffleX - 40, 642)
shuffleGroup:insert( shuffleBlock )
elseif ( blockXStart == shuffleX + 40 and blockYStart == 642 ) then
local shuffleBlock
blockGroup:remove(blockGroup[i])
shuffleBlock = display.newImage( blockTable[i], shuffleX + 40, 642)
shuffleGroup:insert( shuffleBlock )
–If block isn’t moving into shuffle space or occupied space move down
elseif ( blockYStart > 176 and occupied == 0 ) then
blockGroup[i].y = blockYStart - 26
end
end
end
end
–Add blocks to screen
local randomBlock = function()
blockNumber = blockNumber + 1
choice = math.random( 100 )
local block
if ( choice > 66 ) then
block = display.newImage( “red.png”, -40 + (80 * math.random( 8 ) ), 1136 )
blockGroup:insert( block )
blockTable[blockNumber] = “red.png”
elseif ( choice <= 66 and choice > 33 ) then
block = display.newImage( “blue.png”, -40 + (80 * math.random( 8 ) ), 1136 )
blockGroup:insert( block )
blockTable[blockNumber] = “blue.png”
else
block = display.newImage( “green.png”, -40 + (80 * math.random( 8 ) ), 1136 )
blockGroup:insert( block )
blockTable[blockNumber] = “green.png”
end
end
–Timer functions to drop blocks and add movement
timer.performWithDelay( 2400, randomBlock, 0)
timer.performWithDelay( 1200, blockMove, 0)