看了下,AndEngine里面Example中的PathModifierExample 的例子。大致了解到其实现的过程为:
指定元素要运行的路线,然后构造PathModifier,通过PathModifier动画来实现元素按指定路线行进。
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 // =========================================================== }