又是一个参数说明的例子,AnimatedSprite已经在前面多次出现,在上一节中我也说明了原理,这Demo就看着对具体细节的学习吧。
首先TiledSprite需要的ITextureRegion为ITiledTextureRegion,区别在于ITiledTextureRegion中包含了多个ITextureRegion,根据索引就可以完成显示范围的切换了。当定义纹理范围时,确认了精灵的大小,和横向、纵向切分的块数,最后这些区域会形成一个区域数组,在AnimatedSprite中调用。
AnimatedSprite中的animate方法是关键,他定义了有哪些图片组成播放的动画,每张图片间隔时间为多长,是否循环播放等等信息。
好了,整个问题就简单了,继续上代码:
package org.andengine.examples;
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 org.andengine.util.debug.Debug;
public class AnimatedSpritesExample extends SimpleBaseGameActivity {
private static final int CAMERA_WIDTH = 480;
private static final int CAMERA_HEIGHT = 320;
private BuildableBitmapTextureAtlas mBitmapTextureAtlas;
private TiledTextureRegion mSnapdragonTextureRegion;
private TiledTextureRegion mHelicopterTextureRegion;
private TiledTextureRegion mBananaTextureRegion;
private TiledTextureRegion mFaceTextureRegion;
@Override
public EngineOptions onCreateEngineOptions() {
final Camera camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
return new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), camera);
}
@Override
public void onCreateResources() {
BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
//可构建的纹理集
this.mBitmapTextureAtlas = new BuildableBitmapTextureAtlas(this.getTextureManager(), 512, 256, TextureOptions.NEAREST);
// this.mBitmapTextureAtlas = new BuildableBitmapTextureAtlas(this.getTextureManager(), 512, 256, TextureOptions.BILINEAR);
this.mSnapdragonTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlas, this, "snapdragon_tiled.png", 4, 3);
this.mHelicopterTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlas, this, "helicopter_tiled.png", 2, 2);
this.mBananaTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlas, this, "banana_tiled.png", 4, 2);
this.mFaceTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlas, this, "face_box_tiled.png", 2, 1);
try {
//构建纹理集,按照BlackPawnTextureAtlasBuilder的设置计算间隔,并回调设置TiledTextureRegion的Tile大小。
this.mBitmapTextureAtlas.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(0, 0, 1));
this.mBitmapTextureAtlas.load();
} catch (TextureAtlasBuilderException e) {
Debug.e(e);
}
}
@Override
public Scene onCreateScene() {
this.mEngine.registerUpdateHandler(new FPSLogger());
final Scene scene = new Scene();
scene.setBackground(new Background(0.09804f, 0.6274f, 0.8784f));
/* Quickly twinkling face. */
final AnimatedSprite face = new AnimatedSprite(100, 50, this.mFaceTextureRegion, this.getVertexBufferObjectManager());
//延时100毫秒,循环播放所有Tile
face.animate(100);
scene.attachChild(face);
/* Continuously flying helicopter. */
final AnimatedSprite helicopter = new AnimatedSprite(320, 50, this.mHelicopterTextureRegion, this.getVertexBufferObjectManager());
//循环播放索引为1-2的Tile,间隔时间分别为100,100
helicopter.animate(new long[] { 100, 100 }, 1, 2, true);
scene.attachChild(helicopter);
/* Snapdragon. */
final AnimatedSprite snapdragon = new AnimatedSprite(300, 200, this.mSnapdragonTextureRegion, this.getVertexBufferObjectManager());
snapdragon.animate(100);
scene.attachChild(snapdragon);
/* Funny banana. */
final AnimatedSprite banana = new AnimatedSprite(100, 220, this.mBananaTextureRegion, this.getVertexBufferObjectManager());
banana.animate(100);
scene.attachChild(banana);
return scene;
}
}