Book Image

AndEngine for Android Game Development Cookbook

By : JAYME SCHROEDER, Brian Boyles
Book Image

AndEngine for Android Game Development Cookbook

By: JAYME SCHROEDER, Brian Boyles

Overview of this book

AndEngine is a broad 2D game engine which allows game developers, both experienced and inexperienced, to develop games for the Android platform with ease. Don't be fooled by the simplicity, though. As easy as it is to “pick up and go,” AndEngine includes enough functionality to bring any type of 2D game world to life.The "AndEngine for Android Game Development Cookbook" contains all of the necessary information and examples in order to build the games as you imagine them. The book's recipes will walk you through the various aspects of game design with AndEngine and provides detailed instructions on how to achieve some of the most desirable effects for your games.The "AndEngine for Android Game Development Cookbook" begins with detailed information on some of the more useful structuring techniques in game design and general aspects of resource management. Continuing on, the book will begin to discuss AndEngine entities, including sprites, text, meshes, and more. Everything from positioning, to modifiers, and even tips on improving entity functionality with raw OpenGL capabilities. From here on, everything from applying physics to your game, working with multi-touch events and gestures, game optimization, and even an overview of the various AndEngine extensions will be covered.The book has a widerange of recipes, from saving and loading game data, applying parallax backgrounds to create a seemingly 3D world, relying on touch events to zoom the game camera, taking screen-shots of the device's screen, and performance optimization using object pools. If physics-based games are more interesting to you, there's also a list of recipes ranging from controlling the world forces and calculating forces applied to bodies, creating destructible objects, and even creating rag-dolls.Pong styled games were fun 35 years ago, but it is time to take your game to the next level with the AndEngine for Android Game Development Cookbook.
Table of Contents (18 chapters)
AndEngine for Android Game Development Cookbook
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Choosing our engine type


Before we start programming our game, it is a good idea to come up with the performance needs of the game. AndEngine includes a few different types of engines we can choose to use, each with their own benefits. The benefits,of course, depend on the type of game we plan to create.

Getting ready

Carry out the Know the life cycle recipe in this chapter to get a basic AndEngine project set up in our IDE, then continue on to the How to do it... section.

How to do it…

In order for us to properly define a specific Engine object for our game to use, we must override the onCreateEngine() method, which is part of AndEngine's startup process. Add the following code to any base AndEngine activity in order to handle the Engine's creation manually:

/* The onCreateEngine method allows us to return a 'customized' Engine object
* to the Activity which for the most part affects the way frame updates are 
* handled. Depending on the Engine object used, the overall feel of the 
* gameplay can alter drastically. 
*/
@Override
public Engine onCreateEngine(EngineOptions pEngineOptions) {
  return super.onCreateEngine(pEngineOptions);
  /* The returned super method above simply calls:
      return new Engine(pEngineOptions);
  */
}

How it works…

The following is an overview of the various Engine objects available in AndEngine, as well as a brief code snippet displaying how to set up each of the Engine objects:

  • Engine: First and foremost, we have the ordinary Engine object. The Engine object is not ideal for most game development as it has absolutely no limitations in regards to frames per second. On two separate devices, it is very likely that you will notice differences in the speed of the game. One way to think of this is if two separate devices are watching a video which was started at the same time, the faster device is likely to finish the video first rather than both finishing at the same time. For this reason, noticeable issues can arise in devices which might not run as fast, especially when physics are a big part of the game. There are no extra steps involved in incorporating this type of engine into our game.

  • FixedStepEngine: The second type of engine we have at our disposal is the FixedStepEngine. This is the ideal engine used in game development as it forces the game loop to update at a constant speed regardless of the device. This is done by updating the game based on the time passed rather than the device's ability to execute code faster. FixedStepEngine requires us to pass the EngineOptions object, as well as an int value, in that order. The int value defines the number of steps per second that the engine will be forced to run at. The following code creates an engine that will run at a constant 60 steps per second:

    @Override
    public Engine onCreateEngine(EngineOptions pEngineOptions) {
      // Create a fixed step engine updating at 60 steps per second
        return new FixedStepEngine(pEngineOptions, 60);
      }
  • LimitedFPSEngine: The LimitedFPSEngine engine allows us to set a limit on the frames per second that the Engine will run at. This will cause the Engine to do some internal calculations, and if the difference between the preferred FPS is greater than the current FPS that the Engine is achieving, the Engine will wait a fraction of a second before proceeding with the next update. LimitedFPSEngine requires two parameters in the constructor, including the EngineOptions object and an int value specifying the maximum frames per second. The following code creates an engine that will run at a maximum of 60 frames per second:

    @Override
    public Engine onCreateEngine(EngineOptions pEngineOptions) {
      // Create a limited FPS engine, which will run at a maximum of 60 FPS
      return new LimitedFPSEngine(pEngineOptions, 60);
    }
  • SingleSceneSplitScreenEngine and DoubleSceneSplitScreenEngine: The SingleSceneSplitScreenEngine engine and DoubleSceneSplitScreenEngine engine allow us to create a game with two separate cameras, either with a single scene, most generally used for single player games, or two scenes for multiplayer games on a single device. These are just examples, however, but these two engine's can have a wide range of uses, including mini-maps, multiple perspectives, menu systems, and much more. See Chapter 4, Creating a Split-screen Game, for more specific details on setting up these types of Engine object.