Let's summarize what has been made to make sure that you have everything added into your game. You can also refer to the Breakout Final
folder in the Chapter 4
folder to see the final code. You made sure that the necessary variables were introduced in the game. You also initialized the main()
function that starts the game play. A main menu screen was implemented with the game title and a play button.
Next, you transitioned the menuScreenGroup
away from the screen to load the main playing field. The main display objects of the game, such as the paddle, ball, and bricks, were added. The score and level number were displayed as the UI elements and updated throughout game play. Paddle movement in both the simulator and accelerometer were added as well as the collision detection with the paddle and the ball.
The physical properties of the paddle and ball were added at the start of the game. The brick layouts for each of the two levels were created. You have also added event listeners to all our game objects from the point when they need to be activated during the game and removed when game play is over.
Every time the ball collides with a brick, the brick is removed from the scene. The directional changes of the ball are updated for every wall, paddle, or brick collision made. Every time a win or lose condition occurred, all game objects are reset to begin the start of the current or new level.
When a condition occurs, an alert screen pops up, notifying the player of what has happened. The display objects that initiate the alerts are created in a function. Finally, the win and lose arguments are created to determine whether the current level has to be replayed, whether the player goes to the next level, or whether the game has been completed.
Beware of case-sensitive variables and functions in case you run into errors. Also, be sure to check whether you're missing any punctuation required in your code. These can be easily overlooked. Refer to your terminal window in the simulator for any error references.
Q1. How do you properly remove a display object from the stage?
remove()
object: remove()
object:removeSelf()
object = nil
Q2. What is the correct way to make the following display object into a physics object?
local ball = display.newImage("ball.png")
physics.addBody( ball, { density=2.0, friction=0.5, bounce=0.2,radius = 25 })
physics.addBody( ball, "dynamic", { density=2.0, friction=0.5, bounce=0.2,radius = 15 } )
and 2.
Q3. What best represents what "began"
means in the following function?
local function onCollision( event ) if event.phase == "began" and event.object1.myName == "Box 1" then print( "Collision made." ) end end