Question

To switch between levels in my game I recreate my GameScene, but but when I switch levels about 3 or 4 times I get an errno 12 out of memory error. I need to be able to somehow delete the whole scene and stop it from running in the background. Is there a way to do this so I don't run out of memory?

EDIT:

This is my method in which I load the new scene.

public void loadGameScene(final Engine mEngine, final SharedPreferences sp, final String map) {
setScene(loadingScene);
ResourcesManager.getInstance().unloadMenuTextures();
mEngine.registerUpdateHandler(new TimerHandler(0.3f,
    new ITimerCallback() {
        public void onTimePassed(final TimerHandler pTimerHandler) {
        ResourcesManager.getInstance().unloadGameTextures();
        mEngine.unregisterUpdateHandler(pTimerHandler);
        ResourcesManager.getInstance().loadGameResources();
        GameScene gameScene = new GameScene();      
        gameScene.setSp(sp);
        gameScene.loadLevel(map);
        gameScene.loadMap(map);
        setScene(gameScene);
        }
    }));
}

I put in the line

ResourcesManager.getInstance().unloadGameTextures()

but the problem persists.

Was it helpful?

Solution

If you are correctly calling unload() on the atlas that you are using when you call ResourcesManager.getInstance().unloadGameTextures(), you shouldn't get an out of memory error.

I don't know how your ResourceManager behaves, but you don't need to recreat all the atlas everytime you change a scene (and by recreating I mean calling new BitmapAtlas() or some variant).

Your ResourceManager should create all the needed objects at the beggining of the game (this includes the atlas). Then when you need regions for a certain scene, you call atlas.load() on the atlas that have those regions and atlas.unload() on other atlas that you don't need.

OTHER TIPS

Before recreating it you have to unload all the resources of Pre-Level, like Texture Atlas, sprites .If you are using Physics then you have to unload bodies which you have created for sprites.So that all objects will cleared which makes your memory efficient.

Hopefully you;re not too far along in your project, because the answer i harder to add retro-actively than as you build. But here it is: whenever you create an object, you should have a way to destroy that object. Then make sure you destroy all of the objects when you stop using them to free up memory.

From what you describe, I would bet what your are doing is creating the same textures over and over again, thus eating up your scare texture memory. Try this instead: Create a separate class to create and manage your textures. When you need a texture for a scene, ask that class for a reference. If the texture does not exist, it will create it. But if you have already created that texture before it will just pass back the reference to the texture you created the first time you created the scene.

If you need to see a working example, check out the code samples from the book "AndEngine for Android Game Development Cookbook" http://www.packtpub.com/support/10411

Search through the chapter examples for the "ResourceManager" class.

Its a good reference for anyone trying to learn Andengine GLES2.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top