In the third part we have created an empty activity. We have set the engine to show "null" as our scene in onCreateScene method. If you check the examples, there is always a real scene created in this method. But we want to have multiple scenes and therefore we need a mechanism how to switch between them.
Also we will need a mechanism to pass important objects to each scene. We can do it when constructing scenes. Or we can use another class called ResourceManager.
Resource manager
This class will be used for our resources like textures, sounds and music. Because all of our scenes will be using it extensively, we will also put some important objects there. Loading the textures etc will be described in the next parts, I will be adding them as they will be needed. For now, create a really simple Resource Manager that looks like this:
package is.kul.squongtutorial.resources;
import is.kul.squongtutorial.GameActivity;
import org.andengine.engine.Engine;
import org.andengine.engine.camera.Camera;
import org.andengine.opengl.vbo.VertexBufferObjectManager;
public class ResourceManager {
private static final ResourceManager INSTANCE = new ResourceManager();
//common objects
public GameActivity activity;
public Engine engine;
public Camera camera;
public VertexBufferObjectManager vbom;
private ResourceManager() {}
public static ResourceManager getInstance() {
return INSTANCE;
}
public void init(GameActivity activity) {
this.activity = activity;
this.engine = activity.getEngine();
this.camera = engine.getCamera();
this.vbom = engine.getVertexBufferObjectManager();
}
}
Update the GameActivity to instantiate the ResourceManager:
@Override
public void onCreateResources(
OnCreateResourcesCallback pOnCreateResourcesCallback)
throws IOException {
ResourceManager.getInstance().init(this);
pOnCreateResourcesCallback.onCreateResourcesFinished();
}
This class follows what we call Singleton pattern. It means there can be only one instance in the whole program. That's why there is the private constructor, nobody else can create a new instance. It is important to have only single resource manager because due to the limitations of the hardware, you really want to keep only one instance of assets in memory.
For now, we will only use it to have a simple reference to activity, engine, camera and vertex buffer object manager.
Vertex Buffer Object is part of OpenGL. It provides methods to render vertex data. AndEngine does great job of hiding this from you. Simply use the VBO Manager all the time, at least for your first game.
Scene is basically a set of entities that are being currently displayed. You typically want to have completely different entities in menu and in the game. And that's why you create menu scene and game scene.
Read more »