【发布时间】:2014-02-18 09:25:36
【问题描述】:
我想在andengine 中创建一个带有移动精灵的恒定背景图像,
我使用了下面的代码,但它崩溃了。请帮帮我。
public VertexBufferObjectManager vbom;
private void createBackground() {
ParallaxBackground background = new ParallaxBackground(0, 0, 0);
background.attachParallaxEntity(new ParallaxEntity(0, new Sprite(0, 0, background_region, vbom)));
scene.setBackground(background);
}
private void loadGameGraphics() {
BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/game/");
gameTextureAtlas = new BuildableBitmapTextureAtlas(activity.getTextureManager(), 1024, 1024, TextureOptions.BILINEAR);
background_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(gameTextureAtlas, activity, "background.png");
}
日志猫
02-18 04:38:16.370: E/AndEngine(1246): org.andengine.opengl.texture.atlas.buildable.builder.ITextureAtlasBuilder$TextureAtlasBuilderException: Could not build: 'AssetBitmapTextureAtlasSource(gfx/game/background.png)' into: 'BitmapTextureAtlas'.
02-18 04:38:16.370: E/AndEngine(1246): at org.andengine.opengl.texture.atlas.buildable.builder.BlackPawnTextureAtlasBuilder.build(BlackPawnTextureAtlasBuilder.java:88)
02-18 04:38:16.370: E/AndEngine(1246): at org.andengine.opengl.texture.atlas.buildable.BuildableTextureAtlas.build(BuildableTextureAtlas.java:241)
02-18 04:38:16.370: E/AndEngine(1246): at com.game.manager.ResourcesManager.loadGameGraphics(ResourcesManager.java:199)
02-18 04:38:16.370: E/AndEngine(1246): at com.game.manager.ResourcesManager.loadGameResources(ResourcesManager.java:113)
02-18 04:38:16.370: E/AndEngine(1246): at com.game.manager.SceneManager$1.onTimePassed(SceneManager.java:128)
02-18 04:38:16.370: E/AndEngine(1246): at org.andengine.engine.handler.timer.TimerHandler.onUpdate(TimerHandler.java:94)
02-18 04:38:16.370: E/AndEngine(1246): at org.andengine.engine.handler.UpdateHandlerList.onUpdate(UpdateHandlerList.java:47)
02-18 04:38:16.370: E/AndEngine(1246): at org.andengine.engine.Engine.onUpdateUpdateHandlers(Engine.java:618)
02-18 04:38:16.370: E/AndEngine(1246): at org.andengine.engine.Engine.onUpdate(Engine.java:605)
02-18 04:38:16.370: E/AndEngine(1246): at org.andengine.engine.LimitedFPSEngine.onUpdate(LimitedFPSEngine.java:52)
02-18 04:38:16.370: E/AndEngine(1246): at org.andengine.engine.Engine.onTickUpdate(Engine.java:568)
02-18 04:38:16.370: E/AndEngine(1246): at org.andengine.engine.Engine$UpdateThread.run(Engine.java:858)
02-18 04:38:16.390: W/dalvikvm(1246): threadid=13: thread exiting with uncaught exception (group=0xb1a35ba8)
02-18 04:38:16.410: E/AndroidRuntime(1246): FATAL EXCEPTION: UpdateThread
02-18 04:38:16.410: E/AndroidRuntime(1246): Process: com.game, PID: 1246
02-18 04:38:16.410: E/AndroidRuntime(1246): java.lang.NullPointerException
02-18 04:38:16.410: E/AndroidRuntime(1246): at org.andengine.entity.sprite.Sprite.<init>(Sprite.java:62)
02-18 04:38:16.410: E/AndroidRuntime(1246): at com.game.scene.GameScene.createBackground(GameScene.java:809)
02-18 04:38:16.410: E/AndroidRuntime(1246): at org.andengine.engine.Engine.onUpdate(Engine.java:605)
02-18 04:38:16.410: E/AndroidRuntime(1246): at org.andengine.engine.LimitedFPSEngine.onUpdate(LimitedFPSEngine.java:52)
02-18 04:38:16.410: E/AndroidRuntime(1246): at org.andengine.engine.Engine.onTickUpdate(Engine.java:568)
02-18 04:38:16.410: E/AndroidRuntime(1246): at org.andengine.engine.Engine$UpdateThread.run(Engine.java:858)
02-18 04:38:16.520: D/AndEngine(1246): GameActivity.onPause @(Thread: 'main')
在gamescene.java中,
private void loadGameGraphics() {
BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/game/");
mAutoParallaxBackgroundTexture = new BitmapTextureAtlas(activity.getTextureManager(), 1024, 1024,TextureOptions.BILINEAR);
mParallaxLayerFront = BitmapTextureAtlasTextureRegionFactory.createFromAsset( mAutoParallaxBackgroundTexture, activity, "parallax_background_layer_front.png", 0, 0);
mParallaxLayerBack = BitmapTextureAtlasTextureRegionFactory.createFromAsset( mAutoParallaxBackgroundTexture, activity, "parallax_background_layer_back.png", 0, 188);
mParallaxLayerMid = BitmapTextureAtlasTextureRegionFactory.createFromAsset( mAutoParallaxBackgroundTexture, activity, "parallax_background_layer_mid.png", 0, 669);
}
在资源.java中
私有场景 createBackground() {
resourcesManager.mAutoParallaxBackgroundTexture.load();
final Scene scene = new Scene();
final AutoParallaxBackground autoParallaxBackground = new AutoParallaxBackground(0, 0, 0, 5);
int CAMERA_HEIGHT = 480;
autoParallaxBackground.attachParallaxEntity(new ParallaxEntity(0.0f, new Sprite(0, CAMERA_HEIGHT - resourcesManager.mParallaxLayerBack.getHeight(), resourcesManager.mParallaxLayerBack,vbom)));
autoParallaxBackground.attachParallaxEntity(new ParallaxEntity(-5.0f, new Sprite(0, 80, resourcesManager.mParallaxLayerMid, vbom)));
autoParallaxBackground.attachParallaxEntity(new ParallaxEntity(-10.0f, new Sprite(0, CAMERA_HEIGHT - resourcesManager.mParallaxLayerFront.getHeight(), resourcesManager.mParallaxLayerFront, vbom)));
scene.setBackground(autoParallaxBackground);
返回场景;
}
堆栈跟踪
02-19 05:01:55.710:W/AndEngine(1083):ParallaxEntity 的 OffsetCenterXposition 预计为 0。 02-19 05:01:55.710:W/AndEngine(1083):a 的 OffsetCenterXposition ParallaxEntity 预计为 0。02-19 05:01:55.710:W/AndEngine(1083):ParallaxEntity 的 OffsetCenterXposition 预计为 0。
公共类 GameActivity 扩展 BaseGameActivity {
private static final int CAMERA_WIDTH = 720;
private static final int CAMERA_HEIGHT = 480;
private BoundCamera camera;
private ITexture parallax_background;
private Music music;
private ITextureRegion background_region;
private BitmapTextureAtlas mAutoParallaxBackgroundTexture;
private BuildableBitmapTextureAtlas al;
private BitmapTextureAtlas background;
public ITextureRegion mParallaxLayerFront;
public ITextureRegion mParallaxLayerBack;
public ITextureRegion mParallaxLayerMid;
private Player player;
@Override
public Engine onCreateEngine(EngineOptions pEngineOptions)
{
return new LimitedFPSEngine(pEngineOptions, 60);
}
public EngineOptions onCreateEngineOptions()
{
camera = new BoundCamera(0, 0, 800, 480);
EngineOptions engineOptions = new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new FillResolutionPolicy(), this.camera);
engineOptions.getAudioOptions().setNeedsMusic(true).setNeedsSound(true);
//engineOptions.getRenderOptions().getConfigChooserOptions().setRequestedMultiSampling(true);
engineOptions.setWakeLockOptions(WakeLockOptions.SCREEN_ON);
return engineOptions;
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK)
{
SceneManager.getInstance().getCurrentScene().onBackKeyPressed();
}
return false;
}
public void onCreateResources(OnCreateResourcesCallback pOnCreateResourcesCallback) throws IOException
{
MusicFactory.setAssetBasePath("mfx/");
try {
music = MusicFactory.createMusicFromAsset(mEngine
.getMusicManager(), this, "abcd.wav");
music.setLooping(true);
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/game");
this.mAutoParallaxBackgroundTexture = new BitmapTextureAtlas(this.getTextureManager(), 2048, 2048,TextureOptions.BILINEAR);
this.mParallaxLayerFront = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mAutoParallaxBackgroundTexture, this, "parallax_background_layer_front.png", 0, 0);
this.mParallaxLayerBack = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mAutoParallaxBackgroundTexture, this, "parallax_background_layer_back.png", 0, 188);
this.mParallaxLayerMid = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mAutoParallaxBackgroundTexture, this, "parallax_background_layer_mid.png", 0, 669);
this.mAutoParallaxBackgroundTexture.load();
ResourcesManager.prepareManager(mEngine, this, camera, getVertexBufferObjectManager());
//mEngine.getTextureManager().loadTexture( mAutoParallaxBackgroundTexture);
pOnCreateResourcesCallback.onCreateResourcesFinished();
}
public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback) throws IOException
{
music.play();
SceneManager.getInstance().createSplashScene(pOnCreateSceneCallback);
this.mEngine.registerUpdateHandler(new FPSLogger());
final Scene scene = new Scene();
final AutoParallaxBackground autoParallaxBackground = new AutoParallaxBackground(0, 0, 0, 5);
final VertexBufferObjectManager vertexBufferObjectManager = this.getVertexBufferObjectManager();
autoParallaxBackground.attachParallaxEntity(new ParallaxEntity(0.0f, new Sprite(0, CAMERA_HEIGHT - this.mParallaxLayerBack.getHeight(), this.mParallaxLayerBack, vertexBufferObjectManager)));
autoParallaxBackground.attachParallaxEntity(new ParallaxEntity(-5.0f, new Sprite(0, 80, this.mParallaxLayerMid, vertexBufferObjectManager)));
autoParallaxBackground.attachParallaxEntity(new ParallaxEntity(-10.0f, new Sprite(0, CAMERA_HEIGHT - this.mParallaxLayerFront.getHeight(), this.mParallaxLayerFront, vertexBufferObjectManager)));
scene.setBackground(autoParallaxBackground);
scene.attachChild(player);
return;
}
public void onPopulateScene(Scene pScene, OnPopulateSceneCallback pOnPopulateSceneCallback) throws IOException
{
mEngine.registerUpdateHandler(new TimerHandler(2f, new ITimerCallback()
{
public void onTimePassed(final TimerHandler pTimerHandler)
{
mEngine.unregisterUpdateHandler(pTimerHandler);
SceneManager.getInstance().createMenuScene();
}
}));
pOnPopulateSceneCallback.onPopulateSceneFinished();
}
@Override
protected void onDestroy()
{
super.onDestroy();
System.exit(0);
}
}
【问题讨论】:
-
如果它确实崩溃了,并且错误是您发布的代码,则意味着该场景为空。你仍然应该发布崩溃的堆栈跟踪
-
感谢您的回复,我添加了stacktrace。
-
对我来说似乎 vbom 是空的
-
我是andengine的新手,我还没有将vbom初始化为null。我不知道如何解决这个问题,我花了很多时间,参考了很多教程,但无法解决我的问题问题。
-
你在扩展哪个类?
标签: android background andengine parallax