Created
November 7, 2013 00:24
-
-
Save j0t3x/7346767 to your computer and use it in GitHub Desktop.
andengine2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.IOException; | |
import org.andengine.engine.camera.Camera; | |
import org.andengine.engine.options.EngineOptions; | |
import org.andengine.engine.options.ScreenOrientation; | |
import org.andengine.engine.options.WakeLockOptions; | |
import org.andengine.engine.options.resolutionpolicy.FillResolutionPolicy; | |
import org.andengine.entity.scene.Scene; | |
import org.andengine.entity.sprite.AnimatedSprite; | |
import org.andengine.entity.sprite.Sprite; | |
import org.andengine.opengl.texture.TextureOptions; | |
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas; | |
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory; | |
import org.andengine.opengl.texture.region.ITextureRegion; | |
import org.andengine.opengl.texture.region.ITiledTextureRegion; | |
import org.andengine.ui.activity.SimpleBaseGameActivity; | |
public class MainActivity extends SimpleBaseGameActivity { | |
private Camera mCamera; | |
private static int WIDTH = 800; | |
private static int HEIGHT= 480; | |
private BitmapTextureAtlas miAtlas; | |
private ITextureRegion texturaChar; | |
private Sprite charSprite; | |
private ITiledTextureRegion texturaAnimada; | |
private AnimatedSprite spriteAnimado; | |
@Override | |
public EngineOptions onCreateEngineOptions() { | |
// Definimos nuestra camara | |
mCamera = new Camera(0, 0, WIDTH, HEIGHT); | |
// Ahora declaramos las opciones del motor | |
EngineOptions engineOptions = new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new FillResolutionPolicy(), mCamera); | |
//EngineOptions(es full screen?, Cual es la orientacion de la pantalla?, Como actuaremos ante distintas resoluciones?, y la camara) | |
// impedimos que la pantalla se apague por inactividad | |
engineOptions.setWakeLockOptions(WakeLockOptions.SCREEN_ON); | |
// Return the engineOptions object, passing it to the engine | |
return engineOptions; | |
} | |
@Override | |
protected void onCreateResources() throws IOException { | |
//primero debemos indicar donde estan las imagenes | |
BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/"); | |
//luego crear el atlas, darle medidas y un tipo de textura. | |
miAtlas = new BitmapTextureAtlas(getTextureManager(), 800, 800, TextureOptions.DEFAULT); | |
//ubicamos nuestra imagen en el atlas | |
texturaChar = BitmapTextureAtlasTextureRegionFactory.createFromAsset(miAtlas, this, "char.png", 0, 0); | |
//Aqui ubicamos el sprite para que no se ubique sobre el anterior | |
//Osea teniendo en cuenta que el anterior estaba en 0,0 y ocupa hasta 75,95 | |
//Lo ubicaremos despues 76 en x, 0 en y, e indicamos que tiene 10 columnas y 10 filas. | |
texturaAnimada = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(miAtlas, this, "animado.png", 76, 0, 10, 10); | |
//y la cargamos | |
miAtlas.load(); | |
//Aqui hemos creado un atlas de 256 x 256 para cargar una imagen de 108 x 253 | |
//en el punto 0,0 de miAtlas, osea nos sobra espacio | |
} | |
@Override | |
protected Scene onCreateScene() { | |
Scene sceneEjemplo = new Scene(); | |
//(posicion x, posicion y, textura, elemento de andengine ignorenlo) | |
charSprite = new Sprite(100, 200, texturaChar, getVertexBufferObjectManager()); | |
//(posicion x, posicion y, textura, elemento de andengine ignorenlo) - igual que arriba | |
spriteAnimado = new AnimatedSprite(200, 200,texturaAnimada, getVertexBufferObjectManager()); | |
//para animar el sprite debemos otorgarle tiempos en milisegundos a cada frame | |
long[] duracionFrame = { 200, 200, 200, 200, 200, 200}; | |
//( tiempos x frame, del frame 1 al 6, siempre repitiendose) | |
spriteAnimado.animate(duracionFrame, 1, 6, true); | |
//y lo añadimos a la escena | |
sceneEjemplo.attachChild(spriteAnimado); | |
sceneEjemplo.attachChild(charSprite); | |
return sceneEjemplo; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment