Foundation Game Design with ActionScript 3.0, Second Edition

Foundation Game Design with ActionScript 3.0, Second Edition

Copyright © 2012 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-3993-2

ISBN-13 (electronic): 978-1-4302-3994-9

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 New York, 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/bulk-sales
.

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 the
work.

Any source code or other supplementary materials referenced by the author in this text is available to readers at
www.apress.com
. For detailed information about how to locate your book's source code, go to
www.apress.com/source-code/
.

Credits

President and Publisher:
Paul Manning
Lead Editor:
Ben Renow-Clarke
Technical Reviewers:
Joshua Freeney
Editorial Board:
Steve Anglin, Mark Beckner, Ewan Buckingham, Gary
Cornell, Morgan Ertel, Jonathan Gennick, Jonathan
Hassell, Robert Hutchinson, Michelle Lowman, James
Markham, Matthew Moodie, Jeff Olson, Jeffrey Pepper,
Douglas Pundick, Ben Renow-Clarke, Dominic
Shakeshaft, Gwenan Spearing, Matt Wade, Tom
Welsh
Coordinating Editor:
Kelly Moritz
    
Copy Editor:
    Mary Behr
    
Compositor:
    Apress (Brigid Duffy)
    
Production Editor:
    Brigid Duffy
    
Indexer:
    BIM Indexing & Proofreading Services
    
Artist:
    SPi Global
    
Cover Image Artist:
    Corné van Dooren
    
Cover Designer:
    Anna Ishchenko

Dedicated to my father, Mani van der Spuy,
without whom this book would not have been written.
Thanks, Dad, for always believing in me.

Contents

About the Author

About the Technical Reviewer

About the Cover Image Designer

Acknowledgments

Introduction

Chapter 1: Programming Foundations: How To Make A Video Game

Basic requirements

1. A computer

2. Graphic design software

3. Computer programming software

Adobe Flash Professional

Things you need to know

And the things you don't need to know

It's all about programming

Programming? But I'm terrible at math!

I already know how to program!

What kind of games can I make?

Learning new terms

Laying the foundation

Using Flash Builder

Setting up the work environment in Flash Builder

Writing your first program

ActionScript files and the .as file extension

I'll take that to go!

A little magic with sprites

Don't skip class!

Using the constructor method

Aligning code

What's your directive?

Adding comments to your code

Running your program and compiling the SWF file

What happened when you compiled the program

It didn't work?

Some common error messages

Confirm your program with the original source files

More about Flash Builder

Editing window quirks and features

Perspectives

A little more about AS3.0 and the Flash Player

Naming conventions

Summary

Chapter 2: Making Game Graphics

Create a game world background

Create a new file in Photoshop

Draw the sky

Drawing hills

Drawing with the pen tool

Duplicating, modifying, and arranging layers

Bevel and emboss

Making boxes with the rectangular selection tool

Making a game character

Create a new file

Draw your character

Making buttons

Create a new file

Draw the button

Using the type tool

Making more buttons

Save your work and make the PNG files

Checking your work

More about Photoshop

Summary

Chapter 3: Programming Objects

But I'm a bit scared of programming!

Loading and displaying images

Setting up a new AS3.0 project

Understanding the setup file

Set up the Flash Player with a metadata tag

Loading and displaying the background.png image

How did that work?

Variables

Variable types

Creating empty boxes

Creating instances and objects

Displaying the image on the stage

Understanding the code structure

Positioning Sprites on the stage

Programming buttons

Understanding dot notation

Methods

Using method calls

Using function definitions

Using a basic method

Understanding events and event listeners

Making your buttons work

Controlling Sprite objects with properties

Going up and down

Moving incrementally

Tracing the output

Using increment and decrement operators

Limiting movement

Making it bigger and smaller

Vanishing!

Spinning around

More properties?

Centering Sprites

Code summary

Summary

Chapter 4: Decision Making

Using text

Setting up the project

The classes you have to import

Creating TextFormat and TextField objects

Setting the format of text

Configuring and displaying the output text

Configuring and displaying the input field

Building a simple guessing game

Understanding the program structure

Learning more about variables

Listening for key presses

Making decisions

Displaying the game status

Using postfix operators to change variable values by one

Tying up strings

Hey, why use the gameStatus variable, anyway?

Using uint vs. int variables

Winning and losing

Modular programming with methods

Polishing up

Tackling random numbers

Disabling the Enter key

Making really nice buttons

Understanding the concepts

Loading the images and displaying the button

Understanding the mouse events

Adding the button to the number guessing game

Disabling the button at the end of the game

Managing complex code

Project extensions

Make a Play again button

Tracking guesses

Adding a visual display

Turning the tables

A quick guide to embedding fonts

Summary

Chapter 5: Controlling A Player Character

Controlling a player character with the keyboard

Controlling with the keyboard'the wrong way!

Controlling the keyboard'the right way!

Moving with velocity

Using the new keyDownHandler

Using the keyUpHandler

Using the enterFrameHandler

Setting stage boundaries

Blocking movement at the stage edges

Screen wrapping

Embedding images

Scrolling

Looking at the code

Better scrolling

Even better scrolling

Taking it further

Parallax scrolling

Summary

Chapter 6: Bumping Into Things

Basic collision detection with hitTestObject

Using hitTestObject

Triggering a change of state

Reducing a health meter

Using scaleX to scale the meter based on a percentage

Updating a score

Picking up and dropping objects

The bad news about hitTestObject

Detecting collisions with the bounding box

Learning to live with it

Creating subobjects

Advanced collision detection strategies

Working with vector based collision detection

Preventing objects from overlapping

Programming with the Collision class

Another look at methods, arguments, and parameters

Taking a look at the Collision.block method

Summary

Chapter 7: Making Games

Making the game graphics

Making the textured box

Making the bomb

Making a game character

Designing the playing field

Making the Game Over graphic

Programming the game

Dealing with many objects

What is a class, really?

Looking at the TimeBombPanic application class

Adding objects to the game

Stage boundaries

Collisions between the character and the boxes

Defusing the bombs

Using a timer

Ending the game

Creating a scrolling game environment

Preparing your artwork for scrolling

Adding objects to the game

Scrolling and stage boundaries

Summary

Chapter 8: Making Bigger Games

Understanding the game structure

Using private variables and methods

Programming the game

Giving LevelOne access to the stage

Making sure that LevelOne is actually on the stage

The LevelOne class

Programming the game character

Programming the monsters

Programming the star weapon

Checking for the end of the level

Ending the level

Using removeChild to completely get rid of objects

Communicating between classes by dispatching events

Event bubbling

Finishing off level one

Firing stars in four directions in level two

More Monster Mayhem!

Moving objects in a scrolling game world

Intelligent monsters

Obstacles and pathfinding

A little more about game structure

Summary

Chapter 9: Physics, Loops, Arrays, and Sounds

Natural motion using physics

A game object class for playing with physics

Acceleration

Friction

Bouncing

Gravity

Jumping

Adding sounds to games

Integrating sound effects in a game

Looping music and setting volume and pan levels

Working with many objects

Using arrays

Making many boxes

Collisions with many objects

Case studies

Summary

Chapter 10: Advanced Object And Character Control

Moving objects with the mouse

Fixing an object to the mouse's position

Moving an object with easing

Case study: Firing projectiles in all directions

Making the game objects

Rotating the wand around the fairy

Firing stars in 360 degrees

Advanced player control systems

Enemy AI systems

Running away from the player

Rotating and shooting toward the mouse

Using a timer to fire bullets

Case study: Killer Bee Pandemonium!

Flash animation and publishing your game

Index

Other books

The Silver Thread by Emigh Cannaday
Candy by Kevin Brooks
Toby by Todd Babiak
Once in a Lifetime by Danielle Steel
Maybe a Fox by Kathi Appelt
What Would Mr. Darcy Do? by Abigail Reynolds
El gran reloj by Kenneth Fearing
Karna's Wife by Kane, Kavita