Siêu thị PDFTải ngay đi em, trời tối mất

Thư viện tri thức trực tuyến

Kho tài liệu với 50,000+ tài liệu học thuật

© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

Tài liệu ActionScript 3.0 Game Programming University doc
PREMIUM
Số trang
592
Kích thước
6.9 MB
Định dạng
PDF
Lượt xem
1018

Tài liệu ActionScript 3.0 Game Programming University doc

Nội dung xem thử

Mô tả chi tiết

ptg

ptg

Gary Rosenzweig

800 East 96th Street

Indianapolis, Indiana 46240 USA

Second Edition

Wow! eBook <WoweBook.Com>

ptg

ActionScript 3.0 Game

Programming University, Second Edition

Copyright © 2011 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-4732-7

ISBN-10: 0-7897-4732-4

Library of Congress Cataloging-in-Publication Data is on file.

Printed in the United States of America

First Printing: January 2011

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

[email protected]

For sales outside of the U.S., please contact

International Sales

[email protected]

Associate Publisher

Greg Wiegand

Acquisitions Editor

Laura Norman

Development Editor

Keith Cline

Managing Editor

Sandra Schroeder

Project Editor

Seth Kerney

Copy Editor

Jovana San-Nicolas Shirley

Indexer

Brad Herriman

Proofreader

Leslie Joseph

Technical Editor

Greg Kettell

Publishing Coordinator

Cindy Teeters

Book Designer

Anne Jones

Compositor

Mary Sudul

Graphics

William Follett

Eve Park

Wow! eBook <WoweBook.Com>

ptg

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: Air Raid II, Space Rocks, and

Balloon Pop

8. Casual Games: Match Three and Collapsing Blocks

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 Racing Games

13. Card Games: Higher or Lower, Video Poker,

and Blackjack

14. 3D Games: Target Practice, Racing Game,

and Dungeon Adventure

15. Building Games for the iPhone

Index

New!

New!

New!

Wow! eBook <WoweBook.Com>

ptg

Contents

Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xix

Flash and Game Development . . . . . . . . . . . . . . . . . . . . . . . . . . . .xx

Who Is This Book For? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxi

What Do You Need To Use This Book? . . . . . . . . . . . . . . . . . . . . .xxi

Prerequisite Knowledge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxi

Software Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxii

Source Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxii

Using the Example Games in Your Projects . . . . . . . . . . . . . . . . . .xxii

What You’ll Find in This Book . . . . . . . . . . . . . . . . . . . . . . . . . . .xxiii

The FlashGameU.com Website . . . . . . . . . . . . . . . . . . . . . . . . . . .xxiii

1. Using Flash and ActionScript 3.0 . . . . . . . . . . . . . . . . . . . .1

What Is ActionScript 3.0? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2

Creating a Simple ActionScript Program . . . . . . . . . . . . . . . . . . . . . .3

A Simple Use of trace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3

Creating Screen Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6

Our First ActionScript 3.0 Class . . . . . . . . . . . . . . . . . . . . . . . . . .8

Working with Flash CS5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .10

Display Objects and Display Lists . . . . . . . . . . . . . . . . . . . . . . . .10

The Stage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .11

The Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .11

The Timeline . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .12

Writing and Editing ActionScript . . . . . . . . . . . . . . . . . . . . . . . . . . .13

ActionScript Game Programming

Strategies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .16

Single Class Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .16

Smallest-Step Approach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .16

Good Programming Practices . . . . . . . . . . . . . . . . . . . . . . . . . . .17

Basic ActionScript Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . .20

Creating and Using Variables . . . . . . . . . . . . . . . . . . . . . . . . . . .20

Condition Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .21

Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .22

Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .23

Testing and Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .23

Types of Bugs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .23

Methods of Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .24

Using the Debugger . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .24

Wow! eBook <WoweBook.Com>

ptg

Publishing Your Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .27

Formats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .27

Flash . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .28

HTML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .29

ActionScript Game Programming

Checklist . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .30

Publishing and Document Settings . . . . . . . . . . . . . . . . . . . . . . .30

Class, Function, and Variable Names . . . . . . . . . . . . . . . . . . . . .31

Runtime Issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .32

Testing Issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .33

2. ActionScript Game Elements . . . . . . . . . . . . . . . . . . . . . . .35

Creating Visual Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .36

Using Movie Clips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .36

Making Buttons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .38

Drawing Shapes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .41

Drawing Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .43

Creating Linked Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .45

Creating Sprite Groups . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .47

Setting Sprite Depth . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .49

Accepting Player Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .50

Mouse Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .50

Keyboard Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .51

Text Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .52

Creating Animation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .54

Using Timers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .56

Time-Based Animation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .57

Physics-Based Animation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .58

Programming User Interaction . . . . . . . . . . . . . . . . . . . . . . . . . . . .59

Moving Sprites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .59

Dragging Sprites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .62

Collision Detection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .63

Accessing External Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .64

External Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .64

Loading Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .66

Saving Local Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .67

Miscellaneous Game Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . .68

Custom Cursors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .68

Playing Sounds . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .69

Loading Screen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .71

Random Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .72

Contents v

Wow! eBook <WoweBook.Com>

ptg

vi ActionScript 3.0 Game Programming University

Shuffling an Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .73

Displaying a Clock . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .74

System Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .75

Game Theft and Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .76

3. Basic Game Framework: A Matching Game . . . . . . . . . . . .79

Placing Interactive Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .80

Methods for Creating Game Pieces . . . . . . . . . . . . . . . . . . . . . . .80

Setting Up the Flash Movie . . . . . . . . . . . . . . . . . . . . . . . . . . . .82

Creating the Basic ActionScript Class . . . . . . . . . . . . . . . . . . . . .83

Using Constants for Better Coding . . . . . . . . . . . . . . . . . . . . . . .86

Shuffling and Assigning Cards . . . . . . . . . . . . . . . . . . . . . . . . . .88

Game Play . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .90

Adding Mouse Listeners . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .90

Setting Up Game Logic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .92

Checking for Game Over . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .95

Encapsulating the Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .97

Creating the Game Movie Clip . . . . . . . . . . . . . . . . . . . . . . . . . .98

Adding an Introduction Screen . . . . . . . . . . . . . . . . . . . . . . . . . .99

Adding a Play Again Button . . . . . . . . . . . . . . . . . . . . . . . . . . .100

Adding Scoring and a Clock . . . . . . . . . . . . . . . . . . . . . . . . . . . . .101

Adding Scoring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .101

Adding a Clock . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .104

Displaying Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .106

Displaying Score and Time After the Game Is Over . . . . . . . . . .107

Adding Game Effects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .109

Animated Card Flips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .109

Limited Card-Viewing Time . . . . . . . . . . . . . . . . . . . . . . . . . . .111

Sound Effects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .113

Modifying the Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .114

4. Brain Games: Memory and Deduction . . . . . . . . . . . . . . .117

Arrays and Data Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .118

Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .118

Data Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .120

Arrays of Data Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .121

Memory Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .121

Preparing the Movie . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .122

Programming Strategy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .123

Class Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .124

Setting the Text, Lights, and Sounds . . . . . . . . . . . . . . . . . . . . .126

Playing the Sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .129

Wow! eBook <WoweBook.Com>

ptg

Contents vii

Switching Lights On and Off . . . . . . . . . . . . . . . . . . . . . . . . . .130

Accepting and Checking Player Input . . . . . . . . . . . . . . . . . . . .131

Modifying the Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .133

Deduction Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .134

Setting Up the Movie . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .135

Defining the Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .137

Starting a New Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .139

Checking Player Guesses . . . . . . . . . . . . . . . . . . . . . . . . . . . . .141

Evaluating Player Moves . . . . . . . . . . . . . . . . . . . . . . . . . . . . .142

Ending the Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .145

Clearing Game Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . .147

Modifying the Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .148

5. Game Animation: Shooting and Bouncing Games . . . . . . .151

Game Animation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .152

Time-Based Animation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .152

Coding Time-Based Animation . . . . . . . . . . . . . . . . . . . . . . . . .154

Air Raid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .157

Movie Setup and Approach . . . . . . . . . . . . . . . . . . . . . . . . . . .158

Flying Airplanes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .159

Moving Gun . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .162

Skyward Bullets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .166

The Game Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .168

Modifying the Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .175

Paddle Ball . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .176

Setting Up the Movie . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .176

Class Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .179

Starting the Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .180

Starting a New Ball . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .182

Game Animation and Collision Detection . . . . . . . . . . . . . . . . .183

Game Over . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .189

Modifying the Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .190

6. Picture Puzzles: Sliding and Jigsaw . . . . . . . . . . . . . . . . .191

Manipulating Bitmap Images . . . . . . . . . . . . . . . . . . . . . . . . . . . .192

Loading a Bitmap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .192

Breaking a Bitmap into Pieces . . . . . . . . . . . . . . . . . . . . . . . . .194

Sliding Puzzle Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .196

Setting Up the Movie . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .197

Setting Up the Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .197

Loading the Image . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .200

Cutting the Bitmap into Pieces . . . . . . . . . . . . . . . . . . . . . . . . .200

Wow! eBook <WoweBook.Com>

ptg

Shuffling the Pieces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .202

Reacting to Player Clicks . . . . . . . . . . . . . . . . . . . . . . . . . . . . .205

Animating the Slide . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .207

Game Over and Cleanup . . . . . . . . . . . . . . . . . . . . . . . . . . . . .208

Modifying the Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .209

Jigsaw Puzzle Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .209

Setting Up the Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .210

Loading and Cutting the Image . . . . . . . . . . . . . . . . . . . . . . . .211

Dragging Puzzle Pieces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .215

Game Over . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .220

Modifying the Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .221

7. Direction and Movement: Air Raid II, Space Rocks, and

Balloon Pop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .223

Using Math to Rotate and Move Objects . . . . . . . . . . . . . . . . . . . .224

The Sin and Cos Functions . . . . . . . . . . . . . . . . . . . . . . . . . . .224

Using Cos and Sin to Drive a Car . . . . . . . . . . . . . . . . . . . . . . .226

Calculating an Angle from a Location . . . . . . . . . . . . . . . . . . . .229

Air Raid II . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .233

Altering the Gun . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .233

Changing the Bullets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .235

Changes to AirRaid2.as . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .237

Space Rocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .239

Game Elements and Design . . . . . . . . . . . . . . . . . . . . . . . . . . .239

Setting Up the Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . .241

Setting Up the Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .242

Starting the Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .244

Score and Status Display Objects . . . . . . . . . . . . . . . . . . . . . . .245

Ship Movement and Player Input . . . . . . . . . . . . . . . . . . . . . . .248

Shields Up! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .253

Rocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .254

Missiles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .257

Game Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .259

Modifying the Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .261

Balloon Pop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .262

Game Elements and Design . . . . . . . . . . . . . . . . . . . . . . . . . . .262

Setting Up the Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . .264

Setting Up the Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .264

Starting the Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .265

Preparing a Game Level . . . . . . . . . . . . . . . . . . . . . . . . . . . . .265

Main Game Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .266

Player Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .268

viii ActionScript 3.0 Game Programming University

New!

Wow! eBook <WoweBook.Com>

ptg

Popping Balloons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .270

Ending Levels and the Game . . . . . . . . . . . . . . . . . . . . . . . . . .270

Timeline Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .271

Modifying the Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .272

8. Casual Games: Match Three and Collapsing Blocks . . . . .273

Reusable Class: Point Bursts . . . . . . . . . . . . . . . . . . . . . . . . . . . . .274

Developing the Point Burst Class . . . . . . . . . . . . . . . . . . . . . . .275

Using Point Bursts in a Movie . . . . . . . . . . . . . . . . . . . . . . . . .279

Match Three . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .282

Playing Match Three . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .282

Game Functionality Overview . . . . . . . . . . . . . . . . . . . . . . . . . .283

The Movie and MatchThree Class . . . . . . . . . . . . . . . . . . . . . . .284

Setting Up the Grid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .285

Player Interaction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .288

Animating Piece Movement . . . . . . . . . . . . . . . . . . . . . . . . . . .291

Finding Matches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .293

Finding Possible Moves . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .297

Score Keeping and Game Over . . . . . . . . . . . . . . . . . . . . . . . .300

Modifying the Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .301

Collapsing Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .302

Setting Up the Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . .303

Setting Up the Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .304

Starting the Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .304

Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .306

Recursive Block Removal . . . . . . . . . . . . . . . . . . . . . . . . . . . . .308

Falling Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .311

Checking for Empty Columns . . . . . . . . . . . . . . . . . . . . . . . . . .312

Game Over . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .314

Modifying the Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .315

9. Word Games: Hangman and Word Search . . . . . . . . . . . .317

Strings and Text Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .318

ActionScript 3.0 String Handling . . . . . . . . . . . . . . . . . . . . . . .318

Applying Text Formatting to Text Fields . . . . . . . . . . . . . . . . . .322

Hangman . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .329

Setting Up the Hangman . . . . . . . . . . . . . . . . . . . . . . . . . . . . .329

The Hangman Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .330

Word Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .333

Development Strategy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .333

Defining the Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .335

Creating the Word Search Grid . . . . . . . . . . . . . . . . . . . . . . . .336

Contents ix

New!

Wow! eBook <WoweBook.Com>

ptg

User Interaction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .340

Dealing with Found Words . . . . . . . . . . . . . . . . . . . . . . . . . . . .343

Modifying the Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .346

10. Questions and Answers: Trivia and Quiz Games . . . . . . . .347

Storing and Retrieving Game Data . . . . . . . . . . . . . . . . . . . . . . . .348

Understanding XML Data . . . . . . . . . . . . . . . . . . . . . . . . . . . .348

Importing External XML Files . . . . . . . . . . . . . . . . . . . . . . . . . .350

Trapping Load Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .352

Trivia Quiz . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .352

Designing a Simple Quiz Game . . . . . . . . . . . . . . . . . . . . . . . .353

Setting Up the Movie . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .353

Setting Up the Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .354

Loading the Quiz Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .357

Message Text and Game Button . . . . . . . . . . . . . . . . . . . . . . . .357

Moving the Game Forward . . . . . . . . . . . . . . . . . . . . . . . . . . .359

Displaying the Questions and Answers . . . . . . . . . . . . . . . . . . .360

Judging the Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .362

Ending the Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .363

Deluxe Trivia Quiz . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .364

Adding a Time Limit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .364

Adding Hints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .367

Adding a Factoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .369

Adding Complex Scoring . . . . . . . . . . . . . . . . . . . . . . . . . . . . .370

Randomizing the Questions . . . . . . . . . . . . . . . . . . . . . . . . . . .372

Picture Quiz . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .373

Better Answer Arrangement . . . . . . . . . . . . . . . . . . . . . . . . . . .373

Recognizing Two Types of Answers . . . . . . . . . . . . . . . . . . . . .375

Creating Loader Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . .375

Determining the Right Answer . . . . . . . . . . . . . . . . . . . . . . . . .376

Expanding the Click Area . . . . . . . . . . . . . . . . . . . . . . . . . . . .377

Images for Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .378

Modifying the Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .379

11. Action Games: Platform Games . . . . . . . . . . . . . . . . . . . .381

Designing the Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .382

Level Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .382

Designing the Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .389

Planning Which Functions Are Needed . . . . . . . . . . . . . . . . . . .390

Building the Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .391

Class Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .391

Starting the Game and Level . . . . . . . . . . . . . . . . . . . . . . . . . .392

x ActionScript 3.0 Game Programming University

Wow! eBook <WoweBook.Com>

ptg

Keyboard Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .397

The Main Game Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .397

Character Movement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .399

Scrolling the Game Level . . . . . . . . . . . . . . . . . . . . . . . . . . . . .404

Checking for Collisions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .405

Enemy and Player Death . . . . . . . . . . . . . . . . . . . . . . . . . . . . .406

Collecting Points and Objects . . . . . . . . . . . . . . . . . . . . . . . . . .408

Showing Player Status . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .409

Ending the Levels and the Game . . . . . . . . . . . . . . . . . . . . . . .410

The Game Dialog Box . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .410

Modifying the Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .412

12. Game Worlds: Driving and Racing Games . . . . . . . . . . . . .413

Creating a Top-Down Driving Game . . . . . . . . . . . . . . . . . . . . . . .414

Creating a Top-Down World . . . . . . . . . . . . . . . . . . . . . . . . . .414

Game Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .417

The Class Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .420

The Constructor Function . . . . . . . . . . . . . . . . . . . . . . . . . . . .422

Finding the Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .424

Placing the Trash . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .424

Keyboard Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .426

The Game Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .427

Moving the Car . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .428

Checking for Trash and Trashcan Collisions . . . . . . . . . . . . . . .431

The Clock . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .433

The Score Indicators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .433

Game End . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .434

Modifying the Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .435

Building a Flash Racing Game . . . . . . . . . . . . . . . . . . . . . . . . . . .435

Racing Game Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .435

Making the Track . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .436

Sound Effects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .438

Constants and Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .438

Starting the Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .439

The Main Game Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .440

Car Movement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .442

Checking Progress . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .444

The Countdown and the Clock . . . . . . . . . . . . . . . . . . . . . . . . .445

Game Over . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .447

Modifying the Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .447

Contents xi

Wow! eBook <WoweBook.Com>

ptg

13. Card Games: Higher or Lower, Video Poker, and

Blackjack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .449

Higher or Lower . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .450

Creating the Deck . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .450

Setting Up the Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .451

Starting the Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .452

Responding to Player Moves . . . . . . . . . . . . . . . . . . . . . . . . . .454

Cleaning Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .455

Modifying the Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .456

Video Poker . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .456

Shuffle Up and Deal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .457

Timed Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .458

Making the Deck . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .458

Game Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .459

Setting Up the Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .460

Shuffling the Cards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .462

Timed Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .463

Here’s the Deal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .464

Drawing Cards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .466

Finishing a Hand . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .468

Calculating Poker Winnings . . . . . . . . . . . . . . . . . . . . . . . . . . .469

Modifying the Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .470

Blackjack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .470

Game Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .471

Setting Up the Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .471

Starting the Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .473

Timed Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .474

Dealing Cards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .475

Hit or Stay . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .476

The Dealer’s Moves . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .478

Calculating Blackjack Hands . . . . . . . . . . . . . . . . . . . . . . . . . . .478

Other Game Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .480

Modifying the Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .481

14. 3D Games: Target Practice, Racing Game, and

Dungeon Adventure . . . . . . . . . . . . . . . . . . . . . . . . . . . .483

Flash 3D Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .484

Setting 3D Positions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .484

Rotating Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .485

Target Practice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .487

Game Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .487

Setting Up the Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .489

xii ActionScript 3.0 Game Programming University

New!

New!

Wow! eBook <WoweBook.Com>

ptg

Starting the Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .489

Drawing the Cannon and Target . . . . . . . . . . . . . . . . . . . . . . .491

Moving the Cannon . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .492

Firing the Cannonball . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .492

Modifying the Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .493

3D Racing Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .494

Game Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .495

Setting Up the Movie . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .495

User Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .498

Player Movement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .500

Z-Index Sorting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .501

Modifying the Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .502

3D Dungeon Adventure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .503

Game Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .503

Setting Up the Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .504

Constructing the Dungeon . . . . . . . . . . . . . . . . . . . . . . . . . . . .505

Main Game Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .508

Player Movement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .509

Collecting Coins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .511

Game Limitations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .512

Extending the Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .512

15. Building Games for the iPhone . . . . . . . . . . . . . . . . . . . .515

Getting Started with iOS Development . . . . . . . . . . . . . . . . . . . . .516

What You Need . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .516

Publishing for iOS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .518

The iOS Game-Building Process . . . . . . . . . . . . . . . . . . . . . . . .522

Design and Programming Considerations . . . . . . . . . . . . . . . . . . .523

Screen Size . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .523

No Web Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .524

Touch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .524

Processor Speed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .525

Accelerometers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .525

Sliding Puzzle Adaptation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .527

Adjusting the Screen Size . . . . . . . . . . . . . . . . . . . . . . . . . . . . .527

Changing Publishing Settings . . . . . . . . . . . . . . . . . . . . . . . . . .528

Including the Image . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .528

Publishing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .529

Marble Maze Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .530

Setting Up the Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .530

Starting the Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .531

Contents xiii

New!

Wow! eBook <WoweBook.Com>

ptg

Game Play . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .533

Collision Detection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .534

Game Over . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .536

Modifying the Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .536

Optimizing for iOS Devices . . . . . . . . . . . . . . . . . . . . . . . . . . . . .537

Use the GPU and Bitmap Caching . . . . . . . . . . . . . . . . . . . . . .537

Object Pooling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .539

Simplifying Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .539

Minimizing Screen Redrawing . . . . . . . . . . . . . . . . . . . . . . . . .540

More Optimization Techniques . . . . . . . . . . . . . . . . . . . . . . . . .541

Beyond the iPhone . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .543

Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .545

xiv ActionScript 3.0 Game Programming University

Wow! eBook <WoweBook.Com>

Tải ngay đi em, còn do dự, trời tối mất!