Search code examples
javaandroidvariablesandenginescene

AndEngine - Scene variables won't reset after changing the scene


Here is the scenario:

i have following Scenes:

  1. SplashScene
  2. LoadingScene
  3. GameModeScene
  4. PlayModeScene
  5. GameScene

I have a SceneController class which manages navigation between scenes.
In SceneController i have instances to all the Scenes.

I'm initializing all the scene instances and doing loading of all scene resources in LoadingScene.

When i change scene from GameModeScene to PlayModeScene - Works Perfectly fine.
But, When i try to change the scene again to GameModeScene the boolean and other variables initialized during 1st run of GameModeScene remains as it is and won't reset.

Note : Variables are private to each class and are non-static

I tried pGameModeSceneto.reset(); but it won't work.
Anyy suggestions?

Here is how i'm initializing scene insatnces


SceneController.loadSceneResources()

public void loadSceneResources(SceneType mType){
        switch(mType){
        case SplashScene:
            this.mSplashScene = new SplashScene(this.mActivity);
            break;


        case LoadingScene:
            mLoadingScene = new LoadingScene(mActivity);
            break;

        case GameModeScene:
            mGameModeScene=new GameModeScene(this.mActivity);
            break;

        case PlayModeScene:
            mPlayModeScene=new PlayModeScene(this.mActivity);
            break;

        case GameScene:
            break;

            default:
        }
    }

Constructor of each class is loading the resources.


Here is how m getiing current Scene Instance

SceneController.getScene() public Scene getScene(SceneType mType){

        switch(mType){
        case SplashScene:
            this.mCurrentScene = mSplashScene;
            break;


        case LoadingScene:
            this.mCurrentScene = mLoadingScene;
            break;

        case GameModeScene:
            this.mCurrentScene = mGameModeScene;
            break;

        case PlayModeScene:
            this.mCurrentScene = mPlayModeScene;
            break;

            default:
        }

Solution

  • In the org.andengine.entity.scene.Scene class, there's a reset() method, which is what I assume you're referring to when you say

    I tried pGameModeSceneto.reset(); but it won't work.

    All that method does is the following:

    @Override
    public void reset() {
        super.reset();
    
        this.clearChildScene();
    }
    

    Your scenes (i assume) are extending Scene. So when you call reset() on them, all you're doing is initializing the parent-level information and up-the-tree from there via the super.reset() call. (Scene extends Entity.) None of the private fields that you create and set in your scene would be affected by this. To get the behavior you want, override the reset() method in your individual scene classes. Make sure to call super.reset() at the start of them and add initializing information, i.e., whatever your "empty" scene should look like. Something like this:

    @Override
    public void reset() {
        super.reset();
        myLocalInt = 0; 
        myLocalFancyPantsObject = null;
    }