Help with Brick Breaker game

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 :slight_smile:

Have you considered learning from this instead of going it alone? --> 

https://marketplace.coronalabs.com/asset/breakout-game-templates

https://www.youtube.com/watch?v=DBahwJ2HMCU

It’s not the template I need help with. I need help turning the blue brick into the red brick.

Simply remove the blue brick and instantiate a red brick in the same location.  You could use a texture fill to switch graphics easily.

Read this https://coronalabs.com/blog/2013/11/26/tutorial-techniques-for-swapping-images/

I tried this it Isn’t working :frowning:

Wow okay so I checked the corona simulator console and it said

 ERROR: main.lua:371: physics.addBody() cannot be called when the world is locked and in the middle of number crunching, such as during a collision event

Which is why i told you to use a texture fill to “paint” the image.

Or you could do the change in a timer.performWithDelay if you add a 1ms delay (to allow the collision event to end).

Oh okay I get it now thanks for the help :slight_smile:

The usual advice to avoid: “physics.addBody() cannot be called when the world is locked and in the middle of number crunching, such as during a collision event” is to use a timer.performWithDelay() with a short amount of time, say 17ms and handle any add/remove statements in that timer call. That way you give the collision function time to finish.

Rob

Have you considered learning from this instead of going it alone? --> 

https://marketplace.coronalabs.com/asset/breakout-game-templates

https://www.youtube.com/watch?v=DBahwJ2HMCU

It’s not the template I need help with. I need help turning the blue brick into the red brick.

Simply remove the blue brick and instantiate a red brick in the same location.  You could use a texture fill to switch graphics easily.

Read this https://coronalabs.com/blog/2013/11/26/tutorial-techniques-for-swapping-images/

I tried this it Isn’t working :frowning:

Wow okay so I checked the corona simulator console and it said

 ERROR: main.lua:371: physics.addBody() cannot be called when the world is locked and in the middle of number crunching, such as during a collision event

Which is why i told you to use a texture fill to “paint” the image.

Or you could do the change in a timer.performWithDelay if you add a 1ms delay (to allow the collision event to end).

Oh okay I get it now thanks for the help :slight_smile:

The usual advice to avoid: “physics.addBody() cannot be called when the world is locked and in the middle of number crunching, such as during a collision event” is to use a timer.performWithDelay() with a short amount of time, say 17ms and handle any add/remove statements in that timer call. That way you give the collision function time to finish.

Rob