SyntaxHighlighter

2012年1月31日火曜日

【AndEngine】背景を画像にする


背景に画像を使いたい場合の方法。


onLoadResources()内で
背景テクスチャのTextureRegionクラスのインスタンスを生成

onLoadScene()内で
背景画像からSpriteのインスタンスを生成、
SpriteからSpriteBackgroundのインスタンスを生成してsetBackgroundメソッドに読み込ませてあげる。



■SpriteBackgroundクラスのコンストラクタ
SpriteBackground(final BaseSprite pBaseSprite)
SpriteBackground(final float pRed, final float pGreen, final float pBlue, final BaseSprite pBaseSprite)

■SceneクラスのsetBackgroundメソッド
void setBackground(final IBackground pBackground)

参考にしたソース

AndEngine-Forumsより
Set as a background image

では実際に使用した際から抜粋



/**  省略  **/

private BitmapTextureAtlas mBackgroundTextureAtlas;
private TextureRegion mBackgroundGrassTextureRegion;

public void onLoadResources() {
  BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
  this.mBackgroundTextureAtlas = new BitmapTextureAtlas(512, 512, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
  this.mBackgroundGrassTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBackgroundTextureAtlas, this, "bg1.png", 0, 0);
  this.mEngine.getTextureManager().loadTextures(this.mBackgroundTextureAtlas);
}
 
public Scene onLoadScene() {
  this.mEngine.registerUpdateHandler(new FPSLogger());
  this.mScene = new Scene();
  
  final Sprite sp = new Sprite(0, 0, mBackgroundGrassTextureRegion);
  this.mScene.setBackground(new SpriteBackground(sp));
  
  /** 省略 **/
  
  return this.mScene;
}








0 件のコメント:

コメントを投稿