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

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.

  1. Above the gameLevel1() function, create a new function called startGame().
    function startGame()
  2. 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})
  3. 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

  

You are currently reading a PREVIEW of this book.

                                                                                                                    

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

  

Start a Free 10-Day Trial


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