Time for action – resetting and changing levels

We'll need to create functions that set up the first and second levels in the game. If a level needs to be replayed, only the current level the user lost in can be accessed. Follow these steps to transition between the levels:

  1. Create a new function called changeLevel1(). This will be placed below the updateBall() function:
    function changeLevel1()
  2. Clear the bricks group when the player loses the round, and then reset them:
      bricks:removeSelf()
    
      bricks.numChildren = 0
      bricks = display.newGroup()
  3. Remove alertDisplayGroup:
      alertBox:removeEventListener("tap", restart)
      alertDisplayGroup:removeSelf()
      alertDisplayGroup = nil
  4. Reset the ball and paddle positions:
      ball.x = (display.contentWidth * 0.5) - (ball.width * 0.5)
      ball.y = (paddle.y - paddle.height) - (ball.height * 0.5) -2
    
      paddle.x = display.contentWidth * 0.5
  5. Redraw the bricks for the current level:
    gameLevel1()
  6. Add an event listener to the background object for startGame(). Close the function:
      background:addEventListener("tap", startGame)
    end
  7. Next, create a new function called changeLevel2(). Apply the same code used for changeLevel1(), but make sure that the bricks are redrawn for gameLevel2():
    function changeLevel2()
    
      bricks:removeSelf()
    
      bricks.numChildren = 0
      bricks = display.newGroup()
    
      alertBox:removeEventListener("tap", restart)
      alertDisplayGroup:removeSelf()
      alertDisplayGroup = nil
    
    
      ball.x = (display.contentWidth * 0.5) - (ball.width * 0.5)
      ball.y = (paddle.y - paddle.height) - (ball.height * 0.5) -2
    
      paddle.x = display.contentWidth * 0.5
      
      gameLevel2() -- Redraw bricks for level 2
    
      background:addEventListener("tap", startGame)
    end

When a level needs to be reset or changed, the display objects have to be wiped from the screen. In this case, we removed the bricks group using bricks:removeSelf().

When any alert screen pops up, whether win or lose, the entire alertDisplayGroup is removed during the reset as well. The ball and paddle objects are set back to their starting position.

The gameLevel1() function is called to redraw the bricks for level 1. The function holds the initial setup for the brick display objects and bricks group.

The background object is used again to call the startGame() function with an event listener. When level 2 needs to be set up, the same procedure like in function changeLevel1()is used, but changeLevel2() and gameLevel2() are called to redraw the bricks.

Right now, the game only has two levels. What can be done to extend this game is to add more levels. They can be created using the same logic used for gameLevel1() and gameLevel2(), by adjusting the numbers used to create rows and columns of bricks. You'll have to create a new function that resets the level. We can use the same method followed for changeLevel1() and changeLevel2() to recreate a level and reset it.