You are previewing AdvancED Game Design with Flash.
O'Reilly logo
AdvancED Game Design with Flash

Book Description

Creating games in Flash is a never-ending journey of exploration, learning, and most of all, fun. Once you've mastered the basics, a new world is opened up to you, enabling you to take your existing skills to the next level and discover new skills that will in turn open new doors. This book is a direct continuation of Foundation Game Design with Flash, and is a complete point-by-point roundup of the most important skills a Flash game designer needs to know. You'll increase your ActionScript knowledge and your game design skills while creating some excellent example games. You'll learn advanced collision detection skills; professional AI and pathfinding; and how to load and save game data, create destructible environments, and build and switch game levels.

  • Each chapter highlights a new advanced technique illustrated by practical examples.

  • Examples of games are given in a variety of genres, all of which take an object-oriented programming approach.

  • Advanced game design topics are covered, including vector-based collision reaction, pathfinding, billiard ball physics, and modeling game data.

Table of Contents

  1. Copyright
  2. About the Author
  3. About the Technical Reviewer
  4. About the Cover Image Designer
  5. Acknowledgments
  6. Introduction
    1. Enter the labyrinth!
      1. Things you need to know
      2. What about math?
      3. Things you need to have
    2. What kind of games will we make?
    3. How to read this book
    4. The files you'll need
    5. Setting up your work environment
      1. Organizing the project folder
        1. FLAs and metadata for Flash Professional
      2. Using the class library
        1. Setting the path to the class library
        2. Packages and namespaces
        3. Using SWC Files
        4. Testing the class library installation
    6. Optimizing the code
    7. About the code
      1. Code format conventions
      2. Public properties or getters and setters?
    8. Layout conventions
  7. 1. Modeling Game Data
    1. 1.1. Verlet integration
    2. 1.1. The problem with Euler integration
      1. 1.1.2. Understanding Verlet integration
      2. 1.1.3. Using Verlet integration
      3. 1.1.4. Using the main application class
        1. 1.1.4.1. Importing the classes
        2. 1.1.4.2. Positioning the _player object
        3. 1.1.4.3. Figuring out the new velocity, checking collisions, and moving the player
        4. 1.1.4.4. Testing the system
      4. 1.1.5. Onward to Planet X!
        1. 1.1.5.1. Revving the Verlet velocity engine
        2. 1.1.5.2. Moving the spaceship
    3. 1.2. Introducing the Model-View-Controller Pattern
      1. 1.2.1. Understanding MVC
      2. 1.2.2. MVC in action
        1. 1.2.2.1. The view
        2. 1.2.2.2. The controller
        3. 1.2.2.3. The model
      3. 1.2.3. MVC your way
      4. 1.2.4. An MVC player and map view example
        1. 1.2.4.1. The PlayerModel
        2. 1.2.4.2. The PlayerView
        3. 1.2.4.3. The PlayerController
        4. 1.2.4.4. The MapView
        5. 1.2.4.5. The PlayerMVC application class
      5. 1.2.5. Verlet + MVC + physics = fun!
        1. 1.2.5.1. Making stars
        2. 1.2.5.2. Moving stars
    4. 1.3. A crash course in the drawing API
      1. 1.3.1. Drawing lines
      2. 1.3.2. Drawing squares
      3. 1.3.3. Drawing circles
      4. 1.3.4. Adding filters
      5. 1.3.5. Gradient fills
      6. 1.3.6. Complex shapes
        1. 1.3.6.1. Drawing the ship
        2. 1.3.6.2. Drawing the ship's thruster flame
        3. 1.3.6.3. Add both shapes to a container sprite
        4. 1.3.6.4. Using drawPath for really complex shapes
    5. 1.4. Introducing interfaces
    6. 1.5. Summary
  8. 2. Vectors: Ghosts in the Machine
    1. 2.1. What are vectors?
      1. 2.1.1. Vector characteristics
        1. 2.1.1.1. The x and y components
        2. 2.1.1.2. Vector magnitude
      2. 2.1.2. Calculating the angle
      3. 2.1.3. Vector normals
      4. 2.1.4. Normalizing vectors
    2. 2.2. Using and viewing vectors
      1. 2.2.1. Creating the vector model
      2. 2.2.2. Creating the vector view
      3. 2.2.3. Let's see what those vectors look like!
        1. 2.2.3.1. Updating the vector each frame
        2. 2.2.3.2. Rotating text
        3. 2.2.3.3. Rounding numbers to a specific number of decimal places
    3. 2.3. Adding and subtracting vectors
      1. 2.3.1. Scaling vectors
      2. 2.3.2. Gravity in action
      3. 2.3.3. Real gravity
    4. 2.4. Projecting vectors
      1. 2.4.1. Are the vectors pointing in the same direction?
        1. 2.4.1.1. DragHandle objects
        2. 2.4.1.2. Scaling a vector object's view
        3. 2.4.1.3. The VectorMath class
      2. 2.4.2. Projection in action
      3. 2.4.3. Using vector projection for environmental boundaries
    5. 2.5. Intersection
      1. 2.5.1. Finding the intersection point
      2. 2.5.2. Intersection in action
    6. 2.6. Collision and bounce
      1. 2.6.1. Collision on one side of the line
        1. 2.6.1.1. Getting the vector magnitude and orientation
        2. 2.6.1.2. Detecting a collision
      2. 2.6.2. Resolving the collision
      3. 2.6.3. Bounce
      4. 2.6.4. Momentum
      5. 2.6.5. Solid objects
      6. 2.6.6. Collision on both sides of the line
      7. 2.6.7. Bounce, friction, and gravity
    7. 2.7. A crash course in embedding assets
      1. 2.7.1. Embedding fonts
      2. 2.7.2. Embedding images
    8. 2.8. Important vector formulas
      1. 2.8.1.
        1. 2.8.1.1. Create vectors
        2. 2.8.1.2. Vector magnitude
        3. 2.8.1.3. Vectors and angles
        4. 2.8.1.4. Left and right normals
        5. 2.8.1.5. Normalized vectors (unit vectors)
        6. 2.8.1.6. Scaling vectors
        7. 2.8.1.7. Gravity
        8. 2.8.1.8. Dot product
        9. 2.8.1.9. Projection
        10. 2.8.1.10. Define an environmental boundary
        11. 2.8.1.11. Resolve a collision with a vector
        12. 2.8.1.12. Bounce
        13. 2.8.1.13. Friction
    9. 2.9. Summary
  9. 3. Collisions Between Circles
    1. 3.1. Abstract classes
      1. 3.1.1. Understanding abstract and concrete classes
      2. 3.1.2. Creating and implementing abstract classes
        1. 3.1.2.1. Creating an abstract Verlet motion class
        2. 3.1.2.2. Creating an abstract view class
        3. 3.1.2.3. Creating a concrete CircleView class
    2. 3.2. Keyboard and mouse control
    3. 3.3. Collision-handling basics
      1. 3.3.1. Collisions between moving and stationary circles
      2. 3.3.2. Collision between a circle and a line
        1. 3.3.2.1. Finding the point of contact
        2. 3.3.2.2. Finding the edge of the line
      3. 3.3.3. Collision between moving circles
    4. 3.4. Multiple-object collision
    5. 3.5. Fast-moving circles
    6. 3.6. Summary
  10. 4. Collisions Between Polygons
    1. 4.1. The separating axis theorem
      1. 4.1.1. Understanding SAT
      2. 4.1.2. Using SAT
        1. 4.1.2.1. An SAT limitation
        2. 4.1.2.2. Finding the amount of overlap
        3. 4.1.2.3. Finding the collision side
    2. 4.2. Rectangle collisions
      1. 4.2.1. Rectangle collision handling in action
      2. 4.2.2. Bounce and friction
    3. 4.3. Triangle collisions
      1. 4.3.1. SAT with triangles—the wrong way
      2. 4.3.2. SAT with triangles—the right way
      3. 4.3.3. Triangle collision handling in action
        1. 4.3.3.1. Make the shapes
        2. 4.3.3.2. Plot the hypotenuse as a vector
        3. 4.3.3.3. Create a distance vector between the centers of the objects
        4. 4.3.3.4. Project the distance vector onto the hypotenuse's normal
        5. 4.3.3.5. Project the square onto the hypotenuse's normal
        6. 4.3.3.6. Create a gap vector between the square and hypotenuse
        7. 4.3.3.7. Find the dot product
        8. 4.3.3.8. Check if the shapes are overlapping
        9. 4.3.3.9. Move the square out of the collision
      4. 4.3.4. Triangle collision wrap-up
    4. 4.4. Oriented bounding box collisions
    5. 4.5. Polygon and circle collisions
      1. 4.5.1. Circle and square collisions
      2. 4.5.2. Circle and triangle collisions
      3. 4.5.3. Left-facing triangles
    6. 4.6. Case studies
      1. 4.6.1. Case study 1: Polygon environment
        1. 4.6.1.1. Handling slopes
        2. 4.6.1.2. Building game worlds
      2. 4.6.2. Case study 2: Block Game
        1. 4.6.2.1. MVC saves the day again!
        2. 4.6.2.2. The application class
        3. 4.6.2.3. The GameModel
        4. 4.6.2.4. The GameView
        5. 4.6.2.5. The GameController
        6. 4.6.2.6. Plotting the grid of blocks
    7. 4.7. Multilevel games
    8. 4.8. Summary
  11. 5. Pixel-Perfect Collision and Destructible Environments
    1. 5.1. Vector vs. bitmap graphics
    2. 5.2. Using bitmaps
    3. 5.3. Bitmap collisions
      1. 5.3.1. Checking for a collision
      2. 5.3.2. The problems with bitmap collision
      3. 5.3.3. Finding the collision boundary
    4. 5.4. Bitmap collision-detection strategies
      1. 5.4.1. Convex shapes
        1. 5.4.1.1. Creating bitmaps from sprites
        2. 5.4.1.2. Creating the asteroid bitmap
        3. 5.4.1.3. Collision with the asteroid
        4. 5.4.1.4. Inside out
      2. 5.4.2. Surfaces
      3. 5.4.3. Concave shapes
        1. 5.4.3.1. A really huge cave
        2. 5.4.3.2. Scrolling
        3. 5.4.3.3. The mini-map
        4. 5.4.3.4. The map marker
    5. 5.5. Adding objects to scrolling environments
      1. 5.5.1. Using two-dimensional arrays
      2. 5.5.2. Creating and adding the objects
      3. 5.5.3. Scrolling the objects
    6. 5.6. Building a rotating gun turret
      1. 5.6.1. Drawing and rotating the cannon
      2. 5.6.2. Firing bullets
      3. 5.6.3. Moving and removing bullets
    7. 5.7. Destroying things!
      1. 5.7.1. Bitmap collision using points
      2. 5.7.2. Erasing the bitmap
      3. 5.7.3. Jagged rocks
    8. 5.8. Summary
  12. 6. Explosions, Blitting, and Optimization
    1. 6.1. A simple particle explosion
      1. 6.1.1. Using timer events for animation
      2. 6.1.2. Creating the Explosion class
        1. 6.1.2.1. Dynamic properties
        2. 6.1.2.2. Adding and removing explosions
    2. 6.2. A more realistic explosion
      1. 6.2.1. Taking a snapshot
      2. 6.2.2. Slicing and dicing
        1. 6.2.2.1. Tiles, tile sheets, and grids
        2. 6.2.2.2. Making the bitmap explosion
    3. 6.3. Starburst explosions
    4. 6.4. Fast particle explosions
      1. 6.4.1. Introducing bit-block transfer
      2. 6.4.2. Basic blitting
      3. 6.4.3. How fast is fast?
        1. 6.4.3.1. Calculating the frame rate and memory usage
        2. 6.4.3.2. MovieClip vs. copyPixels
      4. 6.4.4. To blit or not to blit?
      5. 6.4.5. Blit explosions
    5. 6.5. Lookup tables
    6. 6.6. An Explosion Controller
    7. 6.7. Smoke trails
      1. 6.7.1. The Perlin noise effect
      2. 6.7.2. Smoke trail optimization
    8. 6.8. Summary
  13. 7. Make It Fun! Sound, Music, and AI
    1. 7.1. Put fun first
    2. 7.2. Sound effects and music
      1. 7.2.1. Adding sound effects
      2. 7.2.2. Playing music
        1. 7.2.2.1. Playing and pausing
        2. 7.2.2.2. Restarting music
        3. 7.2.2.3. Fast-forwarding and rewinding
        4. 7.2.2.4. Changing the volume and speaker panning
        5. 7.2.2.5. Looping music
      3. 7.2.3. Using sound and music in a game
      4. 7.2.4. Finding sound effects and music
    3. 7.3. Buttons
      1. 7.3.1. Creating simple buttons
      2. 7.3.2. Making custom buttons
    4. 7.4. Enemy AI: Line of sight
      1. 7.4.1. Chasing the player
    5. 7.5. Case study: Escape!
      1. 7.5.1. Structure: your best friend and worst enemy
        1. 7.5.1.1. Creating a simple helper class
        2. 7.5.1.2. Structuring Escape!
      2. 7.5.2. Managing game screens
        1. 7.5.2.1. Delaying the game-over screen
      3. 7.5.3. Multiple views of the game data
      4. 7.5.4. New enemy AI techniques
        1. 7.5.4.1. Lying in wait
        2. 7.5.4.2. Traveling along a fixed path
      5. 7.5.5. Managing game states
      6. 7.5.6. Now make your own game!
    6. 7.6. Summary
  14. 8. Tile-Based Game Design
    1. 8.1. Tile-based game advantages
    2. 8.2. Building the game world
      1. 8.2.1. Making tiles
        1. 8.2.1.1. The tile sheet
        2. 8.2.1.2. Tile sheet coordinates
      2. 8.2.2. Making a map
      3. 8.2.3. Describing the map with a two-dimensional array
      4. 8.2.4. Creating the tile model
      5. 8.2.5. Putting the map in the game
      6. 8.2.6. Blitting tiles
      7. 8.2.7. Reviewing the Map application class
    3. 8.3. Adding a game character
      1. 8.3.1. Layering maps
      2. 8.3.2. Making the game character move
        1. 8.3.2.1. Jumping
        2. 8.3.2.2. Moving with the mouse
      3. 8.3.3. Blitting a moving character in a tile-based world
    4. 8.4. Platform collision
      1. 8.4.1. Understanding spatial grid collision
      2. 8.4.2. Finding the corners
      3. 8.4.3. Applying a spatial grid to platform collision
      4. 8.4.4. Working with round tiles
    5. 8.5. Adding more interaction
      1. 8.5.1. Adding soft platforms
      2. 8.5.2. Adding elevators
      3. 8.5.3. Collecting objects
      4. 8.5.4. Wind them up and let them loose!
      5. 8.5.5. Squashing enemies
      6. 8.5.6. Blit animations
        1. 8.5.6.1. Longer blit animations
        2. 8.5.6.2. Movie clips vs. blitting for animation
      7. 8.5.7. Switching levels
    6. 8.6. Blit scrolling
      1. 8.6.1. Adding a camera
      2. 8.6.2. Establishing game world coordinates
    7. 8.7. Using sprites in a tile-based world
      1. 8.7.1. Blitting the tile into a sprite
      2. 8.7.2. Creating the car's control system
      3. 8.7.3. Stuck in the grass
    8. 8.8. Storing extra game data in arrays
      1. 8.8.1. Creating the AI car
      2. 8.8.2. Controlling the AI car
    9. 8.9. Collision maps
      1. 8.9.1. Understanding dynamic spatial grids
      2. 8.9.2. Updating a dynamic grid
      3. 8.9.3. Creating a collision map
    10. 8.10. Other broad-phase collision strategies
    11. 8.11. Summary
  15. 9. Pathfinding
    1. 9.1. Moving through a maze
      1. 9.1.1. Centering game objects
      2. 9.1.2. Moving and changing direction
        1. 9.1.2.1. margin-left:0in
        2. 9.1.2.2. Changing direction
      3. 9.1.3. Random movement in a maze
        1. 9.1.3.1. Finding an intersection
        2. 9.1.3.2. Changing direction
      4. 9.1.4. Chasing
      5. 9.1.5. Tile-based line of sight
    2. 9.2. Finding the shortest path
      1. 9.2.1. Understanding A*
        1. 9.2.1.1. Calculating costs
        2. 9.2.1.2. Finding the second step
        3. 9.2.1.3. Linking the nodes through their parents
      2. 9.2.2. A* in code
        1. 9.2.2.1. margin-left:0in
        2. 9.2.2.2. The complete AStar class
      3. 9.2.3. Using the AStar class
      4. 9.2.4. Understanding heuristics
      5. 9.2.5. Rounding corners
      6. 9.2.6. Walking the path
      7. 9.2.7. Extending and customizing A*
        1. 9.2.7.1. Variable terrain
        2. 9.2.7.2. Influence map
        3. 9.2.7.3. Dijkstra's algorithm
    3. 9.3. Summary
  16. 10. XML and External Data
    1. 10.1. Local shared objects
      1. 10.1.1. Creating and loading shared objects
      2. 10.1.2. Using shared objects
      3. 10.1.3. Limitations of local shared objects
      4. 10.1.4. Loading and saving files to a specific location
        1. 10.1.4.1. Loading the file
        2. 10.1.4.2. Saving the file
    2. 10.2. Understanding XML
      1. 10.2.1. The building blocks of XML
      2. 10.2.2. XML hierarchy
      3. 10.2.3. XML and ActionScript
    3. 10.3. Creating XML objects
      1. 10.3.1. Reading elements, text nodes, and attributes
        1. 10.3.1.1. Reading attributes
        2. 10.3.1.2. Looping through elements and attributes
        3. 10.3.1.3. Finding all the child elements
        4. 10.3.1.4. A better way to find what you're looking for
    4. 10.4. Changing XML data
      1. 10.4.1. Adding new elements and attributes
      2. 10.4.2. Building XML documents from existing variables
      3. 10.4.3. Removing nodes from XML documents
    5. 10.5. Loading game levels from XML data
      1. 10.5.1. Creating a game level map
      2. 10.5.2. Loading and interpreting the XML map data
      3. 10.5.3. Creating multiple game levels with XML
    6. 10.6. Loading XML files at runtime
      1. 10.6.1. Using URLLoader to load files
      2. 10.6.2. Runtime loading security issues
        1. 10.6.2.1. Problems loading files locally
        2. 10.6.2.2. Problems loading files remotely
    7. 10.7. Are we there yet?
      1. 10.7.1. 3D games
      2. 10.7.2. 2D physics
      3. 10.7.3. Online multiplayer games
      4. 10.7.4. Further reading
      5. 10.7.5. Where to next?