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

2. Hello Pong > Animation

Animation

You have the paddles moving based on where each player touches the screen, so now it’s time to put the puck into motion. The game needs an animation loop that will move the puck in a specific direction and at a specific speed. Add the following variables to track direction and speed into the PaddlesViewController interface:

float dx;
float dy;
float speed;

The dx and dy variables represent the direction the puck is travelling and the speed represents how fast the puck is moving. I like to track speed and direction as separate variables so it is easier to speed up the puck while the game progresses. The dx variable is the direction the puck is travelling along the x-axis. If dx is –1 then the puck is moving left, if it is 0 then it is not moving, and if 1 then it is moving to the right. The dy variable represents the direction the puck is travelling along the y-axis. The dy variable will move the puck upwards if –1 and downwards if it is 1. The direction can also be any value between –1 and 1 so the puck can be moving on the screen at any angle.


  

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