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

Chapter 7. Tile-Based Worlds > Adding a Character to the World

Adding a Character to the World

In this section we're going to add a character to the simple world we have just created. Our character is nothing more than a ball. The goal is to be able to move the ball around the grid using the arrow keys. If a cell has a type of greater than 1, it is a wall, and we will not let the ball enter this cell.

When an arrow key is pressed, we look ahead to see where the edge of the ball would be if we were to move it there. If the edge is in an acceptable cell (type = 1), then we move the ball there; if not, then we disregard the key press. More specifically, if the right arrow key is pressed, then we look at the ball's current position, plus the ball's speed, plus the ball's radius to form a number that represents the far right edge of the ball if it were to be moved one quantity (or unit) of speed to the right. We then check to see in which cell that far-right point is. If it is in a cell of type = 1, then we move the ball there.


PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint