public class PathModifierExample extends SimpleBaseGameActivity {
// ===========================================================
// Constants
// ===========================================================
private static final int CAMERA_WIDTH = 720;
private static final int CAMERA_HEIGHT = 480;
// ===========================================================
// Fields
// ===========================================================
private RepeatingSpriteBackground mGrassBackground;
private BitmapTextureAtlas mBitmapTextureAtlas;
private TiledTextureRegion mPlayerTextureRegion;
// ===========================================================
// Constructors
// ===========================================================
// ===========================================================
// Getter & Setter
// ===========================================================
// ===========================================================
// Methods for/from SuperClass/Interfaces
// ===========================================================
@Override
public EngineOptions onCreateEngineOptions() {
Toast.makeText(this, "You move my sprite right round, right round...",
Toast.LENGTH_LONG).show();
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.mGrassBackground = new RepeatingSpriteBackground(CAMERA_WIDTH,
CAMERA_HEIGHT, this.getTextureManager(),
AssetBitmapTextureAtlasSource.create(this.getAssets(),
"gfx/background_grass.png"),
this.getVertexBufferObjectManager());
// 創建紋理集
this.mBitmapTextureAtlas = new BitmapTextureAtlas(
this.getTextureManager(), 128, 128);
// 創建紋理區域
this.mPlayerTextureRegion = BitmapTextureAtlasTextureRegionFactory
.createTiledFromAsset(this.mBitmapTextureAtlas, this,
"player.png", 0, 0, 3, 4);
this.mBitmapTextureAtlas.load();
}
@Override
public Scene onCreateScene() {
this.mEngine.registerUpdateHandler(new FPSLogger());
final Scene scene = new Scene();
scene.setBackground(this.mGrassBackground);
/* Create the face and add it to the scene. */
final AnimatedSprite player = new AnimatedSprite(10, 10, 48, 64,
this.mPlayerTextureRegion, this.getVertexBufferObjectManager());
// 指定其行走的路線
final Path path = new Path(5).to(10, 10).to(10, CAMERA_HEIGHT - 74)
.to(CAMERA_WIDTH - 58, CAMERA_HEIGHT - 74)
.to(CAMERA_WIDTH - 58, 10).to(10, 10);
/* Add the proper animation when a waypoint of the path is passed. */
player.registerEntityModifier(
// 循環實體修飾集合
new LoopEntityModifier(
// 構造路線修飾實體
new PathModifier(30, path, null, new IPathModifierListener() {
@Override
public void onPathStarted(final PathModifier pPathModifier,
final IEntity pEntity) {
Debug.d("onPathStarted");
}
@Override
public void onPathWaypointStarted(
final PathModifier pPathModifier,
final IEntity pEntity, final int pWaypointIndex) {
Debug.d("onPathWaypointStarted: " + pWaypointIndex);
switch (pWaypointIndex) {
// 人物向下走的時候,使用第6到第8張圖
case 0:
player.animate(new long[] { 200, 200, 200 }, 6, 8,
true);
break;
// 人物像向走的時候,使用第3到第5張圖
case 1:
player.animate(new long[] { 200, 200, 200 }, 3, 5,
true);
break;
// 人物向上走的時候,使用第0到2張圖
case 2:
player.animate(new long[] { 200, 200, 200 }, 0, 2,
true);
break;
// 人物向左走的號死後,使用第9到11張圖
case 3:
player.animate(new long[] { 200, 200, 200 }, 9, 11,
true);
break;
}
}
@Override
public void onPathWaypointFinished(
final PathModifier pPathModifier,
final IEntity pEntity, final int pWaypointIndex) {
Debug.d("onPathWaypointFinished: " + pWaypointIndex);
}
@Override
public void onPathFinished(
final PathModifier pPathModifier,
final IEntity pEntity) {
Debug.d("onPathFinished");
}
}, EaseSineInOut.getInstance())));
scene.attachChild(player);
return scene;
}
// ===========================================================
// Methods
// ===========================================================
// ===========================================================
// Inner and Anonymous Classes
// ===========================================================
}