Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Learning AndEngine
Learning AndEngine

Learning AndEngine: Design and create Android games with the simple but powerful tool AndEngine

By Martin Varga
€25.99 €17.99
Book Sep 2014 286 pages 1st Edition
eBook
€25.99 €17.99
Print
€32.99
Subscription
€14.99 Monthly
eBook
€25.99 €17.99
Print
€32.99
Subscription
€14.99 Monthly

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Buy Now

Product Details


Publication date : Sep 11, 2014
Length 286 pages
Edition : 1st Edition
Language : English
ISBN-13 : 9781783985968
Category :
Table of content icon View table of contents Preview book icon Preview Book

Learning AndEngine

Chapter 1. Setting Up an AndEngine Project

In this chapter, we are going to develop an empty AndEngine application that will serve as a base for a game. First, we will discuss prerequisites and download and install the required software. You will also learn where to get the latest and stable AndEngine libraries from. Lastly, we are going to create and implement a simple Android application that uses AndEngine libraries and then run it.

It doesn't matter on which platform you develop the project as long as you can install the Java Software Development Kit (SDK) and Android SDK there. However, AndEngine is not a multiplatform framework because applications created with AndEngine can run only on an Android device or inside an Android emulator.

Prerequisites


Android applications are simply Java applications running inside an Android virtual machine called Dalvik. You will encounter this name when compiling and running the application. The final compiled code is not fully compatible with the Oracle Java Virtual Machine, but for the purpose of this book, you are only expected to know basic Java programming.

You will need the following software and hardware:

  • Windows XP, Vista, 7, or 8; Linux (Ubuntu is recommended); or Mac OS 10.5.8 or a later operating system

  • Java SDK

  • Android SDK with the ADT bundle

  • An Android device

You should already know how to install the Java SDK (JDK) and keep it up to date. The Android SDK requires at least JDK 6. You can use higher versions if available. Always use the latest update for security and compatibility reasons.

Note

It is important to use JDK and not just the Java Runtime Environment (JRE) for development. It is also a requirement of Android SDK. Make sure the JAVA_HOME environment variable is set to the correct folder.

Android SDK contains the Eclipse Integrated Development Environment (IDE) with preinstalled plugins and Android platform tools. Using the latest version of Android SDK is recommended.

Tip

If you have used Eclipse before, you can use your own existing Eclipse installation as well, but then you are required to install the plugins manually on your own.

We are going to download and install Android SDK with the Android Development Tools (ADT) bundle in the next section. ADT allows you to install the application to your device and also connect to it in order to get important information such as the LogCat console output (text output from installed applications that is not visible to users) and other interesting statistics about running apps.

Your device should be running at least Android 2.2, but using a more recent version is recommended. If you don't own an Android device, you can use an Android emulator for development. However, consider getting a physical device because the behavior of the emulator is different from that of a real phone or tablet. The game might run slower or have problems. Emulators are known to have issues especially with hardware-accelerated graphics. Nothing can replace testing on a real device, and in fact for serious game development, it's a necessity to test on multiple devices.

Downloading and installing the required software


We are going to download and install Android SDK with the ADT bundle first. JDK 6 or later should already be installed and configured on your development machine.

Downloading the Android SDK

Go to http://developer.android.com/sdk/. If you are on Windows, click on the Download Eclipse ADT with the Android SDK for Windows button. If you are using a different operating system, the page should autodetect it and offer you the link for your OS. In case it doesn't detect it correctly, there is a link called VIEW ALL DOWNLOADS AND SIZES that will expand a list of available platforms. Choose the right platform for you. Be careful when choosing 32-bit or 64-bit software. If you are running a 32-bit Java on a 64-bit system, choose the 32-bit software. Accept the license agreement and save the file to your hard drive. The examples in this chapter are from the 32-bit version for Windows.

Note

There is another IDE available, Android Studio. However, it is in the early access stage. It is not recommended to use this IDE.

Installing the Android SDK

The downloaded file will be a ZIP archive called something like adt-bundle-windows-x86-20131030.zip. The filename indicates the selected platform and the date of release. To install the software, simply unpack the archive to any folder.

Tip

If you have decided to use an existing Eclipse IDE, expand the GET THE SDK FOR AN EXISTING IDE link and click on the Download the stand-alone Android SDK tools for Windows button. Or, download the SDK tools for appropriate platforms from the VIEW ALL DOWNLOADS AND SIZES link. After that, follow the instructions at http://developer.android.com/sdk/installing/.

You should find the following in the folder where you have unpacked the archive:

  • The eclipse folder

  • The sdk folder

  • The SDK Manager.exe file

First, run the SDK Manager.exe file. It might take a while to start, and it doesn't show any splash screen. You will be presented with a window similar to the one in the following screenshot:

Check for any packages that have updates available. Note that you only need the latest SDK, which at the time this book was written was SDK 19 for Android 4.4. The latest version is always bundled with the Android SDK. You should also check the Google USB Driver option.

Note

Every SDK version allows you to write applications that can be deployed to any Android version. If you use a function from an SDK version that is not available in an older Android SDK, the method will not be executed and you will get a warning or an exception. Be careful with that because the code will compile and deploy!

Optionally, you can install the Android emulator system images for older Android versions. This is useful for testing. However, AndEngine can't be deployed to an Android emulator with Android versions 2.x. For now, do not install anything else but the updates and the USB driver. Install the selected packages and close the SDK manager. The installation might take a while, because the packages are downloaded from the Internet.

Configuring the Eclipse IDE

Now, run the Eclipse IDE. From the eclipse folder, run the eclipse.exe file. After the splash, the first thing you will see is the workspace selection window. This is shown in the following screenshot:

If you have never worked with Eclipse, note that a workspace is both a virtual space on your screen (the layout) and a directory where your projects and some metadata that Eclipse keeps about your projects are stored. Choose any directory you want, but make sure it is one with easy access. You can have multiple workspaces. If you check the Use this as the default and do not ask again checkbox, you will be working with this workspace by default and this dialog box won't appear again at the start.

Tip

The workspace selection dialog box is always available by navigating to File | Switch Workspace menu in Eclipse.

After the start, close the Welcome screen and you will be presented with the Eclipse default layout. Parts of the layout can be dragged and dropped to different locations. Also, you can have different perspectives (presets of the opened windows and tabs) for each workspace. We will be using the Java perspective for most of the time but we will also use Dalvik Debug Monitor Server (DDMS) and the Debug perspective for debugging and tuning our application. The perspectives are available by navigating to Window | Open Perspective.

Stay in the Java perspective, but go to Window | Show View | Other… and search for LogCat, as shown in the following screenshot:

LogCat is a log console. The Android system has a logging system that allows developers to print categorized text messages to LogCat from their apps. This is very useful for game developers because the text output to the screen can be very limited. The ADT takes care of getting the LogCat output from the connected Android device.

The Eclipse IDE can be a bit difficult to grasp from the start. However, as a Java developer, you have probably used it before or have used a similar one. Most of the IDEs nowadays are very similar. Try to familiarize yourself with Eclipse before proceeding to the next step.

Getting the AndEngine libraries


AndEngine is an open source game engine and the sources are licensed under Apache License Version 2.0. This not only allows us to use AndEngine in both free and commercial games, but it also gives us a chance to inspect or alter the source code. The old way of adding AndEngine to an Android application was to add compiled Java ARchive (JAR) files to your app. This is no longer necessary as you can add AndEngine as a library project.

The author of AndEngine, Nicolas Gramlich, decided to store the whole engine inside GitHub. Git is a version control system (VCS) for source codes and GitHub is a hosting website for Git repositories. A VCS allows you to not only store your sources, but also to store every version of each file. This is of course useful when you need to see the history of your changes or collaborate with more people.

A repository is a logical unit. As a rule of thumb, one repository contains one project. A repository has its address and history separated from other repositories. A repository in GitHub can be forked (creates your own copy with your own history), cloned (creates a version on your machine linked with the repository), or zipped and downloaded locally.

Tip

For more general information about AndEngine, visit the official website at www.andengine.org.

Selecting the correct branch

Android uses OpenGL ES to display accelerated graphics. OpenGL ES is a free API for full-function 2D and 3D graphics on embedded systems. There are several versions of OpenGL ES, namely versions 1.x, 2.x, and recently 3.x.

A repository can also contain multiple branches. Think of a branch as an alternative version of the project. AndEngine is split into three branches. The GLES1 branch uses OpenGL ES 1.0 and it is the oldest. I don't recommend using it. GLES2 is newer and GLES2-AnchorCenter is the newest. They both use OpenGL ES 2. Use the GLES2-AnchorCenter branch if you haven't used AndEngine before. The GLES2-AnchorCenter branch's biggest difference from the GLES2 branch is the fact that point [0, 0] is now in the lower-left corner of the screen. This is the same as in OpenGL and that's why it's recommended.

For the purpose of this book, we are going to exclusively use the GLES2-AnchorCenter branch.

AndEngine repositories

AndEngine is split into a main project simply called AndEngine and many extensions called, for example, AndEngineMultiplayerExtension, which are dependent on the main project. Each project resides in its own repository.

For our game, we need the main AndEngine project and a 2D physics extension called AndEnginePhysicsBox2DExtension. This extension adds a very popular 2D physics simulation framework called Box2D to AndEngine. A lot of popular games use this framework.

There is also a repository called AndEngineExamples. It is not an extension but an application that includes all of the other AndEngine libraries and extensions and illustrates their use.

Tip

If you download all the extensions and the AndEngineExamples project, you can build the AndEngineExamples app and run it on your Android device. An older version is also available online on the Google Play store at https://play.google.com/store/apps/details?id=org.anddev.andengine.examples.

Downloading the sources

You can download the sources from Nicolas Gramlich's original repository at https://github.com/nicolasgramlich/.

However, there is a danger that the codes will be changed any time in the future. For that reason, I recommend to download the sources from my forked repository, which is guaranteed to work with the sources in this book.

The easiest way is to download the complete repository as a ZIP archive, which can be done with the help of the following steps:

  1. Browse to https://github.com/sm4/, switch to the Repositories tab, and select AndEngine. Notice the drop-down menu titled branch. Make sure GLES2-AnchorCenter is selected. Alternatively, you can browse directly to the correct branch by going to https://github.com/sm4/AndEngine/tree/GLES2-AnchorCenter.

  2. The following screenshot is what you should see now:

  3. Click on the Download ZIP button. A file named AndEngine-GLES2-AnchorCenter.zip will be downloaded.

  4. Unpack it in the workspace directory you selected when configuring the Eclipse IDE. Note that the directory is called AndEngine-GLES2-AnchorCenter. It contains the name of the project and the branch name as well. Rename the directory to AndEngine.

  5. Repeat the process for the AndEnginePhysicsBox2DExtension repository. It can be downloaded from https://github.com/sm4/AndEnginePhysicsBox2DExtension/tree/GLES2-AnchorCenter.

  6. Don't forget to rename the directory to AndEnginePhysicsBox2DExtension only. You should have two folders in your workspace directory: AndEngine and AndEnginePhysicsBox2DExtension.

Note

If you decide to use a different branch, remember that extensions used must be from the same branch as the main project.

Adding AndEngine to the Eclipse IDE

In Eclipse, navigate to File | Import and choose Existing Projects into Workspace (under the General folder). Browse to your workspace directory. You should see two projects. Check them both and import them by clicking on the Finish button. This is shown in the following screenshot:

If you have installed the ADT bundle with Android SDK 19, you should see no errors. In case there are errors, try cleaning and building all projects again. Simply click on the Project drop-down menu in Eclipse's main menu and select the Clean… option while having the Build Automatically option checked.

It is possible that when you were installing the ADT bundle, it contained a different SDK version from 19, or maybe you are, for some other reason, using a different version. In that case, you will see messages like the following in the console:

[2014-01-14 15:36:11 - AndEngine] Unable to resolve target 'android-19'

Right-click on the project, select the Properties option, and then choose the Android option. Make sure that the Android SDK version of your choice (or simply the latest) is selected. This is shown in the following screenshot. You will probably have to clean the project to make the error disappear. We have to do this because the project was saved with SDK 19 selected. Do it for both projects.

Left arrow icon Right arrow icon

Key benefits

Description

AndEngine is a very popular open source OpenGL (open graphics library) Android game engine, used to create mobile games quickly while maintaining the ability to fully customize them. This book will guide you through the whole development process of creating a mobile game for the Android platform using one of the most popular and easy-to-use game engines available today. Beginning with the very basics, you will learn how to install AndEngine, gather graphics, add sound and music assets, and design game rules. You will first design an example game and enhance it by adding various features over the course of the book. Each chapter adds more colors, enhances the game, and takes it to the next level. You will also learn how to work with Box2D, a popular 2D physics engine that forms an integral part of some of the most successful mobile games. By the end of the book, you will be able to create a complete, interactive, and fully featured mobile game for Android and publish it to Google Play.

What you will learn

Understand the basics of a game engine Install all the required software and download and set up AndEngine along with its physics extension Design basic game rules and gather game assets Create a basic runnable application using AndEngine Display text and graphics in the game Implement basic interactions between the player and the game, making use of the mobile device sensors Simulate realworld physics ingame Finish a polished 2D physicsbased

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Buy Now

Product Details


Publication date : Sep 11, 2014
Length 286 pages
Edition : 1st Edition
Language : English
ISBN-13 : 9781783985968
Category :

Table of Contents

19 Chapters
Learning AndEngine Chevron down icon Chevron up icon
Credits Chevron down icon Chevron up icon
About the Author Chevron down icon Chevron up icon
Acknowledgments Chevron down icon Chevron up icon
About the Reviewers Chevron down icon Chevron up icon
www.PacktPub.com Chevron down icon Chevron up icon
Preface Chevron down icon Chevron up icon
1. Setting Up an AndEngine Project Chevron down icon Chevron up icon
2. Game Concept and Assets Chevron down icon Chevron up icon
3. From Assets to Entities Chevron down icon Chevron up icon
4. HUD and Text Display Chevron down icon Chevron up icon
5. Basic Interactions Chevron down icon Chevron up icon
6. Physics Chevron down icon Chevron up icon
7. Detecting Collisions and Reacting to Events Chevron down icon Chevron up icon
8. Advanced Physics Chevron down icon Chevron up icon
9. Adding a Menu and Splash Scene Chevron down icon Chevron up icon
10. Polishing the Game Chevron down icon Chevron up icon
11. Testing, Publishing, and What's Next Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon

Customer reviews

Filter icon Filter
Top Reviews
Rating distribution
Empty star icon Empty star icon Empty star icon Empty star icon Empty star icon 0
(0 Ratings)
5 star 0%
4 star 0%
3 star 0%
2 star 0%
1 star 0%

Filter reviews by


No reviews found
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.