Show different scenes for different levels. AndEngine

点点圈 提交于 2019-12-06 13:02:06

问题


I'm using AndEngine for a Game 2d. Wanna to load different Activities or better different Scenes for the different levels and for the main menu. How to achieve this ? ..Where can I find an example ?


回答1:


I would recommend for you to use scenes instead activities to avoid black screens between switching activities. Create your own scene manager and use it for changing screens. Basic example:

public class SceneManager {
    private static Game game; // your main activity
    private static SceneManager sm;

    private Scene mScene;

    private SceneManager(){
    }

    public static void init(Game pGame){
            SceneManager.game = pGame;
    }

    public static SceneManager getManager(){
            if(game == null) throw new IllegalStateException("You must first initialize scenemanager class");
            if(sm == null) return sm = new SceneManager();

            return sm;
    }

    public void setMainMenuScreen(){
            mScene = new MainMenuScene();
            game.getEngine().setScene(mScene);
    }

    public void setGameScreen(){
            mScene = new GameScene();
            game.getEngine().setScene(mScene);
    }

    public Scene getCurrScene(){
            return mScene;
    }
}

And in your main class Game.java firstly you should initialize your scene manager class and get local instance. After this feel free to use it according your game logic:

private SceneManager sm;
SceneManager.init(this);
sm = SceneManager.getManager();
sm.setMainMenuScreen();

...

sm.setGameScreen(); 


来源:https://stackoverflow.com/questions/9154251/show-different-scenes-for-different-levels-andengine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!