使用BuildableBitmapTextureAtlas时,Sprite不显示

时间:2015-01-05 21:07:46

标签: java android andengine

我有以下代码大纲:

资源:

mMenuTextureAtlas = new BuildableBitmapTextureAtlas(pActivity.getTextureManager(), 2048, 2048);
BitmapTextureAtlasTextureRegionFactory.setAssetBasePath(GFX_BASE_LOCATION);
mPlayBtnTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(mMenuTextureAtlas, mActivity, "play_btn.png");
mExitBtnTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(mMenuTextureAtlas, mActivity, "exit_btn.png");
mBackgroundTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(mMenuTextureAtlas, mActivity, "menu_background.png");

构建并加载:

try {
    mMenuTextureAtlas.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(0, 0, 0));
    mMenuTextureAtlas.load();
} catch (TextureAtlasBuilderException e) {
    Debug.e(e);
}

Sprites定义:

mBackgroundSprite = new Sprite(0, 0, 800, 480, mBackgroundTextureRegion, mActivity.getVertexBufferObjectManager());
mPlayBtnSprite = new Sprite(50, 100, 160, 80, mPlayBtnTextureRegion, mActivity.getVertexBufferObjectManager());
mExitBtnSprite = new Sprite(50, 200, 160, 80, mExitBtnTextureRegion, mActivity.getVertexBufferObjectManager());

将精灵附加到场景:

mScene.attachChild(mPlayBtnSprite);
mScene.attachChild(mExitBtnSprite);
mScene.attachChild(mBackgroundSprite);

我在无数的例子中看过上面的大纲,但由于某些原因我的按钮精灵(mPlayBtnSprite mExitBtnSprite)没有在场景中显示。背景精灵(mBackgroundSprite)显示正常。任何想法为什么会发生这种情况以及如何解决这个问题?

更新 - 添加不同情况的说明

  1. 这是在任何更改之前菜单场景的样子(没有按钮精灵!): enter image description here
  2. 如果没有附加mBackgroundSprite,这就是场景的样子(请注意有精灵应该是的矩形,但是它们不是各自的图像,而是背景的颜色): enter image description here
  3. 如果按钮的TextureRegions位于单独的BuildableBitmapTextureAtlas上,这就是场景的样子(两个按钮是相同的,虽然他们的TextureRegions加载了不同的图像。第一个要创建的TextureRegion是两个按钮中显示的那个...)!? enter image description here
  4. 为每个精灵添加一个BuildableBitmapTextureAtlas确实有效,所以这是否意味着每个精灵都需要一个BuildableBitmapTextureAtlas?为什么tutorials我看到它不需要?: enter image description here

2 个答案:

答案 0 :(得分:0)

attachChild顺序很重要,因此如果背景填满整个屏幕,则mExitBtnSprite和mPlayBtnSprite隐藏在它后面。试试这个:

mScene.attachChild(mBackgroundSprite);
mScene.attachChild(mPlayBtnSprite);
mScene.attachChild(mExitBtnSprite);

或者你也可以这样做:

mPlayBtnSprite.setZIndex(1);
mExitBtnSprite.setZIndex(1);
mBackgroundSprite.setZIndex(0);
mScene.sortChildren(true);

修改

试试这个:

mMenuTextureAtlas = new BuildableBitmapTextureAtlas(pActivity.getTextureManager(), 2048, 2048, TextureOptions.BILINEAR);

texture = BitmapTextureAtlasTextureRegionFactory.createFromAsset(mMenuTextureAtlas, mActivity.getAssets(), "name.png");

atlasA = new BuildableBitmapTextureAtlas(pActivity.getTextureManager(), imageAWidth, imageAHeight, TextureOptions.BILINEAR);
atlasB = new BuildableBitmapTextureAtlas(pActivity.getTextureManager(), imageBWidth, imageBHeight, TextureOptions.BILINEAR);
atlasC = new BuildableBitmapTextureAtlas(pActivity.getTextureManager(), imageCWidth, imageCHeight, TextureOptions.BILINEAR);

textureA = BitmapTextureAtlasTextureRegionFactory.createFromAsset(atlasA, mActivity.getAssets(), "imageA.png");
textureB = BitmapTextureAtlasTextureRegionFactory.createFromAsset(atlasB, mActivity.getAssets(), "imageB.png");
textureC = BitmapTextureAtlasTextureRegionFactory.createFromAsset(atlasC, mActivity.getAssets(), "imageC.png");

atlasA.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(0, 0, 0));
atlasB.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(0, 0, 0));
atlasC.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(0, 0, 0));

atlasA.load();
atlasB.load();
atlasC.load();

答案 1 :(得分:0)

我终于弄明白了:在我的代码中,我实际上在加载Sprites之前创建了TextureAtlas。换句话说,

mBackgroundSprite = new Sprite(0, 0, 800, 480, mBackgroundTextureRegion, mActivity.getVertexBufferObjectManager());
mPlayBtnSprite = new Sprite(50, 100, 160, 80, mPlayBtnTextureRegion, mActivity.getVertexBufferObjectManager());
mExitBtnSprite = new Sprite(50, 200, 160, 80, mExitBtnTextureRegion, mActivity.getVertexBufferObjectManager());

之前被调用

try {
    mMenuTextureAtlas.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(0, 0, 0));
    mMenuTextureAtlas.load();
} catch (TextureAtlasBuilderException e) {
    Debug.e(e);
}

已被执行。

这导致意外行为。我希望这可以帮助别人。