Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
AndEngine for Android Game Development Cookbook

You're reading from   AndEngine for Android Game Development Cookbook AndEngine is a simple but powerful 2D game engine that's ideal for developers who want to create mobile games. This cookbook will get you up to speed with the latest features and techniques quickly and practically.

Arrow left icon
Product type Paperback
Published in Jan 2013
Publisher Packt
ISBN-13 9781849518987
Length 380 pages
Edition 1st Edition
Arrow right icon
Authors (3):
Arrow left icon
Brian Boyles Brian Boyles
Author Profile Icon Brian Boyles
Brian Boyles
Nicolas Gramlich Nicolas Gramlich
Author Profile Icon Nicolas Gramlich
Nicolas Gramlich
JAYME SCHROEDER JAYME SCHROEDER
Author Profile Icon JAYME SCHROEDER
JAYME SCHROEDER
Arrow right icon
View More author details
Toc

Table of Contents (18) Chapters Close

AndEngine for Android Game Development Cookbook
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
1. AndEngine Game Structure FREE CHAPTER 2. Working with Entities 3. Designing Your Menu 4. Working with Cameras 5. Scene and Layer Management 6. Applications of Physics 7. Working with Update Handlers 8. Maximizing Performance 9. AndEngine Extensions Overview 10. Getting More From AndEngine Source Code for MagneTank Index

Creating object factories


Object factories are a useful design pattern used in all sorts of areas in programming. In game development specifically, a factory might be used to spawn enemy objects, spawn bullet objects, particle effects, item objects, and much more. In fact, AndEngine even uses the factory pattern when we create sounds, music, textures, and fonts, among other things. In this recipe, we'll find out how we can create an object factory and discuss how we can use them to provide simplicity in object creation within our own projects.

Getting ready

Please refer to the class named ObjectFactory in the code bundle.

How to do it…

In this recipe, we're using the ObjectFactory class as a way for us to easily create and return subtypes of the BaseObject class. However, in a real-world project, the factory would not normally contain inner classes.

  1. Before we create our object factory, we should create our base class as well as at least a couple subtypes extending the base class:

    public static class BaseObject {
        
      /* The mX and mY variables have no real purpose in this recipe, however in
       * a real factory class, member variables might be used to define position,
       * color, scale, and more, of a sprite or other entity.   */
      private int mX;
      private int mY;
        
      // BaseObject constructor, all subtypes should define an mX and mY value on creation
      BaseObject(final int pX, final int pY){
        this.mX = pX;
        this.mY = pY;
      }
    }
  2. Once we've got a base class with any number of subtypes, we can now start to consider implementing the factory design pattern. The ObjectFactory class contains the methods which will handle creating and returning objects of types LargeObject and SmallObject in this case:

    public class ObjectFactory {
      
      // Return a new LargeObject with the defined 'x' and 'y' member variables.
      public static LargeObject createLargeObject(final int pX, final int pY){
        return new LargeObject(pX, pY);
      }
      // Return a new SmallObject with the defined 'x' and 'y' member variables.
      public static SmallObject createSmallObject(final int pX, final int pY){
        return new SmallObject(pX, pY);
      }
    }

How it works…

In the first step of this recipe, we are creating a BaseObject class. This class includes two member variables called mX and mY, which we can imagine would define the position on the device's display if we are dealing with AndEngine entities. Once we've got our base class set up, we can start creating subtypes of the base class. The BaseObject class in this recipe has two inner classes which extend it, one named LargeObject and the other, SmallObject. The object factory's job is to determine which subtype of the base class that we need to create, as well as define the object's properties, or mX and mY member variables in this instance.

In the second step, we are taking a look at the ObjectFactory code. This class should contain any and all variations for object creation relating to the specific object-types that the factory deals with. In this case, the two separate objects simply require an mX and mY variable to be defined. In a real-world situation, we may find it helpful to create a SpriteFactory class. This class might contain a few different methods for creating ordinary sprites, button sprites, or tiled sprites, via SpriteFactory.createSprite(), SpriteFactory.createButtonSprite(), and SpriteFactory.createTiledSprite(). On top of that, each of these methods would probably require parameters that define the position, scale, texture region, color, and more. The most important aspect to this class is that its methods return a new subtype of an object as this is the whole purpose behind the factory class.

You have been reading a chapter from
AndEngine for Android Game Development Cookbook
Published in: Jan 2013
Publisher: Packt
ISBN-13: 9781849518987
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at AU $24.99/month. Cancel anytime