AdvancED Game Design with Flash potx - Pdf 11

REX VAN DER SPUY
AdvancED
Game Design
with Flash
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.
Download from Wow! eBook <www.wowebook.com>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

Jonathan Hassell, Michelle Lowman, Matthew Moodie,
Duncan Parkes, Jeffrey Pepper, Frank Pohlmann,
Douglas Pundick, Ben Renow-Clarke,
Dominic Shakeshaft, Matt Wade, Tom Welsh

Coordinating Editor:
Kelly Moritz
Copy Editor:
Marilyn Smith
Compositor:
Lynn L’Heureux
Indexer:
Brenda Miller
Artist:
April Milne
Cover Designer:
Bruce Tang

For Mom and Jim, for all your love and support
iv
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

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
CONTENTS
vi
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

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
CONTENTS
viii
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

ix
How fast is fast? 416
To blit or not to blit? 421
Blit explosions 424
Lookup tables 429
An Explosion Controller 434
Smoke trails 438
The Perlin noise effect 438
Smoke trail optimization 442
Summary 444
Chapter 7: Make It Fun! Sound, Music, and AI 445
Put fun first 446
Sound effects and music 447
Adding sound effects 448
Playing music 451
Using sound and music in a game 460
Finding sound effects and music 462
Buttons 463
Creating simple buttons 463
Making custom buttons 464
Enemy AI: Line of sight 467
Chasing the player 476
Case study: Escape! 477
Structure: your best friend and worst enemy 479
Managing game screens 484
Multiple views of the game data 489
New enemy AI techniques 491
Managing game states 498
Now make your own game! 500
Summary 501

Adding a camera 587
Establishing game world coordinates 588
Using sprites in a tile-based world 590
Blitting the tile into a sprite 592
Creating the car’s control system 595
Stuck in the grass 598
Storing extra game data in arrays . 599
Creating the AI car 603
Controlling the AI car 604
Collision maps . 605
Understanding dynamic spatial grids. 606
Updating a dynamic grid . 607
Creating a collision map 611
Other broad-phase collision strategies 620
Summary 621
Download from Wow! eBook <www.wowebook.com>
CONTENTS
xi
Chapter 9: Pathfinding 623
Moving through a maze 624
Centering game objects 625
Moving and changing direction 626
Random movement in a maze 633
Chasing 642
Tile-based line of sight 647
Finding the shortest path 655
Understanding A* 656
A* in code 667
Using the AStar class 676
Understanding heuristics 680

3D games 745
2D physics 745
Online multiplayer games 746
Further reading 747
Where to next? 747
Index 749

xiii
About the Author
Rex van der Spuy, author of Foundation Game Design with Flash, is
a freelance interactive media designer specializing in Flash game
design, interface design, and ActionScript programming.
Rex programmed his first adventure game at 10 years’ old on his
Commodore VIC-20. He went on to study film production, graduating
with a BFA in Film/Video from York University (Toronto) in 1993, and
spent a number of years working as an independent producer and
freelance cameraman. He has designed Flash games and done
interactive interface programming for clients such as Agency Interactive
(Dallas), Scottish Power (Edinburgh), DC Interact (London), Draught
Associates (London), and the Bank of Montreal (Canada). He also
builds game engines and interactive museum installations for
PixelProject (Cape Town). In addition, he taught advanced courses in
Flash game design for the Canadian School of India (Bangalore).
In his spare time, Rex has done a considerable amount of technical and fiction writing, and maintains a
semiprofessional musical career as a performer on the sitar. Rex currently divides his time equally
between Canada, India, and South Africa, and works on consulting and software development projects for
clients in India, North America, and the UK. He also maintains the game design learning and experimental
lab, www.kittykatattack.com.
xiv
About the Technical Reviewer

iterating through some equations to calculate the coordinates of
millions of points. The points are then plotted with an assigned
color.
x
n + 1
= x
n
cos(a) – (y
n
– x
n
p
) sin(a)
y
n+1
= x
n
sin(a) + (y
n
– x
n
p
) cos(a)

xvi
Acknowledgments
A great debt of gratitude to the phenomenally hard-working, dedicated, and talented team at friends of ED
who made this book possible. You’re the best bunch of people to work with, ever. Josh, Fran, Kelly,
Marilyn, and Ben, we did it!
A particular note of gratitude to the lead editor, Ben Renow-Clarke. It was his vision for Foundation Game

(friends of ED, 2009). If you’ve read that book, and have had a bit of practical experience making some of
your own games, you have all the skills and knowledge you need to enjoy the fun we’re going to have in
this book.
If you haven’t read Foundation Game Design with Flash, but have a solid practical experience programming
with ActionScript 3.0 (AS3.0), this book is all you need to jump right in and start building games.
INTRODUCTION
xviii
However, make sure you have a comfortable understanding of these topics:
 Creating, controlling, and modifying Sprite and MovieClip objects
 Making and programming buttons
 Generating random numbers
 Controlling objects with the mouse and keyboard
 Moving objects with acceleration and friction
 Implementing collision detection: hitTestObject, hitTestPoint, and distance-based systems
 Using ADDED_TO_STAGE and REMOVED_FROM_STAGE events to reliably initialize and remove
display objects
 Changing the display object-stacking order
 Filtering display objects to add bevel and drop-shadow effects
 Using the Point class and converting local coordinates to global coordinates
 Object-oriented programming (OOP):
 Inheritance (making new classes by extending other classes)
 Composition (using instances of classes inside other classes)
 Private properties
 Getters and setters
 Different classes for game construction
 Dependency (building classes so that they can work without depending on other classes)
 Using static properties and methods
 Calculating distance and angles using the Math class
 Making drag-and-drop objects
 Using a Timer object

Flash IDE is that you can draw your game objects using its drawing tools, have access to the
movie clip timeline for doing animation, and create game objects using library instances.
 Flash Builder 4: Flash Builder (formerly Flex Builder) is optimized for AS3.0 programming. It
doesn’t allow you to create game objects visually—there are no drawing tools and no library. But
it’s probably the best pure-code AS3.0 editor available. Flash Builder is free for students.
If you don’t need or want all the bells and whistles of Flash Professional or Flash Builder, and don’t need to
do timeline animation or create symbols, you can use an alternative IDE:
 Flash Develop: A completely free AS3.0 programming IDE for Windows.
 Eclipse: A general IDE for any programming language, which can be customized for AS3.0. It’s
free and available for Windows, Linux, and Mac OS X. Flash Builder is actually based on Eclipse,
and Adobe has a Flash Builder plug-in you can use with it.
 Xcode: A free IDE from Apple that can be customized to work with AS3.0.
 Plain text editor: You can compile your code into a SWF file by using the Flex SDK command-
line interface.
 TextMate: It’s not free, but if you’re using Mac OS X, you may want to consider it as a leaner
alternative to Flash Builder or Eclipse. You’ll also need to install TextMate’s ActionScript 3.0
bundle, which is a plug-in for writing and compiling AS3 programs.
INTRODUCTION
xx
To use any of these alternative IDEs, you also need the following:
 Adobe’s free Flex Software Development Kit (SDK): This is the core software that compiles
SWF files from AS3.0 programs. Slightly confusingly, the Flex SDK is not just for creating Flex
applications. It’s used to compile any AS3.0 program into a SWF. You can download it for free
from Adobe’s website (http://opensource.adobe.com).
 Debug version of Flash Player: The debug version of Adobe Flash Player is for AS3.0
developers using the Flex SDK to compile their code. It allows you to see the output of trace
commands and runtime errors. If you’re unsure which version of Flash Player you currently have
installed, or where you can find the debug version, point your browser to
http://playerversion.com.
A bit of time set aside to learn how to install the SDK and make it work with your chosen IDE. The

Papervision3D, Alternativa3D, and Away3D.
How to read this book
This is not a book of quick fixes. It’s very unlikely that you’ll be able to skim through it and pick and choose
snippets of code to help you meet a pressing deadline. Instead, it’s a book about learning and
understanding. I’ve kept all the material very general and open-ended, so that it can be used for as wide
an application as possible. The techniques in this book won’t decide for you what kinds of games you’re
limited to making—that’s entirely up to you.
I’ve also kept the material as nonspecific to AS3.0 and Flash as possible. That means that most of the
concepts in this book can be applied to other platforms and programming languages. That’s important
because, if you you’re working at this level, it’s unlikely that AS3.0 is going to be the only language you
learn in your game-design career. Most of these concepts and techniques will be just as applicable to
Java, C++, and Objective-C as they are to AS3.0.
The content follows a very linear path. Most of the content in this book requires that you understand the
Model-View-Controller model (covered in Chapter 1) and basic vector math (covered in Chapter 2). So
you’re not going to get very far unless you’ve read the first two chapters. They’re must-reads. But with
those two chapters under your belt, you have a bit more flexibility:
Chapters 3 and 4 deal with specific areas of collision detection involving circles and polygons. If you don’t
think you’ll need to use that information right away, consider skipping ahead to Chapter 5. However, make
sure you read the section on abstract classes in Chapter 3 and the section on game structure in Chapter 4.
Chapter 6 contains a very important section on how to do bit-block transfers (blitting). If you work though
that section, you could skip ahead to Chapters 7 and 8, which depend on knowing that technique.
Chapters 8 and 9 are about a specific style of game design called tile-based games. You can combine a
tile-based design style with any of the other techniques in this book. Chapter 10 covers how to load and
save game data using some of AS3.0’s built-in tools.
To get the most benefit from this material, you should set aside some time to write your own code based
on the examples and concepts.
INTRODUCTION
xxii
Here’s how I suggest you go about learning each new topic:
 Understand it: Take as much time as you need to become fluent with the new code and new

the examples in each chapter. The AS files are in a folder called src, and the SWFs are in a
folder called bin. I’ll refer to these example files throughout the book, and you can test, change,
and play with the code as much as you like.
In the next section, I’ll explain how these files and folders work together, and how you’ll need to set up
your IDE to open, change, and recompile them.
INTRODUCTION
xxiii
Setting up your work environment
Over the decades that programming has matured as a craft, programmers have developed some
consistent conventions about where and how to store and organize different types of files. It’s worth taking
a bit of time to learn these conventions for a number of important reasons:
 It’s common for even small game-design projects to involve hundreds of image, code, sound,
text, and video files. If after working on a big project for a few weeks, you find that you spend
more time looking for files than actually doing any work with them, you’ll recognize how important
it is to implement a consistent organization system.
 Following conventions that others use is a way of learning from the wisdom of the crowd. Widely
adopted conventions evolve because a lot of people struggled with similar problems and, through
trial and error, found a system that helps to avoid them. Some of the conventions may not make
sense right away, but if you follow them, you’ll likely preempt a lot of problems that you might
never have considered could occur.
 If you do what everyone else is doing, you’ll be able to read their code, and they’ll be able to read
yours. Working in a team, this is essential. But even if you’re working alone, it can often be hard to
remember where you kept which file or how you wrote your code if you return to a project after a
holiday. Stick to standard conventions, and you’ll have a consistent system you can drop your work
into.
Organizing the project folder
The Chapter01 folder in the source files contains a subfolder called HelloWorld. It’s a model of the file
and folder organization convention that we’re going to follow in this book. If you open it and look at its
structure, you’ll see something like Figure 1.


Nhờ tải bản gốc

Tài liệu, ebook tham khảo khác

Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status