我正在使用AndEngine开发一个场景管理器,并且正在获取GLMatrixStackOverflowExceptionerror:
E / AndroidRuntime(1906):致命异常:GLThread 130 E / AndroidRuntime(1906):org.andengine.opengl.util.GLMatrixStack $ GLMatrixStackOverflowException E / AndroidRuntime(1906):at org.andengine.opengl.util.GLMatrixStack.glPushMatrix(GLMatrixStack.java:94) E / AndroidRuntime(1906):at org.andengine.opengl.util.GLState.pushProjectionGLMatrix(GLState.java:574) E / AndroidRuntime(1906):at org.andengine.entity.scene.Scene.onManagedDraw(Scene.java:243) E / AndroidRuntime(1906):at org.andengine.entity.Entity.onDraw(Entity.java:1348) E / AndroidRuntime(1906):at org.andengine.entity.Entity.onManagedDraw(Entity.java:1585) E / AndroidRuntime(1906):at org.andengine.entity.scene.Scene.onManagedDraw(Scene.java:259)
以下2个类:GameActivity
和SceneManager
问题出现在此行的GameActivity
中:
sceneManager.setCurrentScene(SceneType.MENU); // ???
我做错了什么?
package com.example;
import java.io.IOException;
import org.andengine.engine.camera.Camera;
import org.andengine.engine.handler.timer.ITimerCallback;
import org.andengine.engine.handler.timer.TimerHandler;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.andengine.entity.scene.Scene;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.ui.activity.BaseGameActivity;
import android.util.Log;
import com.example.scenemanager.SceneManager;
import com.example.scenemanager.SceneManager.SceneType;
/**
* Example of using a SceneManager with GLES2 AnchorCenter
*/
public class GameActivity extends BaseGameActivity {
Scene mScene;
protected static final int CAMERA_WIDTH = 800;
protected static final int CAMERA_HEIGHT = 480;
BitmapTextureAtlas playerTexture;
ITextureRegion playerTextureRegion;
SceneManager sceneManager;
Camera mCamera;
@Override
public EngineOptions onCreateEngineOptions() {
mCamera = new Camera (0,0, CAMERA_WIDTH, CAMERA_HEIGHT);
EngineOptions engineOptions = (new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT) , mCamera));
return engineOptions;
}
@Override
public void onCreateResources(
OnCreateResourcesCallback pOnCreateResourcesCallback)
throws IOException {
// mEngine is from superclass
sceneManager = new SceneManager(this, mEngine, mCamera);
sceneManager.loadSplashSceneResources();
// let the game engine know we are done loading the resources we need
pOnCreateResourcesCallback.onCreateResourcesFinished();
}
@Override
public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)
throws IOException {
pOnCreateSceneCallback.onCreateSceneFinished(sceneManager.createSplashScene());
}
@Override
public void onPopulateScene(Scene pScene, OnPopulateSceneCallback pOnPopulateSceneCallback)
throws IOException {
mEngine.registerUpdateHandler(new TimerHandler(3.0f, new ITimerCallback() { // 3 seconds to load all stuff
@Override
public void onTimePassed(TimerHandler pTimerHandler) {
Log.d("onPopulateScene()", "onTimePassed()");
// unregister so as only do this once
mEngine.unregisterUpdateHandler(pTimerHandler);
// done displaying splash, so go to menu
sceneManager.loadMenuSceneResources();
sceneManager.createMenuScene();
mScene.registerUpdateHandler(new IUpdateHandler() {
@Override
public void reset() { }
@Override
public void onUpdate(final float pSecondsElapsed) {
Log.d("onPopulateScene()", "onUpdate()");
sceneManager.setCurrentScene(SceneType.MENU); // ???
}
});
}
}));
pOnPopulateSceneCallback.onPopulateSceneFinished();
}
}
package com.example.scenemanager;
import org.andengine.engine.Engine;
import org.andengine.engine.camera.Camera;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.background.Background;
import org.andengine.entity.sprite.Sprite;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.region.TextureRegion;
import org.andengine.ui.activity.BaseGameActivity;
public class SceneManager {
private SceneType currentScene;
private BaseGameActivity activity;
private Engine engine;
private Camera camera;
private Scene splashScene;
private Scene menuScene;
private Scene gameScene;
private BitmapTextureAtlas splashTextureAtlas;
private TextureRegion splashTextureRegion;
private BitmapTextureAtlas menuTextureAtlas;
private TextureRegion menuTextureRegion;
private float xPosition;
private float yPosition;
public enum SceneType
{
SPLASH,
MENU,
GAME
}
/**
* constructor
*
* @param baseGameActivity
* @param engine
* @param camera
*/
public SceneManager(BaseGameActivity baseGameActivity, Engine engine, Camera camera) {
this.activity = baseGameActivity;
this.engine = engine;
this.camera = camera;
xPosition = camera.getWidth()/2;
yPosition = camera.getHeight()/2;
}
public void loadSplashSceneResources() {
BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
splashTextureAtlas = new BitmapTextureAtlas(this.activity.getTextureManager(),256, 256);
splashTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(splashTextureAtlas, this.activity, "splash.png", 0, 0);
splashTextureAtlas.load();
}
public void loadMenuSceneResources() {
BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
menuTextureAtlas = new BitmapTextureAtlas(this.activity.getTextureManager(),256, 256);
menuTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(menuTextureAtlas, this.activity, "menu.png", 0, 0);
menuTextureAtlas.load();
}
public void loadGameSceneResources() {
}
public Scene createSplashScene() {
// Set background color and add the splash image
splashScene = new Scene();
splashScene.setBackground(new Background(0, 1, 0)); // green
Sprite splash = new Sprite(0, 0, splashTextureRegion, activity.getVertexBufferObjectManager());
splash.setPosition(xPosition, yPosition);
splashScene.attachChild(splash);
return splashScene;
}
public Scene createMenuScene() {
menuScene = new Scene();
menuScene.setBackground(new Background(0,0,0));
Sprite sprite = new Sprite (0, 0, menuTextureRegion, engine .getVertexBufferObjectManager());
sprite.setPosition(xPosition, yPosition);
menuScene.attachChild(menuScene);
return menuScene;
}
public void createGameScene() {
//Create the Main Game Scene and set background colour to blue
gameScene = new Scene();
gameScene.setBackground(new Background(0, 0, 1));
}
public SceneType getCurrentScene() {
return currentScene;
}
public void setCurrentScene(SceneType scene) {
if (scene != currentScene) {
currentScene = scene;
switch (scene)
{
case SPLASH:
break;
case MENU:
engine.setScene(menuScene);
break;
case GAME:
engine.setScene(gameScene);
break;
default:
break;
}
}
}
}