AndEngine Example(8):PathModifierExample

来源:互联网 发布:电子元器件数据库 编辑:程序博客网 时间:2024/06/06 04:46

目标:

1. 了解场景背景图片的设置RepeatingSpriteBackground

2.  掌握如何使用PathModifier控制精灵的移动

3. 了解如何使用路径Path得到PathModifier

4. Sprite的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.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;/** * (c) 2010 Nicolas Gramlich * (c) 2011 Zynga * * @author Nicolas Gramlich * @since 11:54:51 - 03.04.2010 */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// ===========================================================@Overridepublic 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);}@Overridepublic 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();}@Overridepublic 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() {@Overridepublic void onPathStarted(final PathModifier pPathModifier, final IEntity pEntity) {Debug.d("onPathStarted");}@Overridepublic 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;}}@Overridepublic void onPathWaypointFinished(final PathModifier pPathModifier, final IEntity pEntity, final int pWaypointIndex) {Debug.d("onPathWaypointFinished: " + pWaypointIndex);}@Overridepublic void onPathFinished(final PathModifier pPathModifier, final IEntity pEntity) {Debug.d("onPathFinished");}}, EaseSineInOut.getInstance())));scene.attachChild(player);return scene;}// ===========================================================// Methods// ===========================================================// ===========================================================// Inner and Anonymous Classes// ===========================================================}


-----------------------------------------

使用RepeatingSpriteBackground的好处是:

    使用一张较小的图,通过重复,就可以得到整个背景。


路径构造函数中可以指定路径分为多少段。然后使用to(x,y)来连接到下一个位置

   你可以使用一个Point数组,然后

Path path = new Path(points.length);for(Point point:points) {    path.to(point.x,point.y);}
这样就得到一个完整的路径了。


PathModifier(float,Path,IEntityModiferListener,IPathModiferListener,IEaseFunction)

para1: 整个路径完成需要的时间

para2: 路径

para3: 修饰器监听

para4: 路径监听器

para5: 可以理解为一个插值器(interpolator)


路径监听器包含4个回调函数:

onPathStarted(...) 整个路径的开始

onPathWaypointStarted(final PathModifier pPathModifier, final IEntity pEntity, final int pWaypointIndex)  某段路径的开始

onPathWaypointFinished(final PathModifier pPathModifier, final IEntity pEntity, final int pWaypointIndex) 某段路径的结束

onPathFinished(...) 整个路径的结束



EaseSineInOut 是一个 IEaseFunction,下一节将重点介绍它。










0 0