我想在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");
}
log cat
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);
}
在resources.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):OffsetCenterXposition of a 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);
}
}
答案 0 :(得分:0)
vbom为空。
第一方面,你需要添加vertexBufferObjectManager,当你在Andengine GLES2中初始化任何实体时,你不需要初始化自己的vbom变量:
所以,这是最简单的方法:
background.attachParallaxEntity(new ParallaxEntity(0, new Sprite(0, 0, background_region, this.getVertexBufferObjectManager())));
如果你来自一个活动(BaseGameActivity或者像这样,我看到你的&#34;活动&#34;变量),你可以使用:
background.attachParallaxEntity(new ParallaxEntity(0, new Sprite(0, 0, background_region, activity.getVertexBufferObjectManager())));
另一方面你的&#34; background.png&#34;需要进入&#34; assets / gfx / game&#34;项目中的文件夹。