import org.andengine.engine.camera.Camera;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.background.Background;
import org.andengine.entity.sprite.AnimatedSprite;
import org.andengine.entity.util.FPSLogger;
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.atlas.bitmap.BuildableBitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.source.IBitmapTextureAtlasSource;
import org.andengine.opengl.texture.atlas.buildable.builder.BlackPawnTextureAtlasBuilder;
import org.andengine.opengl.texture.atlas.buildable.builder.ITextureAtlasBuilder.TextureAtlasBuilderException;
import org.andengine.opengl.texture.region.TiledTextureRegion;
import org.andengine.ui.activity.SimpleBaseGameActivity;
import android.util.DisplayMetrics;
/**
* 多个动画精灵
*/
public class AnimateSpriteActivity extends SimpleBaseGameActivity
{
private static int winWidth = 854;
private static int winHeight = 480;
//图片纹理区域(自动定义位置)
private BuildableBitmapTextureAtlas mBuildableBitmapTextureAtlas;
private TiledTextureRegion mBananaTiledTextureRegion,mFaceBoxTiledTextureRegion,
mHelicopterTiledTextureRegion,mSnapdragonTiledTextureRegion;
@Override
public EngineOptions onCreateEngineOptions()
{
setScreenDisplay();
Camera camera = new Camera(0, 0, winWidth, winHeight);
return new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new RatioResolutionPolicy(winWidth, winHeight), camera);
}
@Override
protected void onCreateResources()
{
BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("images/");
/*
* BuildableBitmapTextureAtlas对象是一个很好的方式来实现ITextureRegion对象在我们的纹理地图中,无需手动定义位置。
* 而BitmapTextureAtlas是手动定义TextureRegion在BitmapTextureAtlas中的位置。
* BuildableBitmapTextureAtlas纹理地图的目的是自动将其ITextureRegion对象放到纹理地图中最合适的坐标。
* 用这种方法创建纹理是最简单,最有效的方法,因为使用很多纹理地图开发大型游戏时,会非常耗时和容易出错。
* 除了BuildableBitmapTextureAtlas的自动化功能,开发人员可以在纹理地图中定义透明边距,以避免纹理出血的发生。
*/
mBuildableBitmapTextureAtlas = new BuildableBitmapTextureAtlas(this.getTextureManager(), 1024, 1024, TextureOptions.DEFAULT);
mBananaTiledTextureRegion = BitmapTextureAtlasTextureRegionFactory
.createTiledFromAsset(mBuildableBitmapTextureAtlas, this, "banana_tiled.png", 4, 2);
mFaceBoxTiledTextureRegion = BitmapTextureAtlasTextureRegionFactory
.createTiledFromAsset(mBuildableBitmapTextureAtlas, this, "face_box_tiled.png", 2, 1);
mHelicopterTiledTextureRegion = BitmapTextureAtlasTextureRegionFactory
.createTiledFromAsset(mBuildableBitmapTextureAtlas, this, "helicopter_tiled.png", 2, 2);
mSnapdragonTiledTextureRegion = BitmapTextureAtlasTextureRegionFactory
.createTiledFromAsset(mBuildableBitmapTextureAtlas, this, "snapdragon_tiled.png", 4, 3);
try
{
//BlackPawnTextureAtlasBuilder作用是AndEngine自动组合N个贴图纹理到一张大的纹理里面。其几个参数是子纹理间的距离等
//在几乎所有的情况下,我们将避免纹理出血的可能性。但是在极端的情况下如果纹理出血,请加大第三个参数的值,这将有助于缓解问题
mBuildableBitmapTextureAtlas.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(0, 1, 1));
} catch (TextureAtlasBuilderException e)
{
e.printStackTrace();
}
//一旦地图集已经建成,我们现在可以加载
mBuildableBitmapTextureAtlas.load();
}
@Override
protected Scene onCreateScene()
{
this.mEngine.registerUpdateHandler(new FPSLogger());
Scene scene = new Scene();
scene.setBackground(new Background(0.09804f, 0.6274f, 0.8784f));
AnimatedSprite mBananaSprite = new AnimatedSprite(100, 50, mBananaTiledTextureRegion, this.getVertexBufferObjectManager());
AnimatedSprite mFaceBoxSprite = new AnimatedSprite(400, 30, mFaceBoxTiledTextureRegion, this.getVertexBufferObjectManager());
AnimatedSprite mHelicopterSprite = new AnimatedSprite(100, 250, mHelicopterTiledTextureRegion, this.getVertexBufferObjectManager());
AnimatedSprite mSnapdragonSprite = new AnimatedSprite(400, 250, mSnapdragonTiledTextureRegion, this.getVertexBufferObjectManager());
mBananaSprite.animate(100);
mFaceBoxSprite.animate(100);
mHelicopterSprite.animate(new long[]{100,100},1,2,true);
mSnapdragonSprite.animate(100);
scene.attachChild(mBananaSprite);
scene.attachChild(mFaceBoxSprite);
scene.attachChild(mHelicopterSprite);
scene.attachChild(mSnapdragonSprite);
return scene;
}
/**
* 设置屏幕大小
*/
private void setScreenDisplay()
{
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
winWidth = displayMetrics.widthPixels;
winHeight = displayMetrics.heightPixels;
}
}