我是Andengine的新手,我发现如果你想为一些ButtonSprites / Sprit使用一个BitmapTextureAtlas,你必须这样做
BitmapTextureAtlas texAtlas = new BitmapTextureAtlas(activity.getTextureManager(), 316, 1062, TextureOptions.NEAREST_PREMULTIPLYALPHA);
for (int i = 0; i < 10; i++) {
TextureRegion pistolActive = BitmapTextureAtlasTextureRegionFactory.createFromAsset(texAtlas, activity.getApplicationContext(), POWERUP_TEXTURE_PATH, 0, 0);
pistolActive.set(0, 0, TILE_WIDTH, TILE_HEIGHT);
TextureRegion pistolUnactive = BitmapTextureAtlasTextureRegionFactory.createFromAsset(texAtlas, activity.getApplicationContext(), POWERUP_TEXTURE_PATH, 0, 0);
pistolUnactive.set(0, TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT);
TextureRegion pistolDeactive = BitmapTextureAtlasTextureRegionFactory.createFromAsset(texAtlas, activity.getApplicationContext(), POWERUP_TEXTURE_PATH, 0, 0);
pistolDeactive.set(0, TILE_HEIGHT * 2, TILE_WIDTH, TILE_HEIGHT);
texAtlas.load();
ButtonSprite button = new ButtonSprite(0, 0, pistolActive, pistolUnactive, pistolDeactive, activity.getVertexBufferObjectManager(), new ButtonSprite.OnClickListener() {
@Override
public void onClick(final ButtonSprite buttonSprite, float v, float v2) {
}
});
}
但现在我检查一下,如果你在循环之前放置texAtlas.load()
,它也会起作用。但最佳做法是什么?
答案 0 :(得分:0)
BitmapTextureAtlas texAtlas = new BitmapTextureAtlas(activity.getTextureManager(), 316, 1062, TextureOptions.NEAREST_PREMULTIPLYALPHA);
TextureRegion pistolActive = BitmapTextureAtlasTextureRegionFactory.createFromAsset(texAtlas, activity.getApplicationContext(), POWERUP_TEXTURE_PATH, 0, 0);
pistolActive.set(0, 0, TILE_WIDTH, TILE_HEIGHT);
TextureRegion pistolUnactive = BitmapTextureAtlasTextureRegionFactory.createFromAsset(texAtlas, activity.getApplicationContext(), POWERUP_TEXTURE_PATH, 0, 0);
pistolUnactive.set(0, TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT);
TextureRegion pistolDeactive = BitmapTextureAtlasTextureRegionFactory.createFromAsset(texAtlas, activity.getApplicationContext(), POWERUP_TEXTURE_PATH, 0, 0);
pistolDeactive.set(0, TILE_HEIGHT * 2, TILE_WIDTH, TILE_HEIGHT);
texAtlas.load();
for (int i = 0; i < 10; i++) {
ButtonSprite button = new ButtonSprite(0, 0, pistolActive, pistolUnactive, pistolDeactive, activity.getVertexBufferObjectManager(), new ButtonSprite.OnClickListener() {
@Override
public void onClick(final ButtonSprite buttonSprite, float v, float v2) {
}
});
}
无需多次创建纹理区域。这样做。
实际上如果你以这种方式加载,你会遇到一些UI问题。 所以我的建议是使用不同的纹理来加载不同的精灵
如果你想避免加载问题,你可以去Sprite表(例如.. TexturePacker)