// 代码一目了然,注释写的很清楚,我就不多说废话了
// 图片:
banana_tiled.png
face_box_tiled.png
helicopter_tiled.png
snapdragon_tiled.png
// 以上是需要的图片,都放到工程目录下的assets/gfx目录下
public class AnimatedSpritesActivity extends BaseGameActivity
{
// Constants
private static final int CAMERA_WIDTH = 480;
private static final int CAMERA_HEIGHT = 320;
// Fields
private Camera mCamera;
// 我们要把上边提到的几张图片加载到mBitmapTextureAtlas上
// 然后mSnapdragonTextureRegion,mHelicopterTextureRegion,mBananaTextureRegion和mFaceTextureRegion
// 各自从mBitmapTextureAtlas上截取自己需要的
private BitmapTextureAtlas mBitmapTextureAtlas;
private TiledTextureRegion mSnapdragonTextureRegion;
private TiledTextureRegion mHelicopterTextureRegion;
private TiledTextureRegion mBananaTextureRegion;
private TiledTextureRegion mFaceTextureRegion;
public Engine onLoadEngine()
{
this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
return new Engine(new EngineOptions(true,
ScreenOrientation.LANDSCAPE, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT),
this.mCamera));
}
public void onLoadResources()
{
// 想象一下mBitmapTextureAtlas是一张大画布,我们在上边画画,画了四张图片,每张是一个TextureRegion
// 由于三个图片的大小为:
// snapdragon_tiled.png : 400 X 180
// bana_tiled.png : 132 X 70
// face_box_tiled.png : 64 X 32
// helicopter_tiled.png : 96 X 84
// 故总共为 (400 + 132 + 64 + 96) X (180,70, 32, 84中的较大者) = 692 X 180
// 因为长和宽必须为2的幂次,所以我们取1024 X 256
this.mBitmapTextureAtlas = new BitmapTextureAtlas(1024, 256, TextureOptions.BILINEAR);
// 设置png的起始路径,我们把所有的图片都放到了assets/gfx下边
BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
// 分别取四个动画的区域
// 解释一下创建mSnapdragonTextureRegion时的参数0,0,4,3,
// 其中的0,0代表该区域在mBitmapTextureAtlas中的起始位置
// 我们需要设置该位置,使四个TextureRegion不重叠
// 同样,想象一下mBitmapTextureAtlas是一张大画布,我们在上边画画,画了四张图片,每张是一个TextureRegion
// 下边解释4,3
// 4代表列数,3代表行数
// 观察一下图片就可以看见,snapdragon_tiled.png这幅图片是分了3行4列的
this.mSnapdragonTextureRegion =
BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(
this.mBitmapTextureAtlas,
this,
"snapdragon_tiled.png",
0, 0, 4, 3);
this.mHelicopterTextureRegion =
BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(
this.mBitmapTextureAtlas,
this,
"helicopter_tiled.png",
400, 0, 2, 2);
this.mBananaTextureRegion =
BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(
this.mBitmapTextureAtlas,
this,
"banana_tiled.png",
496, 0, 4, 2);
this.mFaceTextureRegion =
BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(
this.mBitmapTextureAtlas,
this,
"face_box_tiled.png",
628, 0, 2, 1);
this.mEngine.getTextureManager().loadTexture(this.mBitmapTextureAtlas);
}
public Scene onLoadScene()
{
final Scene scene = new Scene();
scene.setBackground(new ColorBackground(0.09804f, 0.6274f, 0.8784f));
/* Quickly twinkling face. */
final AnimatedSprite face = new AnimatedSprite(100, 50, this.mFaceTextureRegion);
face.animate(100);
scene.attachChild(face);
/* Continuously flying helicopter. */
final AnimatedSprite helicopter = new AnimatedSprite(320, 50, this.mHelicopterTextureRegion);
helicopter.animate(new long[] { 100, 100 }, 1, 2, true);
scene.attachChild(helicopter);
/* Snapdragon. */
final AnimatedSprite snapdragon = new AnimatedSprite(300, 200, this.mSnapdragonTextureRegion);
snapdragon.animate(100);
scene.attachChild(snapdragon);
/* Funny banana. */
final AnimatedSprite banana = new AnimatedSprite(100, 220, this.mBananaTextureRegion);
banana.animate(100);
scene.attachChild(banana);
return scene;
}
public void onLoadComplete()
{
}
}