Okay so I’ve made this Brick Breaker game. I have it so I make a red brick and it takes one hit to destroy it. I make the bricks using this code
levels[1] = {{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,1,1,0,0,0},
{0,0,0,1,1,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},}
levels[2] = {{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,1,1,1,1,0,0},
{0,0,1,1,1,1,0,0},
{0,0,1,1,1,1,0,0},
{0,0,1,1,1,1,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},}
levels[3] = {{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,1,1,0,0,0},
{0,1,0,0,0,0,1,0},
{0,1,1,1,1,1,1,0},
{0,1,0,1,1,0,1,0},
{0,0,0,0,0,0,0,0},
{0,0,0,1,1,0,0,0},
{0,0,0,0,0,0,0,0},}
levels[4] = {{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{1,1,1,1,1,1,1,1},
{1,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1},}
And this code
function buildLevel(level)
– Level length, height
local len = table.maxn(level)
bricks:toFront()
for i = 1, len do
for j = 1, W_LEN do
if(level[i][j] == 1) then
local brick = display.newImage(‘brick.png’)
brick.name = ‘brick’
brick.x = BRICK_W * j - OFFSET
brick.y = BRICK_H * i
physics.addBody(brick, {density = 1, friction = 0, bounce = 0})
brick.bodyType = ‘static’
bricks.insert(bricks, brick)
end
end
end
So basically for every 1 in the table that will be where a red brick is.
I want to make a blue brick that will take two hits to destroy. So I want to make it so that when you hit the blue brick it will turn into a red brick.
So I added some code to make the blue brick and when the ball hits the blue brick it turns into the red brick like I want, but the problem is when it turns into the red brick the ball will just go through the red brick.
Here is the code to turn the blue brick into a red brick when you hit it. Can you tell me what I did wrong?
function removeBrick(e)
– Check the which side of the brick the ball hits, left, right
if(e.other.name == ‘brick’ and (ball.x + ball.width * 0.5) < (e.other.x + e.other.width * 0.5)) then
xSpeed = -5
elseif(e.other.name == ‘brick’ and (ball.x + ball.width * 0.5) >= (e.other.x + e.other.width * 0.5)) then
xSpeed = 5
end
– Bounce, Remove
if(e.other.name == ‘brick’) then
ySpeed = ySpeed * -1
e.other:removeSelf()
e.other = nil
bricks.numChildren = bricks.numChildren - 1
– Score
score = score + 1
scoreNum.text = score * SCORE_CONST
scoreNum.x = 70
elseif(e.other.name == ‘Bluebrick’) then
ySpeed = ySpeed * -1
e.other:removeSelf()
local brick = display.newImage(‘brick.png’)
brick.name = ‘brick’
brick.x = e.other.x
brick.y = e.other.y
physics.addBody(brick, {density = 1, friction = 0, bounce = 0})
brick.bodyType = ‘static’
bricks.insert(bricks, brick)
e.other = nil
bricks.numChildren = bricks.numChildren - 1
– Score
score = score + 1
scoreNum.text = score * SCORE_CONST
scoreNum.x = 70
end
– Check if all bricks are destroyed
if(bricks.numChildren < 0) then
alert(’ You Win!’, ’ Next Level ›’)
gameEvent = ‘win’
end
end
Thanks in advance for your help