ebook img

Foundation game design with ActionScript 3.0 PDF

677 Pages·2012·30.171 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Foundation game design with ActionScript 3.0

Foundation Game Design with ActionScript 3.0 Second Edition Rex van der Spuy 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: Copy Editor: Paul Manning Mary Behr Lead Editor: Compositor: Ben Renow-Clarke Apress (Brigid Duffy) Technical Reviewers: Production Editor: Joshua Freeney Brigid Duffy Editorial Board: Indexer: Steve Anglin, Mark Beckner, Ewan Buckingham, Gary BIM Indexing & Proofreading Services Cornell, Morgan Ertel, Jonathan Gennick, Jonathan Hassell, Robert Hutchinson, Michelle Lowman, James Artist: Markham, Matthew Moodie, Jeff Olson, Jeffrey Pepper, SPi Global Douglas Pundick, Ben Renow-Clarke, Dominic Cover Image Artist: Shakeshaft, Gwenan Spearing, Matt Wade, Tom Corné van Dooren Welsh Cover Designer: Coordinating Editor: Anna Ishchenko Kelly Moritz 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 AT A GLANCE Contents _________________________________________________________________________________ About the Author………………………………………………………………...xiii About the Technical Reviewer………………………………………………...xiv About the Cover Image Designer……………………………………………..xv Acknowledgements…………………………………………………………….xvi Introduction…………………………………………………………………..…xvii Chapter 1: Programming Foundations: How To Make A Video Game……… ..1 Basic requirements .............................................................................................................. 2 1. A computer .................................................................................................................... 2 2. Graphic design software ............................................................................................... 2 3. Computer programming software ................................................................................. 3 Adobe Flash Professional ................................................................................................. 5 Things you need to know ..................................................................................................... 5 And the things you don’t need to know ................................................................................ 6 It’s all about programming .................................................................................................... 6 Programming? But I’m terrible at math! ............................................................................... 7 I already know how to program! ........................................................................................... 8 iv What kind of games can I make? ..................................................................................... 8 Learning new terms .......................................................................................................... 9 Laying the foundation ....................................................................................................... 9 Using Flash Builder ............................................................................................................ 10 Setting up the work environment in Flash Builder .......................................................... 10 Writing your first program ................................................................................................... 20 ActionScript files and the .as file extension ....................................................................... 20 I’ll take that to go! ............................................................................................................... 22 A little magic with sprites ................................................................................................ 23 Don’t skip class! .............................................................................................................. 25 Using the constructor method ......................................................................................... 27 Aligning code .................................................................................................................. 29 What’s your directive? ........................................................................................................ 32 Adding comments to your code ......................................................................................... 34 Running your program and compiling the SWF file ........................................................... 36 What happened when you compiled the program .......................................................... 38 It didn’t work? ..................................................................................................................... 40 Some common error messages ...................................................................................... 43 Confirm your program with the original source files ....................................................... 45 More about Flash Builder ................................................................................................... 45 Editing window quirks and features ................................................................................ 45 Perspectives ................................................................................................................... 48 A little more about AS3.0 and the Flash Player ................................................................. 52 Naming conventions........................................................................................................... 54 Summary ............................................................................................................................ 55 Chapter 2: Making Game Graphics ..................................................................... 57 Create a game world background ...................................................................................... 59 Create a new file in Photoshop ....................................................................................... 60 Draw the sky ................................................................................................................... 64 Drawing hills .................................................................................................................... 76 Drawing with the pen tool ............................................................................................... 79 v Duplicating, modifying, and arranging layers .................................................................. 91 Bevel and emboss .......................................................................................................... 96 Making boxes with the rectangular selection tool ......................................................... 100 Making a game character ................................................................................................ 106 Create a new file ........................................................................................................... 106 Draw your character ..................................................................................................... 107 Making buttons ................................................................................................................. 110 Create a new file ........................................................................................................... 111 Draw the button............................................................................................................. 111 Using the type tool ........................................................................................................ 114 Making more buttons .................................................................................................... 115 Save your work and make the PNG files ...................................................................... 117 Checking your work.......................................................................................................... 118 More about Photoshop ..................................................................................................... 119 Summary .......................................................................................................................... 120 Chapter 3: Programming Objects ...................................................................... 121 But I’m a bit scared of programming! ............................................................................... 122 Loading and displaying images ........................................................................................ 123 Setting up a new AS3.0 project .................................................................................... 124 Understanding the setup file ......................................................................................... 126 Set up the Flash Player with a metadata tag ................................................................ 127 Loading and displaying the background.png image ..................................................... 130 How did that work? ....................................................................................................... 132 Variables ....................................................................................................................... 134 Variable types ............................................................................................................... 135 Creating empty boxes ................................................................................................... 136 Creating instances and objects..................................................................................... 137 Displaying the image on the stage ............................................................................... 140 Understanding the code structure................................................................................. 147 Positioning Sprites on the stage ................................................................................... 148 Programming buttons ....................................................................................................... 158 vi Understanding dot notation ........................................................................................... 159 Methods ........................................................................................................................ 159 Using method calls ....................................................................................................... 160 Using function definitions .............................................................................................. 161 Using a basic method ................................................................................................... 163 Understanding events and event listeners ................................................................... 168 Making your buttons work ............................................................................................. 177 Controlling Sprite objects with properties......................................................................... 178 Going up and down ....................................................................................................... 180 Moving incrementally ....................................................................................................... 182 Tracing the output ......................................................................................................... 183 Using increment and decrement operators .................................................................. 184 Limiting movement ........................................................................................................ 185 Making it bigger and smaller ......................................................................................... 189 Vanishing! ..................................................................................................................... 192 Spinning around ............................................................................................................ 195 More properties? ........................................................................................................... 198 Centering Sprites ............................................................................................................. 198 Code summary ................................................................................................................. 202 Summary ....................................................................................................................... 206 Chapter 4: Decision Making ............................................................................... 209 Using text ......................................................................................................................... 210 Setting up the project .................................................................................................... 210 The classes you have to import .................................................................................... 212 Creating TextFormat and TextField objects ................................................................. 213 Setting the format of text ............................................................................................... 214 Configuring and displaying the output text ................................................................... 216 Configuring and displaying the input field ..................................................................... 218 Building a simple guessing game .................................................................................... 222 Understanding the program structure ........................................................................... 222 Learning more about variables ........................................................................................ 224 vii Listening for key presses .............................................................................................. 227 Making decisions .............................................................................................................. 231 Displaying the game status ........................................................................................... 238 Using postfix operators to change variable values by one .............................................. 240 Tying up strings............................................................................................................. 241 Hey, why use the gameStatus variable, anyway? ........................................................ 245 Using uint vs. int variables ............................................................................................ 245 Winning and losing ....................................................................................................... 246 Modular programming with methods ............................................................................ 252 Polishing up ...................................................................................................................... 253 Tackling random numbers ............................................................................................ 254 Disabling the Enter key ................................................................................................. 258 Making really nice buttons ............................................................................................... 260 Understanding the concepts ......................................................................................... 262 Loading the images and displaying the button ............................................................. 263 Understanding the mouse events ................................................................................. 265 Adding the button to the number guessing game ............................................................ 271 Disabling the button at the end of the game ................................................................. 272 Managing complex code ............................................................................................... 278 Project extensions ............................................................................................................ 279 Make a Play again button ............................................................................................. 279 Tracking guesses .......................................................................................................... 280 Adding a visual display ................................................................................................. 281 Turning the tables ......................................................................................................... 281 A quick guide to embedding fonts .................................................................................... 281 Summary .......................................................................................................................... 284 Chapter 5: Controlling A Player Character ....................................................... 285 Controlling a player character with the keyboard ............................................................. 285 Controlling with the keyboard—the wrong way! ........................................................... 286 Controlling the keyboard—the right way! ...................................................................... 290 Moving with velocity ...................................................................................................... 292 viii Using the new keyDownHandler ................................................................................... 294 Using the keyUpHandler ............................................................................................... 294 Using the enterFrameHandler ...................................................................................... 296 Setting stage boundaries .............................................................................................. 298 Blocking movement at the stage edges ........................................................................ 298 Screen wrapping ........................................................................................................... 303 Embedding images .......................................................................................................... 304 Scrolling ........................................................................................................................... 307 Looking at the code ...................................................................................................... 310 Better scrolling .............................................................................................................. 313 Even better scrolling ..................................................................................................... 320 Taking it further ................................................................................................................ 323 Parallax scrolling ........................................................................................................... 324 Summary .......................................................................................................................... 329 Chapter 6: Bumping Into Things ....................................................................... 331 Basic collision detection with hitTestObject .................................................................. 331 Using hitTestObject ...................................................................................................... 335 Triggering a change of state ............................................................................................ 337 Reducing a health meter ............................................................................................... 341 Using scaleX to scale the meter based on a percentage ............................................. 343 Updating a score ........................................................................................................... 344 Picking up and dropping objects ................................................................................... 351 The bad news about hitTestObject .................................................................................. 357 Detecting collisions with the bounding box ................................................................... 358 Learning to live with it ................................................................................................... 359 Creating subobjects ...................................................................................................... 360 Advanced collision detection strategies ........................................................................ 364 Working with vector based collision detection ................................................................. 365 Preventing objects from overlapping ............................................................................ 365 Programming with the Collision class ........................................................................... 373 Another look at methods, arguments, and parameters ................................................. 378 ix

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.