viernes, 3 de agosto de 2012

Tutorial Sprite Background AndEngine GLES 2.0

Colocar un Sprite de fondo se hace de la misma manera que colocar un Sprite en la pantalla. Casi todos los pasos son iguales al tutorial anterior si os dejo pegado el código y solo explico lo que es diferente.

  1. public class Main extends SimpleBaseGameActivity {
  2. private static final int CAMERA_WIDTH = 800;
  3. private static final int CAMERA_HEIGHT = 480;
  4. private ITextureRegion mFondoTextureRegion;
  5. private BitmapTextureAtlas mFondoTextureAtlas;
  6. public EngineOptions onCreateEngineOptions() {
  7. final Camera camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
  8. return new EngineOptions(true, ScreenOrientation.LANDSCAPE_SENSOR, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), camera);
  9. }
  10. public void onCreateResources() {
  11. BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
  12. this.mFondoTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(), 1500, 1500);
  13. this.mFondoTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mFondoTextureAtlas, this, "fondo.png", 0, 0);
  14. this.mFondoTextureAtlas.load();
  15. }

Tutorial Sprite AndEngine GLES2

Lo primero que tenemos que hacer es configurar AndEngine GLES 2.0. Los pasos para ello los tenéis en el tutorial anterior. Tras esto creamos una carpeta en nuestro proyecto denominada assets y dentro de ella otra llamada gfx, e introducimos ahí nuestra imagen a cargar.
Ahora creamos tres variables. Una sera un BitmapTexture que es el lienzo donde introducimos nuestros sprites, otra sera una ITextureRegion y otro un objeto Sprite.
  1. private ITextureRegion mEnemigoTextureRegion;
  2. private BitmapTextureAtlas mBitmapTextureAtlas;
  3. public Sprite enemigo;
Ahora definiremos la camara en onCreateEngineOptions().


  1. final Camera camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
  2. return new EngineOptions(true, ScreenOrientation.LANDSCAPE_SENSOR, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), camera);


En esta parte podemos definir antes las variables CAMERA_WIDTH y  CAMERA_HEIGHT antes, o introducir los datos directamenta en la función. Yo las he definido antes.


  1. private static final int CAMERA_WIDTH = 800;
  2. private static final int CAMERA_HEIGHT = 480;