CuuDuongThanCong.com www.it-ebooks.info AndEngine for Android Game Development Cookbook Over 70 highly effective recipes with real-world examples to get to grips with the powerful capabilities of AndEngine and GLES Jayme Schroeder Brian Broyles BIRMINGHAM - MUMBAI CuuDuongThanCong.com www.it-ebooks.info AndEngine for Android Game Development Cookbook Copyright © 2013 Packt Publishing All rights reserved No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews Every effort has been made in the preparation of this book to ensure the accuracy of the information presented However, the information contained in this book is sold without warranty, either express or implied Neither the authors, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals However, Packt Publishing cannot guarantee the accuracy of this information First published: January 2013 Production Reference: 1070113 Published by Packt Publishing Ltd Livery Place 35 Livery Street Birmingham B3 2PB, UK ISBN 978-1-84951-898-7 www.packtpub.com Cover Image by Jayme Schroeder (jayme.schroeder@gmail.com) CuuDuongThanCong.com www.it-ebooks.info Credits Authors Project Coordinator Jayme Schroeder Priya Sharma Brian Broyles Proofreader Kevin McGowan Reviewers Mateusz Mysliwiec Sergio Viudes Carbonell Jafar Abdulrasoul [Jimmar] Acquisition Editor Kartikey Pandey Lead Technical Editor Sweny M Sukumaran Indexer Rekha Nair Graphics Aditi Gajjar Production Coordinator Shantanu Zagade Cover Work Technical Editors Shantanu Zagade Sharvari Baet Dominic Pereira CuuDuongThanCong.com www.it-ebooks.info About the Authors Jayme Schroeder was introduced to computers at a very young age By 11, he had started creating modifications and level packs for his favorite game By age 16, he had found his true passion in game development and network programming in C++ and OpenGL In early 2011, Jayme had received an Android smartphone and immediately fell in love with the development experience Since then, he has been researching and developing for the Android platform on a daily basis There are many people I would like to thank for the opportunity to write this book and also thank those who helped me out every step of the way First and foremost, I would like to thank Packt Publishing for the acceptance of AndEngine for Android Game Development Cookbook and Amber D'souza for first approaching me with the opportunity to write this book I would also like to thank Kartikey Pandey, Michelle Quadros, Sweny Sukumaran, Priya Sharma from Packt Publishing who all played a large part in making the writing process much easier than anticipated and comfortable for me I would also like to thank Dominic Pereira and Sharvari Baet for the effort they've put in throughout the production stage and the suggestions they've made I would like to thank Nicolas Gramlich for creating AndEngine Not only has he created an amazing engine for novice and advanced developers alike, but he's created a great community for Android developers to both learn and strengthen their development skills relating to game development I would like to also thank my co-author, Brian Broyles, for his contributions and commitment to the book It's been an honor to share the experience with him Finally, I would like to thank all of my family and friends who showed their support and provided feedback during this experience More specifically, I would like to thank Kent and Judy Schroeder; my parents, Shannon, Hollie, Taylor, and Brittanie; my brothers and sisters and my girlfriend, Krystal Guevremont Of my friends, I would like to specifically thank Leo Wandersleb and Jordi Puigdellívol, with whom I've spent many days discussing the finer details of AndEngine which greatly helped to improve my knowledge CuuDuongThanCong.com www.it-ebooks.info Brian Broyles is a freelance programmer and 2D/3D graphic designer with over 12 years of experience Before entering the mobile development industry in 2010 as the lead programmer of IFL Game Studio, he designed advanced artificial intelligence systems and graphical effects for interactive PC applications In addition to his vast programming and design experience, he is also a commercial pilot, instrument flight instructor, and advanced instrument ground instructor I'd like to thank my amazing, beautiful wife, Bethany, for her support and enthusiasm as well as my family for encouraging me in all of my endeavors CuuDuongThanCong.com www.it-ebooks.info About the Reviewers Mateusz Mysliwiec was born 1993 in Tarnow, Poland, graduating from high school in 2012 He currently lives in England During his last year of high school, he decided that he would like to study Software Engineering or a different subject connected with math, programming, and engineering He is also an independent game developer In his free time, he develops professional mobile games focusing especially on the Android platform His goal is to permanently impact the global mobile gaming industry in the near future He is active in a variety of projects, including open source His passions away from game development are sports and recreation such as football, skydiving, and jogging His family and friends are the important aspects of his life Sergio Viudes is a 30 years old software developer from Elche (Spain) He works developing commercial web apps, and develops video games for Android in his free time He likes to play video games since childhood He started playing with his brother's Spectrum when he was years old When he bought his first PC (well, his parents did), he was 14 years old, and started learning computer programming, computer drawing, and music composing (using the famous "FastTracker 2") When he finished high school, he studied Computer Science at the University of Alicante His interest in mobile devices started with his first smart phone, ten years ago (2002), when he bought the first Symbian device from Nokia, the Nokia 7650 He really liked the idea that he could develop software that could run everywhere So, along with his studies and his job, Sergio started creating simple mobile apps for his phone About two years ago he decided to create his first video game for mobile devices He really enjoys developing for mobile devices, he likes to compose music, he likes to draw, and, of course, he likes to play video games So he decided to put all his hobbies together and develop his first video game for his favorite mobile platform—Android So far Sergio has released games and he continues developing apps and games for Android as a hobby He wishes that someday it will be his job, not just a hobby CuuDuongThanCong.com www.it-ebooks.info Jafar Abdulrasoul—a graduate from the Kuwait University—is a Computer Engineer and an Android game enthusiast who wrote a couple of excellent tutorials on his blog about creating games using AndEngine He is known online by the name Jimmar My gratitude goes to my mother who tries to support me in everything I do, so thank you mama! CuuDuongThanCong.com www.it-ebooks.info www.PacktPub.com Support files, eBooks, discount offers and more You might want to visit www.PacktPub.com for support files and downloads related to your book Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy Get in touch with us at service@packtpub.com for more details At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks TM http://PacktLib.PacktPub.com Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library Here, you can access, read and search across Packt's entire library of books. Why Subscribe? ff ff ff Fully searchable across every book published by Packt Copy and paste, print and bookmark content On demand and accessible via web browser Free Access for Packt account holders If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view nine entirely free books Simply use your login credentials for immediate access CuuDuongThanCong.com www.it-ebooks.info Table of Contents Preface 1 Chapter 1: AndEngine Game Structure Introduction 7 Know the life cycle Choosing our engine type 14 Selecting a resolution policy 17 Creating object factories 18 Creating the game manager 20 Introducing sounds and music 23 Working with different types of textures 27 Applying texture options 34 Using AndEngine font resources 39 Creating the resource manager 42 Saving and loading game data 45 Chapter 2: Working with Entities 51 Introduction 51 Understanding AndEngine entities 52 Applying primitives to a layer 58 Bringing a scene to life with sprites 62 Applying text to a layer 69 Using relative rotation 74 Overriding the onManagedUpdate method 77 Using modifiers and entity modifiers 81 Working with particle systems 93 CuuDuongThanCong.com www.it-ebooks.info ... Street Birmingham B3 2PB, UK ISBN 97 8-1 -8 495 1-8 9 8-7 www.packtpub.com Cover Image by Jayme Schroeder (jayme.schroeder@gmail.com) CuuDuongThanCong.com www.it-ebooks.info Credits Authors Project... recipes with real-world examples to get to grips with the powerful capabilities of AndEngine and GLES Jayme Schroeder Brian Broyles BIRMINGHAM - MUMBAI CuuDuongThanCong.com www.it-ebooks.info AndEngine... types Creating category-filtered bodies Creating multiple-fixture bodies Creating unique bodies by specifying vertices Using forces, velocities, and torque Applying anti-gravity to a specific