Creating Mobile Games Using Java phần 1 docx

43 242 0
Creating Mobile Games Using Java phần 1 docx

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Creating Mobile Games Using Java TM ME Platform to Put the Fun into Your Mobile Device and Cell Phone Carol Hamer 8806ch00FMFIANL.qxd 7/17/07 3:38 PM Page i Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Creating Mobile Games: Using Java™ ME Platform to Put the Fun into Your Mobile Device and Cell Phone Copyright © 2007 by Carol Hamer 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-59059-880-1 ISBN-10 (pbk): 1-59059-880-6 Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. Java™ and all Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc., in the US and other countries. Apress, Inc., is not affiliated with Sun Microsystems, Inc., and this book was writ- ten without endorsement from Sun Microsystems, Inc. Lead Editors: Steve Anglin, Jeff Pepper Technical Reviewer: Alain Le Guirec Editorial Board: Steve Anglin, Ewan Buckingham, Gary Cornell, Jonathan Gennick, Jason Gilmore, Jonathan Hassell, Chris Mills, Matthew Moodie, Jeffrey Pepper, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Project Manager: Richard Dal Porto Copy Edit Manager: Nicole Flores Copy Editor: Liz Welch Assistant Production Director: Kari Brooks-Copony Production Editor: Laura Esterman Compositor: Kinetic Publishing Services, LLC Proofreader: Elizabeth Berry Indexer: Carol Burbo Artist: April Milne Cover Designer: Kurt Krames Manufacturing Director: Tom Debolski Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail orders-ny@springer-sbm.com, or visit http://www.springeronline.com. For information on translations, please contact Apress directly at 2560 Ninth Street, Suite 219, Berkeley, CA 94710. Phone 510-549-5930, fax 510-549-5939, e-mail info@apress.com, or visit http://www.apress.com. 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 this work. The source code for this book is available to readers at http://www.apress.com in the Source Code/Download section. 8806ch00FMFIANL.qxd 7/17/07 3:38 PM Page ii Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com To my boys: Nicolas, Léo, and Emmanuel 8806ch00FMFIANL.qxd 7/17/07 3:38 PM Page iii Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Contents at a Glance About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xi Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii ■CHAPTER 1 Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 ■CHAPTER 2 Using MIDlets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 ■CHAPTER 3 Using the MIDP 2 Games API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 ■CHAPTER 4 Using Threads and Media . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 ■CHAPTER 5 Storing and Retrieving Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131 ■CHAPTER 6 Using Network Communications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193 ■CHAPTER 7 Advanced Messaging and Data Access . . . . . . . . . . . . . . . . . . . . . . . . 263 ■CHAPTER 8 Securing Your Applications. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305 ■CHAPTER 9 The Mobile 3D Graphics API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317 ■CHAPTER 10 Adding a Professional Look and Feel . . . . . . . . . . . . . . . . . . . . . . . . . . 351 ■INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409 v 8806ch00FMFIANL.qxd 7/17/07 3:38 PM Page v Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Contents About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xi Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii ■CHAPTER 1 Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Understanding Java ME . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 How the CLDC Differs from the Rest of the Java Universe. . . . . . . . . 2 The Three Versions of MIDP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Downloading and Installing the Toolkit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Building an Application for MIDP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Creating the “Hello, World” Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Using KToolbar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Running Your Game on an Actual Cell Phone . . . . . . . . . . . . . . . . . . . . . . . . 14 Using WAP. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Preparing the WML File. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 Configuring the Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 Accessing the WML File and Downloading Applications. . . . . . . . . . 18 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 ■CHAPTER 2 Using MIDlets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 Using the MIDlet Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 Understanding the MIDlet Lifecycle . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 Using the Displayable and Display Classes. . . . . . . . . . . . . . . . . . . . . 26 Using Buttons and Menus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 Using the Form and Item Classes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 Using the Graphics and Canvas Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 Using the java.util Package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 ■CHAPTER 3 Using the MIDP 2 Games API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 Starting with the MIDlet Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 Using the Thread Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 vii 8806ch00FMFIANL.qxd 7/17/07 3:38 PM Page vii Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Using the GameCanvas Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 How GameCanvas Differs from Canvas . . . . . . . . . . . . . . . . . . . . . . . . 62 Using the Graphics Class with a GameCanvas . . . . . . . . . . . . . . . . . . 69 Using the LayerManager Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 Using the Sprite Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 Using the TiledLayer Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 ■CHAPTER 4 Using Threads and Media . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 Using Threads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 Differences Between CLDC Threads and Threads in Standard Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 Strategies for Deciding When to Use a New Thread . . . . . . . . . . . . 104 Avoiding Race Conditions and Deadlock . . . . . . . . . . . . . . . . . . . . . . 115 Using Media . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118 Playing Simple Tones . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118 Playing Tones with a Player . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 Using Audio Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 ■CHAPTER 5 Storing and Retrieving Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131 Saving Simple Data. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131 Serializing More Complex Data Using Streams . . . . . . . . . . . . . . . . . . . . . 135 Using Data Types and Byte Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136 Applying Data Storage to a Dungeon Game . . . . . . . . . . . . . . . . . . . . . . . . 143 Creating the Complete Example Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192 ■CHAPTER 6 Using Network Communications. . . . . . . . . . . . . . . . . . . . . . . . . . 193 Choosing a Protocol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193 Using the Micro Edition IO API. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194 Using HTTP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195 The Dungeon Example: Downloading the Next Board . . . . . . . . . . . 197 Writing the Client Code for the Dungeon Example. . . . . . . . . . . . . . 198 Writing the Server Code for the Dungeon Example . . . . . . . . . . . . . 206 Using SMS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209 Using the Push Registry . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210 ■CONTENTSviii 8806ch00FMFIANL.qxd 7/17/07 3:38 PM Page viii Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Creating a Multiplayer Game Example: Checkers . . . . . . . . . . . . . . . . . . . 212 Writing the Communications Code for the Checkers Example. . . . 212 Writing the Game Logic for the Checkers Example . . . . . . . . . . . . . 234 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262 ■CHAPTER 7 Advanced Messaging and Data Access . . . . . . . . . . . . . . . . . . 263 Using Bluetooth . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263 Using the Personal Information Management API . . . . . . . . . . . . . . . . . . . 285 Using the File Connection API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297 More Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303 ■CHAPTER 8 Securing Your Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305 Understanding Protection Domains and Permissions . . . . . . . . . . . . . . . . 305 Requesting Permissions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307 Using Digital Certificates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308 Setting Up Secure Connections. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311 Using HTTPS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311 Using Other Secure Connections. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316 ■CHAPTER 9 The Mobile 3D Graphics API. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317 Vertex Buffers and Coordinates. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317 Defining the Polygon . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317 Lights, Camera, Render! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322 Textures and Lighting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325 The M3G File Format. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332 The M3G File Structure. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332 Creating an M3G File. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333 Nodes and Rendering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335 Rendering Modes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336 A Tour of the World Node . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336 Further Tools and Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345 Animations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345 Collisions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348 Optimization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349 ■CONTENTS ix 8806ch00FMFIANL.qxd 7/17/07 3:38 PM Page ix Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com ■CHAPTER 10 Adding a Professional Look and Feel . . . . . . . . . . . . . . . . . . . . . 351 Customizing for Multiple Target Platforms . . . . . . . . . . . . . . . . . . . . . . . . . 352 Organizing Custom Resources. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352 Modifying Image Colors and Transparency . . . . . . . . . . . . . . . . . . . . 363 Creating a GUI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367 Starting Off on the Right Foot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367 Creating Custom Menus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 380 Implementing Softkeys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 396 Applying Custom Resources to the Game . . . . . . . . . . . . . . . . . . . . . . . . . . 398 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 407 ■INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409 ■CONTENTSx 8806ch00FMFIANL.qxd 7/17/07 3:38 PM Page x Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com About the Author ■CAROL HAMER has worked for more than three years as a software engineer for In-Fusio Mobile Game Connections. She is currently the director of client development for Business Anywhere. xi 8806ch00FMFIANL.qxd 7/17/07 3:38 PM Page xi Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com [...]... Protocol inet addr: 81. 49 .19 5.43 P-t-P :19 3.253 .16 0.3 Mask:255.255.255.255 UP POINTOPOINT RUNNING NOARP MULTICAST MTU :14 92 Metric :1 RX packets :11 08 errors:0 dropped:0 overruns:0 frame:0 CHAPTER 1 ■ GETTING STARTED Simpo PDF TX packets :10 97 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:3 Merge and Split Unregistered Version - http://www.simpopdf.com RX bytes:798 514 (779.7 Kb) TX bytes:98348... KToolbar will use to construct the JAD and manifest files as well as the directories for the project (see Figures 1- 3 and 1- 4) 11 12 CHAPTER 1 ■ GETTING STARTED Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Figure 1- 3 Creating a new project in KToolbar Figure 1- 4 Setting up a new project in KToolbar Once you’ve filled in the project settings, KToolbar creates a set of directories... as a Java ME developer The only java. * packages that you have available to you are java. lang.*, java. util.*, and java. io.* That means you have to do without java. lang reflect.*, java. math.*, java. security.*, and many others Many of the missing packages have been replaced by MIDP packages that are more appropriate for small devices, as you’ll see throughout this book Although the three remaining java. *... CLDC, CDC, and MIDP are all defined as JSRs through the Java Community Process How the CLDC Differs from the Rest of the Java Universe If you’re coming to Java ME from a background of programming for Java Standard Edition (Java SE) or Java Enterprise Edition (Java EE), things should look pretty familiar, but just a little bit different It’s like Java in a parallel universe where everything has been streamlined... Standardization (ISO) Latin 1 range of characters from the Unicode standard, version 3.0 For more information about character encoding in Java, see http:/ /java. sun.com/ javase/6/docs/technotes/guides/intl/encoding.doc.html Also, the initial version of CLDC (1. 0) did not include floating-point numbers as simple data types (double and float) These were added in the newer version, CLDC 1. 1 Differences in the... because of the screen limitations of the target device Try to keep it small and simple Listing 1- 4 shows a minimal example suitable for a download page The file is called hello.wml Listing 1- 4 hello.wml Hello world! Hello World App... phones for fun and entertainment, and you can tap into that market with Java Micro Edition games If you’re already a software engineer with lots of experience in Java Standard Edition and/or Java Enterprise Edition, learning to use Java Micro Edition isn’t too hard And if you have only a basic knowledge of programming in Java, Java ME is a fine place to start The main differences you encounter in your... simple graphics, as well as describe the MIDlet lifecycle and highlight some differences between Java Micro Edition (Java ME) programming and programming for the other two main Java editions (Standard and Enterprise) Figure 2 -1 A MIDlet running on the WTK emulator with KToolbar 21 22 CHAPTER 2 ■ USING MIDLETS Using the MIDlet Class Simpo PDF Merge and Split developing a MIDlet will be to http://www.simpopdf.com... CHAPTER 1 ■ GETTING STARTED Simpo calls if the user presses a key while the application is running The HelloCanvas class repaints the screen with or without the Hello World! message when the toggleHello() method is called PDF Merge and Split Unregistered Version - http://www.simpopdf.com Listing 1- 2 shows the code for Hello .java Listing 1- 2 Hello .java package net.frog_parrot.hello; import javax.microedition.midlet.*;... destroyApp(false); notifyDestroyed(); } catch (MIDletStateChangeException ex) { } } } } CHAPTER 1 ■ GETTING STARTED Listing 1- 3 shows the code for HelloCanvas .java Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Listing 1- 3 HelloCanvas .java package net.frog_parrot.hello; import javax.microedition.lcdui.*; /** * This class represents the region of the screen that has been allotted . a Java ME developer. The only java. * packages that you have available to you are java. lang.*, java. util.*, and java. io.*. That means you have to do without java. lang. reflect.*, java. math.*, java. security.*,. Version - http://www.simpopdf.com Creating Mobile Games Using Java TM ME Platform to Put the Fun into Your Mobile Device and Cell Phone Carol Hamer 8806ch00FMFIANL.qxd 7 /17 /07 3:38 PM Page i Simpo PDF. copyright owner and the publisher. ISBN -13 (pbk): 978 -1- 59059-880 -1 ISBN -10 (pbk): 1- 59059-880-6 Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Trademarked names may appear in

Ngày đăng: 12/08/2014, 11:20

Tài liệu cùng người dùng

Tài liệu liên quan