这是一个路径修改器配合动画精灵的例子,我准备分两块来说明,一个是路径修改器的构成;一个是动画精灵的构成。最后达到的效果是一个小人在屏幕四周循环跑动。
路径修改器中定义内部类Path,用于记录路径上的各个点坐标,PathModifier根据坐标生成MoveModifier,完成每两点的移动,一系列的MoveModifier放入一个顺序容器就完成了物体的移动。这种移动是没有与动画集成的,但是每完成两个点的移动就回调IPathModifierListener监听中的onPathWaypointStarted方法,于是可以在其中判断动画经理播放的动画。
动画精灵AnimatedSprite是一种特殊的TiledSprite。TiledSprite将同一个纹理切分为多个方形的显示区域,通过设置索引确定应该显示哪个区域。AnimatedSprite中按照指定时间、指定顺序变换TiledSprite的索引,完成动画的功能。
好了两个重要的部件已经介绍清楚了,程序中合并了两个部件完成人物的行走,下面上程序:
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.IEntity; import org.andengine.entity.modifier.LoopEntityModifier; import org.andengine.entity.modifier.PathModifier; import org.andengine.entity.modifier.PathModifier.IPathModifierListener; import org.andengine.entity.modifier.PathModifier.Path; import org.andengine.entity.scene.Scene; import org.andengine.entity.scene.background.RepeatingSpriteBackground; import org.andengine.entity.sprite.AnimatedSprite; import org.andengine.entity.util.FPSLogger; import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas; import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory; import org.andengine.opengl.texture.atlas.bitmap.source.AssetBitmapTextureAtlasSource; import org.andengine.opengl.texture.region.TiledTextureRegion; import org.andengine.ui.activity.SimpleBaseGameActivity; import org.andengine.util.debug.Debug; import org.andengine.util.modifier.ease.EaseSineInOut; import android.widget.Toast; public class PathModifierExample extends SimpleBaseGameActivity { private static final int CAMERA_WIDTH = 720; private static final int CAMERA_HEIGHT = 480; private RepeatingSpriteBackground mGrassBackground; private BitmapTextureAtlas mBitmapTextureAtlas; private TiledTextureRegion mPlayerTextureRegion; @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"); } //当Path中两点移动开始时调用,其中pWaypointIndex表示Path中的第几段 @Override public void onPathWaypointStarted(final PathModifier pPathModifier, final IEntity pEntity, final int pWaypointIndex) { Debug.d("onPathWaypointStarted: " + pWaypointIndex); switch(pWaypointIndex) { case 0: player.animate(new long[]{200, 200, 200}, 6, 8, true); break; case 1: player.animate(new long[]{200, 200, 200}, 3, 5, true); break; case 2: player.animate(new long[]{200, 200, 200}, 0, 2, true); break; 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; } }应该来说这个例子中动画的切换并没有真正的实现,实际应该通过对路径中两点的坐标差完成动画的选择,应该的写demo的家伙在偷懒吧。有了这个思想,完全可以实现人物的八方向或者十六方向行走,只需要在适当的时候切换动画就可以了。
这里又想多罗嗦两句,之前一直觉得人物的移动是个很麻烦的事情,也想了一些办法来实现,看了AndEngine的实现发现哪些想法真是丑陋无比啊,在事物的抽象方面他确实下了很大的功夫,不经意间问题迎刃而解,单不看Engine的一些细节,面向对象的思想就值得好好去学习一下。