20

J2me game programming

Embed Size (px)

DESCRIPTION

j2me game,java game,game java free,tai game java,java mobile,java game development,download java game cho dien thoai

Citation preview

Page 1: J2me game programming
Page 2: J2me game programming
Page 3: J2me game programming

This page intentionally left blank

Page 4: J2me game programming
Page 5: J2me game programming

© 2004 by Premier Press, a division of Course Technology. All rights reserved. Nopart of this book may be reproduced or transmitted in any form or by any means,electronic or mechanical, including photocopying, recording, or by any informa-tion storage or retrieval system without written permission from Course PTR,except for the inclusion of brief quotations in a review.

The Premier Press logo and related trade dress are trademarks of Premier Pressand may not be used without written permission.

J2ME is a trademark of Sun Microsystems, Inc. in the United States and othercountries. Photoshop and Acrobat are registered trademarks of Adobe SystemsIncorporated in the United States and other countries. Paint Shop Pro is a regis-tered trademark of Jasc Software in the United States and other countries.SourceForge is a trademark of VA Software Corporation. RetroGuard is a trade-mark of Retrologic Systems in the United States and other countries. The MathFPpackage can only be used for non-commercial purposes. Permission to use theNokia Developer’s Suite 2.0 for J2ME, Nokia 7210 MIDP SDK 1.0, and, Nokia3510i MIDP SDK 1.0, have been granted by Nokia, Inc. (c) 2004. Nokia, Inc. Allrights reserved.

All other trademarks are the property of their respective owners.

Important: Course PTR cannot provide software support. Please contact the appro-priate software manufacturer’s technical support line or Web site for assistance.

Course PTR and the author have attempted throughout this book to distinguishproprietary trademarks from descriptive terms by following the capitalizationstyle used by the manufacturer.

Information contained in this book has been obtained by Course PTR from sourcesbelieved to be reliable. However, because of the possibility of human or mechani-cal error by our sources, Course PTR, or others, the Publisher does not guaranteethe accuracy, adequacy, or completeness of any information and is not responsiblefor any errors or omissions or the results obtained from use of such information.Readers should be particularly aware of the fact that the Internet is an ever-chang-ing entity. Some facts may have changed since this book went to press.

Educational facilities, companies, and organizations interested in multiple copiesor licensing of this book should contact the publisher for quantity discount infor-mation. Training manuals, CD-ROMs, and portions of this book are also avail-able individually or can be tailored for specific needs.

ISBN: 1-59200-118-1Library of Congress Catalog Card Number: 2003109341Printed in the United States of America

Course PTR, a division of Course Technology25 Thomson PlaceBoston, MA 02210

http://www.courseptr.com

SVP, Course Professional,Trade, Reference Group:Andy Shafran

Publisher:Stacy L. Hiquet

Senior Marketing Manager:Sarah O’Donnell

Marketing Manager:Heather Hurley

Manager of Editorial Services:Heather Talbot

Acquisitions Editor:Mitzi Koontz

Associate Marketing Manager:Kristin Eisenzopf

Project Editor:Jenny Davidson

Technical Reviewer:Cristiano Garibaldi

Retail Market Coordinator:Sarah Dubois

Copy Editor:Cathleen Snyder

Interior Layout Tech:Marian Hartsough

Cover Designer:Steve Deschene

CD-ROM Producer:Brandon Penticuff

Indexer:Sharon Shock

Proofreader:Sara Gullion

Page 6: J2me game programming

For G. I.

Page 7: J2me game programming

If you’ve never written a book, I have to tell you that it’s not easy. Once upon a time Iwas an armchair critic of books—in hindsight, maybe that’s why I decided to writeone. The thing that strikes you when you embark on writing something like this is the

sheer mountain of (hard) work involved. In fact, it’s so much work that it really isn’t pos-sible for one person to do it alone. Throughout the writing of this book, I’ve been helpedin so many ways by so many people that I can’t hope to get across how grateful I am. AllI can do is say a thank you and trust you to know how much I mean it.

I will, however, attempt to recognize some of the people who deserve a little acknowledg-ment—if I’ve forgotten you, then obviously you didn’t deserve it. . . . :)

First, thanks to André LaMothe for giving me the opportunity to contribute to such anexcellent series. To Mitzi Koontz, Jenny Davidson, and Cathleen Snyder for the excellentfeedback, continual support, and understanding when things didn’t quite go according toplan—we got there in the end. Thanks also to Cristiano Garibaldi for covering all thetechnical bases (and learning along with me).

To Colin Pyle and J. Alexander von Kotze: Thanks for never giving up on the dream ofmaking games for a living (and an extra thanks to Colin for supplying the excellent spritesused in the examples).

To Blake, for allowing me to use his laptop sometimes, and to Ryan, for showing me howa CD drive opens a thousand times. To Vandana and Pratibha Rai for buying me sometime (and thanks for the math books laaa). To Scott, Rhandy, Simon, Rob, Gibbo, Mike,Jules, Kristy, Kat, Lee, and the rest of the team for keeping the day job challenging, inter-esting, and fun.

vi

Acknowledgments

Page 8: J2me game programming

To Tarek, Sahar, Radfan, and Suleiman for the encouragement and support only friendscan give.

To Rick, my only mentor, for showing me that computing really is a science.

To my mum, for showing me that being creative is a way of life and dad for throwing inregular doses of reality.

And finally, to the one and only G.I.: Thanks for believing from day one we could do it,and then bearing the brunt of following through on that belief.

Acknowledgments vii

Page 9: J2me game programming

MARTIN J. WELLS began programming his own games on a Tandy micro-computer morethan 20 years ago. Throughout an extensive career in the IT industry, he has worked inmany diverse fields involving a huge variety of computer languages and systems, includ-ing Java from its origins. He has extensive experience in media, communications, andentertainment industry development and has founded successful companies in all of theseareas.

Martin lives with his wife and two sons in Sydney, Australia. He loves playing soccer andinline hockey, reading, and playing with anything cool and interesting (including his sons).

About the Series EditorANDRÉ LAMOTHE, CEO, Xtreme Games LLC, has been involved in the computing indus-try for more than 25 years. He wrote his first game for the TRS-80 and has been hookedever since! His experience includes 2D/3D graphics, AI research at NASA, compilerdesign, robotics, virtual reality, and telecommunications. His books are top sellers inthe game programming genre, and his experience is echoed in the Premier Press GameDevelopment books.

viii

About the Author

Page 10: J2me game programming

Letter from the Series Editor

Writing games for PCs is fun, but it just doesn’t have the feel of a console or other hand-held device. However, the thought of creating an embedded game for a phone was com-pletely out of the question a few years ago, unless you wanted to call up Nokia orMotorola and see if you could have the contract to create the on-board games. (I wishsomeone would have; they are terrible!) Anyway, luckily for us, new phones support anumber of technologies that allow programmers to create fantastic applications. Onesuch technology is Java II Micro Edition, or J2ME. And that’s what this book is allabout—writing games for any phone that supports the J2ME standard.

When I first thought of doing a book on J2ME game programming, I knew that I wantedit to push the envelope to set a new standard on what can be done on a phone. Thatmeans I had to find an author who was an expert on the platform, but was also willingto push limits and do the impossible, in a manner of speaking. I have to say that I amvery happy with this book. The author, Martin Wells, had the same vision about want-ing to create the most amazing book on phone/J2ME game programming. For example,he knew that he had to put a chapter on 3D in the book and talk about optimization andother advanced topics. The bottom line is that this book is the best book on the marketabout making real games on the J2ME platform; moreover, it’s written by someone whohas made numerous games on the platform. Marty knows the ins and outs and tricks ofthe system, which is invaluable in such a complex subject area with so many otherchoices to confuse you.

The other amazing thing about this book is that it is completely self-contained; if youdon’t know Java 2, there is a Java 2 primer contained within, so more or less all you needis your phone, the book, and some time and you are going to be creating J2ME games onyour own phone! I think that this is an amazing thing to be able to do. It’s like havingyour own little game console in your hand. You can play your own games or give themto your friends, or possibly even sell and market them (which is also covered within thebook).

In conclusion, if you have been interested in writing games for phones under the J2MEplatform, but don’t know where to start, how to integrate all the technology, or makesense of all the different APIs, then this is the book for you. Rarely can a single bookempower someone to do so much, but Martin Wells has done an amazing job of it.

André LaMotheSeries Editor, Premier Game Development Series

Page 11: J2me game programming

Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxv

Part I What Is J2ME? . . . . . . . . . . . . . . . . . . . . . .1

Chapter 1 J2ME History . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3

Chapter 2 J2ME Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .13

Chapter 3 J2ME-Enabled Devices . . . . . . . . . . . . . . . . . . . . . . . . . . . . .39

Part II Show Me the Code! . . . . . . . . . . . . . . . . .47

Chapter 4 The Development Environment . . . . . . . . . . . . . . . . . . . . . .49

Chapter 5 The J2ME API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .77

Chapter 6 Device-Specific Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . .173

Chapter 7 Game Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .211

Part III Game On . . . . . . . . . . . . . . . . . . . . . . . .245

Chapter 8 The Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .247

Chapter 9 The Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .269

x

Contents at a Glance

Page 12: J2me game programming

Chapter 10 The Action . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .303

Chapter 11 The World . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .367

Chapter 12 The Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .433

Chapter 13 The Front End . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .465

Chapter 14 The Device Ports . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .493

Chapter 15 The Optimizing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .519

Chapter 16 The Localization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .535

Part IV Sell, Sell, Sell . . . . . . . . . . . . . . . . . .541

Chapter 17 Marketing Material . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .543

Chapter 18 Sales Channels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .553

Part V What Next? . . . . . . . . . . . . . . . . . . . . . .569

Chapter 19 CLDC 1.1 and MIDP 2.0 . . . . . . . . . . . . . . . . . . . . . . . . . . .571

Chapter 20 Isometric Games . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .621

Chapter 21 Ray Casting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .639

Chapter 22 Making the Connection . . . . . . . . . . . . . . . . . . . . . . . . . . .669

Appendix A Java 2 Primer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .693

Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .738

Contents at a Glance xi

Page 13: J2me game programming

Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv

Part I What Is J2ME? . . . . . . . . . . . . . . . . . . . . . .1

Chapter 1 J2ME History . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3Java’s Acorn . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3Java’s Growth in the Sun . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6So What Is Java? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6Multiple Editions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .9Micro Devices Everywhere . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .9Micro Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .10Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .12

Chapter 2 J2ME Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .13I Shall Call It Mini-ME . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .13J2ME Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .14

Configurations and Profiles . . . . . . . . . . . . . . . . . . . . . . . . . . . . .15Two Sizes Fit All . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .17

MIDP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .28Target Devices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .28MIDP Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .31MIDP 1.0 Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .35

MIDP 2.0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .37Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .38

xii

Contents

Page 14: J2me game programming

Chapter 3 J2ME-Enabled Devices . . . . . . . . . . . . . . . . . . . . . . . . . . . . .39MID Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .39

Nokia . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .40Sony Ericsson . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .44Motorola . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .45

Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .46

Part II Show Me the Code! . . . . . . . . . . . . . . . . .47

Chapter 4 The Development Environment . . . . . . . . . . . . . . . . . . . . .49Getting the Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .49Installing the Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .50

Setting Up the JDK . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .51Setting Up the MIDP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .51Making Path Changes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .52Setting MIDP_HOME . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .53Updating CLASSPATH . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .54Testing the Setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .54

Baking Some MIDlets! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .55Compiling Your Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . .58Preverifying Your Class File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .58Running Your MIDlet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .59

Creating the Full Package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .59Hello, Again . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .60Building the Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .61Creating the JAR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .61Creating the JAD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .62Running the Package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .63

The J2ME Wireless Toolkit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .64Installing the Toolkit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .64Using the KToolbar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .65

Working with Other Development Environments . . . . . . . . . . . . . . .70JBuilder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .71Sun ONE Studio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .73Other IDEs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .75

Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .76

Contents xiii

Page 15: J2me game programming

Chapter 5 The J2ME API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .77MIDP API Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .77The MIDlet Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .78Using Timers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .86Networking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .90

Working with the Connector . . . . . . . . . . . . . . . . . . . . . . . . . . . .90Working with HttpConnection . . . . . . . . . . . . . . . . . . . . . . . . . . .92

Persistence (RMS) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .95Record Store . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .96Record . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .98Locking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .103Enumerating . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .103Comparing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .104Filtering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .106Listening In . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .108Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .109

User Interface (LCDUI) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .110UI Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .111Display . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .114Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .118Command Listeners . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .120High-Level UI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .123Screens . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .123Forms and Items . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .135Low-Level UI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .150

Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .172

Chapter 6 Device-Specific Libraries . . . . . . . . . . . . . . . . . . . . . . . . . .173Device-Specific Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .173Nokia . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .174

Installing the Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .174The Nokia UI API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .176

Siemens . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .198Installing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .198SMS and Phone API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .199Device Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .200Sounds and Tunes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .201Advanced Graphics and Gaming . . . . . . . . . . . . . . . . . . . . . . . . .202

Contentsxiv

Page 16: J2me game programming

Motorola . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .203Setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .203GameScreen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .204Sprites and PlayField . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .207ImageUtil . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .208PaletteImage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .208

Other Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .210Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .210

Chapter 7 Game Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .211Game Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .211Game Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .211The Application Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .212The Menu . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .214The Game Screen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .216The Game Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .217Adding the Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .221

Double Buffering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .221Drawing the World . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .223Linking to the Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . .225

The Actors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .226Many Roles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .226The Wombat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .230Movement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .231Cars and Trucks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .233Bringing Them to Life . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .235

Input Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .239Collision Detection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .240Game Over . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .242Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .244

Part III Game On . . . . . . . . . . . . . . . . . . . . . . . .245

Chapter 8 The Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .247The State of Play . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .247

Platform Issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .248Game Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .250

Action Games . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .250Puzzle Games . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .251

Contents xv

Page 17: J2me game programming

Adventure Games . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .251Strategy Games . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .251Traditional Games . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .252

Designing Your Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .252The Platform . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .253What Makes a Good Game? . . . . . . . . . . . . . . . . . . . . . . . . . . . .255The Inspiration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .257Visualize Things . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .258The Design Document . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .258

The Development Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .260Creating a Prototype . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .260Developing the Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .261Finish What You . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .262

Your Idea . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .263Game Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .263Game Name . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .264Setting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .264Game Play . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .265Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .265Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .267Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .267

Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .268

Chapter 9 The Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .269Sprite Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .269

Preparing Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .270Loading the Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .273Getting Some Action . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .277

Advanced Sprites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .282States Versus Frames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .282More Action . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .297And Beyond . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .301

Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .302

Chapter 10 The Action . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .303Getting Some Action . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .303Basic Movement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .304

Contentsxvi

Page 18: J2me game programming

Moving at an Angle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .304Cartesian Space . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .305Directional Movement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .305Simulating Floating-Point Math . . . . . . . . . . . . . . . . . . . . . . . . .307A Moving Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .311

Advanced Motion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .317Velocity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .318Acceleration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .318Free Flight . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .319Adding Bounce . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .321

Collision Detection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .325Basic Intersection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .325Intersection through Exclusion . . . . . . . . . . . . . . . . . . . . . . . . . .326Reacting to a Collision . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .328Advanced Collision Detection . . . . . . . . . . . . . . . . . . . . . . . . . . .330A Collision Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .332

Actors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .342The New Actor Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .342Revised Ship Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .345Revised Game Screen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .347Adding Weapon Fire . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .349

The Enemy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .355Computing Distance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .355Targeting an Angle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .356Turning through Spin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .358Aligning Directions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .361Creating an Enemy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .364Creating a Brain . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .365

Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .366

Chapter 11 The World . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .367A New World . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .367

Nature of the Universe . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .367World Coordinates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .368Scrolling the View Port . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .369Panning the View Port . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .370

Contents xvii

Page 19: J2me game programming

Creating a Tile Engine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .373The Tile Map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .374Rendering the Tile Layer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .376Tile Collision Detection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .378Animated Tiles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .379Activator Tiles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .380Non-Collidable Tiles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .383Background Layers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .384Putting It All Together . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .386

Building Worlds . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .388Level Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .388Array Maps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .390Random Level Generator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .390Using a Map Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .407

Advanced Object Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . .416Object Pooling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .417Linked Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .418Mutually Exclusive Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .421Tracking Object Use . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .422The Actor Pool Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .423Adapting the World to Pooling . . . . . . . . . . . . . . . . . . . . . . . . .428

Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .432

Chapter 12 The Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .433The Game Screen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .433Game State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .433

Adding State to GameScreen . . . . . . . . . . . . . . . . . . . . . . . . . . .434The Primary Actor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .437

Ship Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .438Expanding the Enemy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .442

Dealing with Damage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .445Adding Energy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .446The Shield Effect . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .448Energy Bar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .450Dealing with Death . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .452

Saving and Loading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .458Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .464

Contentsxviii

Page 20: J2me game programming

Chapter 13 The Front End . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .465Front End Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .465The Application Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .467

Configuration Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .470Configuring Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .472Saving and Loading Settings . . . . . . . . . . . . . . . . . . . . . . . . . . . .475

The Menus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .479The Splash Screen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .488Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .492

Chapter 14 The Device Ports . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .493Nokia Customization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .493

FullCanvas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .493Vibration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .495Using Reflection and Rotation . . . . . . . . . . . . . . . . . . . . . . . . . .496

Build Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .500Setting Up Ant . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .501Creating a Build File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .502Using Antenna . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .505

Multi-Device Builds . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .512Device-Specific Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .512Managing Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .514The Complete Build . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .515

Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .518

Chapter 15 The Optimizing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .519Speed, Glorious Speed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .519

When to Optimize . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .519Don’t Optimize Blindly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .520Method Profiling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .521Memory Profiling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .523Garbage Monitoring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .525

Optimization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .527High-Level Optimizations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .527Low-Level Optimizations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .529

Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .534

Contents xix