Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

4. Game Controls > Time for action – starting physics for the paddle and ball

Time for action – starting physics for the paddle and ball

Time for action – starting physics for the paddle and ball Right now, our display objects are rather stagnant. In order for gameplay to initiate, we have to activate physics for the paddle and ball in order for any kind of movement in the game to occur. Above the gameLevel1() function, create a new function called startGame(). function startGame() Add in the following lines to instantiate the physics of the paddle and ball: physics.addBody(paddle, "static", {density = 1, friction = 0, bounce = 0}) physics.addBody(ball, "dynamic", {density = 1, friction = 0, bounce = 0}) Create an event listener that uses the background display object to remove the "tap" event for startGame(). Close the function with end. background:removeEventListener("tap", startGame) end In the addGameScreen() function that we created in the previous chapter, we have to add the following line after the call to the gameLevel1() function. This starts the actual game when the background is touched. background:addEventListener("tap", startGame) What just happened? The paddle object has a "static" body type so it is not affected by any collision that occurs against it. The ball object has a "dynamic" body type because we want it to be affected by the collisions on the screen due to directional changes by the wall borders, bricks and paddle. The event listener on the background is removed from the startGame() function, that way it doesn't affect any of the other touch events that are applied in game.

  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint