Book Image

LibGDX Game Development By Example

By : James Cook
Book Image

LibGDX Game Development By Example

By: James Cook

Overview of this book

Table of Contents (18 chapters)
LibGDX Game Development By Example
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Hey, look at all these acorns!


Now we have a handle on what an object pool is, why we need one, and how we can use LibGDX to look after one for us. Next, we need to look at how we can use one in Nutty Birds.

I am going to take a liberty with the gameplay and make a fundamental change to the way the game is played. We will design the game in such a way that we can only have three acorns on the screen at on time. Whenever you fire another while three are already in play, the oldest will be removed from the game. This may sound rather crude, but our aim here is to try out an object pool. Once we are done, you can try and shape the game the way you wish to.

Pooling the acorns

Our first task is to move our acorn from a Sprite instance to a class in its own right. This class will implement the Pool.Poolable interface.

The following is the code listing for our new Acorn class:

public class Acorn implements Pool.Poolable {
  private final Sprite sprite;
  public Acorn(AssetManager assetManager) {
  ...