我有以下代码大纲:
资源:
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
)显示正常。任何想法为什么会发生这种情况以及如何解决这个问题?
更新 - 添加不同情况的说明
答案 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);
}
已被执行。
这导致意外行为。我希望这可以帮助别人。