Gary Rosenzweig
800 East 96th Street
Indianapolis, Indiana 46240 USA
ActionScript 3.0 Game
Programming University
Copyright
©
2008 by Que Publishing
All rights reserved. No part of this book shall be reproduced, stored in a retrieval
system, or transmitted by any means, electronic, mechanical, photocopying, recording, or other-
wise, without written permission from the publisher. No patent liability is assumed with respect
to the use of the information contained herein. Although every precaution has been taken in the
preparation of this book, the publisher and author assume no responsibility for errors or omis-
sions. Nor is any liability assumed for damages resulting from the use of the information
contained herein.
ISBN-13: 978-0-7897-3702-1
ISBN-10: 0-7897-3702-7
Library of Congress Cataloging-in-Publication Data:
Rosenzweig, Gary.
ActionScript 3.0 game programming university / Gary Rosenzweig.
p. cm.
ISBN 0-7897-3702-7
1. Computer games—Programming. 2. Computer animation. 3. ActionScript
(Computer program language) I. Title.
QA76.76.C672R73 2007
794.8'1526—dc22
2007028659
Printed in the United States of America
First Printing: August 2007
Trademarks
All terms mentioned in this book that are known to be trademarks or service marks have been
appropriately capitalized. Que Publishing cannot attest to the accuracy of this information. Use of a
term in this book should not be regarded as affecting the validity of any trademark or service mark.
Warning and Disclaimer
Every effort has been made to make this book as complete and as accurate as possible, but no
warranty or fitness is implied. The information provided is on an “as is” basis. The author and
the publisher shall have neither liability nor responsibility to any person or entity with respect to
any loss or damages arising from the information contained in this book or from the use of
programs accompanying it.
Bulk Sales
Que Publishing offers excellent discounts on this book when ordered in quantity for bulk pur-
chases or special sales. For more information, please contact
U.S. Corporate and Government Sales
1-800-382-3419
corpsales@pearsontechgroup.com
For sales outside of the U.S., please contact
International Sales
international@pearsoned.com
Associate Publisher
Greg Wiegand
Acquisitions Editor
Laura Norman
Development Editor
Laura Norman
Managing Editor
Gina Kanouse
Project Editor
Anne Goebel
Copy Editor
Keith Cline
Senior Indexer
Cheryl Lenser
Proofreader
Heather Waye Arle
Technical Editor
Cheryl Brumbaugh-Duncan
Publishing Coordinator
Cindy Teeters
Book Designer
Anne Jones
Senior Compositor
Gloria Schurick
Graphics
William Follett
Eve Park
Contents at a Glance
Introduction
1. Using Flash and ActionScript 3.0
2. ActionScript Game Elements
3. Basic Game Framework: A Matching Game
4. Brain Games: Memory and Deduction
5. Game Animation: Shooting and Bouncing Games
6. Picture Puzzles: Sliding and Jigsaw
7. Direction and Movement: Space Rocks
8. Casual Games: Match Three
9. Word Games: Hangman and Word Search
10. Questions and Answers: Trivia and Quiz Games
11. Action Games: Platform Games
12. Game Worlds: Driving and Exploration Game
Index
Contents
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1
Flash and Game Development . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2
Who Is This Book For? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3
What Do You Need To Use This Book? . . . . . . . . . . . . . . . . . . . . . .3
Prerequisite Knowledge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3
Software Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4
Source Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4
Using the Example Games in Your Projects . . . . . . . . . . . . . . . . . . . .4
What You’ll Find in This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . .5
The FlashGameU.com Website . . . . . . . . . . . . . . . . . . . . . . . . . . . . .5
1. Using Flash and ActionScript 3.0 . . . . . . . . . . . . . . . . . . . .7
What Is ActionScript 3.0? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8
Creating a Simple ActionScript Program . . . . . . . . . . . . . . . . . . . . . .9
A Simple Use of trace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .9
Creating Screen Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .12
Our First ActionScript 3.0 Class . . . . . . . . . . . . . . . . . . . . . . . . .13
Working with Flash CS3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .15
Display Objects and Display Lists . . . . . . . . . . . . . . . . . . . . . . . .16
The Stage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .16
The Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .17
The Timeline . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .18
Writing and Editing ActionScript . . . . . . . . . . . . . . . . . . . . . . . . . . .19
ActionScript Game Programming Strategies . . . . . . . . . . . . . . . . . .22
Single Class Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .22
Smallest Step Approach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .22
Good Programming Practices . . . . . . . . . . . . . . . . . . . . . . . . . . .23
Basic ActionScript Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . .26
Creating and Using Variables . . . . . . . . . . . . . . . . . . . . . . . . . . .26
Condition Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .27
Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .28
Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .29
Testing and Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .29
Types of Bugs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .29
Methods of Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .30
Using the Debugger . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .31