REX VAN DER SPUY AdvancED Game Design Flash with All the essential techniques that every game designer should know. Physics for games, pathfinding, game structure with the MVC design pattern, tile-based games, vector math, and advanced collision detection strategies. AS3.0 Code compatible with Flash CS3 – CS5, Flash Builder, and the open-source Flex SDK. > m o ok.c o b e w o w w. w w < ok o B e w! o W m o d fr a o nl w o D AdvancED Game Design with Flash Rex van der Spuy AdvancED Game Design with Flash AdvancED Game Design with Flash Copyright © 2010 by Rex van der Spuy All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. ISBN-13 (pbk): 978-1-4302-2739-7 ISBN-13 (electronic): 978-1-4302-2740-3 Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logos, or image we use the names, logos, or images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights. Distributed to the book trade worldwide by Springer Science+Business Media LLC., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail [email protected], or visit www.springeronline.com. For information on translations, please e-mail [email protected] or visit www.apress.com. Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook Licensing web page at www.apress.com/info/bulksales. The information in this book is distributed on an “as is” basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work. The source code for this book is freely available to readers at www.friendsofed.com in the Downloads section. Credits President and Publisher: Coordinating Editor: Paul Manning Kelly Moritz Lead Editor: Copy Editor: Ben Renow-Clarke Marilyn Smith Technical Reviewer: Compositor: Joshua Freeney Lynn L’Heureux Editorial Board: Indexer: Clay Andres, Steve Anglin, Mark Beckner, Brenda Miller Ewan Buckingham, Gary Cornell, Jonathan Gennick, Artist: Jonathan Hassell, Michelle Lowman, Matthew Moodie, April Milne Duncan Parkes, Jeffrey Pepper, Frank Pohlmann, Douglas Pundick, Ben Renow-Clarke, Cover Designer: Dominic Shakeshaft, Matt Wade, Tom Welsh Bruce Tang For Mom and Jim, for all your love and support Contents at a Glance About the Author.....................................................................................................xiii About the Technical Reviewer...............................................................................xiv About the Cover Image Designer............................................................................xv Acknowledgments....................................................................................................xi Introduction............................................................................................................xvii Chapter 1: Modeling Game Data...............................................................................1 Chapter 2: Vectors: Ghosts in the Machine ...........................................................67 Chapter 3: Collisions Between Circles.................................................................161 Chapter 4: Collisions Between Polygons.............................................................223 Chapter 5: Pixel-Perfect Collision and Destructible Environments....................305 Chapter 6: Explosions, Blitting, and Optimization...............................................367 Chapter 7: Make It Fun! Sound, Music, and AI.....................................................445 Chapter 8: Tile-Based Game Design.....................................................................503 Chapter 9: Pathfinding...........................................................................................623 Chapter 10: XML and External Data......................................................................691 Index.......................................................................................................................749 iv Contents About the Author.....................................................................................................xiii About the Technical Reviewer...............................................................................xiv About the Cover Image Designer............................................................................xv Acknowledgments....................................................................................................xi Introduction............................................................................................................xvii Chapter 1: Modeling Game Data...............................................................................1 Verlet integration............................................................................................................................2 The problem with Euler integration...............................................................................................3 Understanding Verlet integration...............................................................................................4 Using Verlet integration..............................................................................................................5 Using the main application class.............................................................................................13 Onward to Planet X!.................................................................................................................18 Introducing the Model-View-Controller Pattern..........................................................................22 Understanding MVC.................................................................................................................23 MVC in action...........................................................................................................................25 MVC your way..........................................................................................................................30 An MVC player and map view example..................................................................................32 Verlet + MVC + physics = fun!.................................................................................................44 A crash course in the drawing API..............................................................................................49 Drawing lines............................................................................................................................50 Drawing squares......................................................................................................................50 Drawing circles.........................................................................................................................52 Adding filters.............................................................................................................................53 Gradient fills..............................................................................................................................54 Complex shapes.......................................................................................................................57 Introducing interfaces...................................................................................................................64 Summary.......................................................................................................................................66 v CONTENTS Chapter 2: Vectors: Ghosts in the Machine ...........................................................67 What are vectors?........................................................................................................................68 Vector characteristics...............................................................................................................69 Calculating the angle................................................................................................................73 Vector normals.........................................................................................................................75 Normalizing vectors..................................................................................................................78 Using and viewing vectors...........................................................................................................80 Creating the vector model........................................................................................................80 Creating the vector view..........................................................................................................87 Let’s see what those vectors look like!...................................................................................87 Adding and subtracting vectors...................................................................................................94 Scaling vectors.........................................................................................................................96 Gravity in action......................................................................................................................100 Real gravity.............................................................................................................................105 Projecting vectors.......................................................................................................................105 Are the vectors pointing in the same direction?...................................................................107 Projection in action.................................................................................................................110 Using vector projection for environmental boundaries........................................................113 Intersection.................................................................................................................................115 Finding the intersection point................................................................................................116 Intersection in action..............................................................................................................118 Collision and bounce..................................................................................................................121 Collision on one side of the line............................................................................................121 Resolving the collision...........................................................................................................129 Bounce....................................................................................................................................135 Momentum..............................................................................................................................138 Solid objects...........................................................................................................................139 Collision on both sides of the line..........................................................................................141 Bounce, friction, and gravity..................................................................................................146 A crash course in embedding assets........................................................................................151 Embedding fonts....................................................................................................................151 Embedding images................................................................................................................152 Important vector formulas..........................................................................................................155 Summary.....................................................................................................................................160 vi CONTENTS Chapter 3: Collisions Between Circles.................................................................161 Abstract classes.........................................................................................................................162 Understanding abstract and concrete classes.....................................................................162 Creating and implementing abstract classes.......................................................................166 Keyboard and mouse control.....................................................................................................176 Collision-handling basics...........................................................................................................181 Collisions between moving and stationary circles...............................................................181 Collision between a circle and a line.....................................................................................187 Collision between moving circles..........................................................................................202 Multiple-object collision..............................................................................................................211 Fast-moving circles....................................................................................................................215 Summary.....................................................................................................................................221 Chapter 4: Collisions Between Polygons.............................................................223 The separating axis theorem.....................................................................................................224 Understanding SAT................................................................................................................224 Using SAT...............................................................................................................................225 Rectangle collisions....................................................................................................................237 Rectangle collision handling in action...................................................................................238 Bounce and friction................................................................................................................242 Triangle collisions.......................................................................................................................244 SAT with triangles—the wrong way......................................................................................246 SAT with triangles—the right way.........................................................................................247 Triangle collision handling in action......................................................................................250 Triangle collision wrap-up......................................................................................................261 Oriented bounding box collisions..............................................................................................262 Polygon and circle collisions......................................................................................................267 Circle and square collisions...................................................................................................268 Circle and triangle collisions..................................................................................................276 Left-facing triangles................................................................................................................282 Case studies...............................................................................................................................283 Case study 1: Polygon environment.....................................................................................283 Case study 2: Block Game....................................................................................................286 Multilevel games.........................................................................................................................301 Summary.....................................................................................................................................303 vii CONTENTS Chapter 5: Pixel-Perfect Collision and Destructible Environments....................305 Vector vs. bitmap graphics........................................................................................................306 Using bitmaps.............................................................................................................................308 Bitmap collisions.........................................................................................................................309 Checking for a collision..........................................................................................................310 The problems with bitmap collision.......................................................................................314 Finding the collision boundary...............................................................................................315 Bitmap collision-detection strategies.........................................................................................320 Convex shapes.......................................................................................................................321 Surfaces..................................................................................................................................328 Concave shapes.....................................................................................................................330 Adding objects to scrolling environments.................................................................................344 Using two-dimensional arrays...............................................................................................345 Creating and adding the objects...........................................................................................349 Scrolling the objects...............................................................................................................350 Building a rotating gun turret.....................................................................................................351 Drawing and rotating the cannon..........................................................................................352 Firing bullets...........................................................................................................................355 Moving and removing bullets.................................................................................................356 Destroying things!.......................................................................................................................357 Bitmap collision using points.................................................................................................359 Erasing the bitmap.................................................................................................................360 Jagged rocks..........................................................................................................................362 Summary.....................................................................................................................................365 Chapter 6: Explosions, Blitting, and Optimization...............................................367 A simple particle explosion........................................................................................................368 Using timer events for animation...........................................................................................369 Creating the Explosion class.................................................................................................371 A more realistic explosion..........................................................................................................378 Taking a snapshot..................................................................................................................379 Slicing and dicing...................................................................................................................384 Starburst explosions...................................................................................................................396 Fast particle explosions.............................................................................................................401 Introducing bit-block transfer.................................................................................................401 Basic blitting............................................................................................................................404 viii