(转)[AndEngine学习教程] 第4节 制作人物动画

来源:互联网 发布:windows10网络文件系统 编辑:程序博客网 时间:2024/05/07 07:47

1.回顾

    在上一节我们制作一个可以变幻的图形,这节将基于上一节的内容,制作一个行走的人物动画.在这里就称它为战士吧!呵呵!!!!

2.新使用到的资源

   1.RepeatingSpriteBackground 游戏场景中使用到的背景绘制策略,本节中使用到草坪背景,由于背景大小是可以自动调节的,

       不可能绘制一张固定大小的图片来作为背景.一方面占用空间大不说,另一方面背景图片经过缩放后效果也不好


   2.PathModifier 为战士设定行走路径.需要说明的是:这里的路径是两点间的直线路径.比如只需要设定起点和终点.战士就会

      沿着这两点间的直线进行移动

3.源代码陈述

      1.关于RepeatingSpriteBackground  ,这是一种重复绘制背景图片的策略,此类继承于 SpriteBackground ,有两个构造函数:

       

[java] view plaincopy
  1. /** 
  2.      * @param pCameraWidth 
  3.      * @param pCameraHeight 
  4.      * @param pTextureManager 
  5.      * @param pBitmapTextureAtlasSource needs to be a power of two as otherwise the <code>repeating</code> feature doesn't work. 
  6.      */  
  7.     public RepeatingSpriteBackground(final float pCameraWidth, final float pCameraHeight, final TextureManager pTextureManager, final IBitmapTextureAt              lasSource pBitmapTextureAtlasSource, final VertexBufferObjectManager pVertexBufferObjectManager) throws IllegalArgumentException {  
  8.         this(pCameraWidth, pCameraHeight, pTextureManager, pBitmapTextureAtlasSource, 1, pVertexBufferObjectManager);  
  9.     }  
  10.   
  11.     public RepeatingSpriteBackground(final float pCameraWidth, final float pCameraHeight, final TextureManager pTextureManager, final IBitmapTextureAt                 lasSource pBitmapTextureAtlasSource, final float pScale, final VertexBufferObjectManager pVertexBufferObjectManager) throws IllegalArgumentException {  
  12.         super(null);  
  13.   
  14.         this.mScale = pScale;  
  15.         this.mEntity = this.loadSprite(pCameraWidth, pCameraHeight, pTextureManager, pBitmapTextureAtlasSource, pVertexBufferObjectManager);  
  16.     }  
本例子中使用到的是第一种构造函数,实现形式如下:

  

[java] view plaincopy
  1.        private static final int CAMERA_WIDTH=800;  
  2. private static final int CAMERA_HEIGHT=480;  
  3. private Camera mCamera;  
  4. private RepeatingSpriteBackground mGrassBackground;  
  5. private TiledTextureRegion mPlayerTextureRegion;  
  6. private BitmapTextureAtlas mTexture;  
  7.   
  8. @Override  
  9. public EngineOptions onCreateEngineOptions() {  
  10.     // TODO Auto-generated method stub  
  11.     mCamera = new Camera(00, CAMERA_WIDTH, CAMERA_HEIGHT);  
  12.     EngineOptions engineOptions = new EngineOptions(true,ScreenOrientation.LANDSCAPE_SENSOR, new RatioResolutionPolicy(CAMERA_WIDTH,CAMERA_HEIGHT), mCamera);  
  13.     return engineOptions;  
  14. }  
  15.   
  16. @Override  
  17. public void onCreateResources(  
  18.         OnCreateResourcesCallback pOnCreateResourcesCallback)  
  19.         throws Exception {  
  20.     // TODO Auto-generated method stub  
  21.     this.mTexture = new BitmapTextureAtlas(getTextureManager(), 128128, TextureOptions.BILINEAR_PREMULTIPLYALPHA);  
  22.     this.mPlayerTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(mTexture, this,"player.png"0,0,34);  
  23.     this.mGrassBackground = new RepeatingSpriteBackground(CAMERA_WIDTH, CAMERA_HEIGHT, getTextureManager(), AssetBitmapTextureAtlasSource.create(getAssets(), "background_grass.png"), getVertexBufferObjectManager());  
  24.       
  25.     mTexture.load();  
  26.     pOnCreateResourcesCallback.onCreateResourcesFinished();  
  27. }  

然后在onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)函数中实现  

[java] view plaincopy
  1. Scene mScene = new Scene();  
  2. mScene.setBackground(mGrassBackground);  
  3. pOnCreateSceneCallback.onCreateSceneFinished(mScene);  
这样就把重复是的背景放置完成.

3.重点介绍

    本例中用到一个72x128大小,3列4行的精灵战士

    

因此动画的创建方式为:

[java] view plaincopy
  1. final AnimatedSprite  player = new AnimatedSprite(10104864, mPlayerTextureRegion, getVertexBufferObjectManager());  
  2. mScene.attachChild(player);       
这样就把战士般到了屏幕上,但是此时,战士还不会动,为了让它动起来可要花费点力气的.呵呵!.有什么办法呢?就是给战士注册监听实体,

由于本例子要求的是战士按照设定路径移动,自然想到AndEngine中的Path类,定义一个可以围绕整个屏幕的path路线:

[java] view plaincopy
  1. final Path path = new Path(5).to(1010).to(10, CAMERA_HEIGHT - 74).to(CAMERA_WIDTH-58, CAMERA_HEIGHT - 74).to(CAMERA_WIDTH-5810).to(1010);  
  2.           
每个to函数为Path添加一个路径,最终形成一个从屏幕左上角->左下角->右下角->右上角->左上角的封闭路径.

有了以上基础,当然是添加基于Path类修改器啦.

[java] view plaincopy
  1. PathModifier pathModifier = new PathModifier(30.0f, path, new IPathModifierListener(){  
  2.   
  3.             @Override  
  4.             public void onPathStarted(PathModifier pPathModifier,  
  5.                     IEntity pEntity) {  
  6.                 // TODO Auto-generated method stub  
  7.                   
  8.             }  
  9.   
  10.             @Override  
  11.             public void onPathWaypointStarted(PathModifier pPathModifier,  
  12.                     IEntity pEntity, int pWaypointIndex) {  
  13.                 // TODO Auto-generated method stub  
  14.                 switch(pWaypointIndex) {  
  15.                 case 0:  
  16.                     player.animate(new long[]{200200200}, 68true);  
  17.                     break;  
  18.                 case 1:  
  19.                     player.animate(new long[]{200200200}, 35true);  
  20.                     break;  
  21.                 case 2:  
  22.                     player.animate(new long[]{200200200}, 02true);  
  23.                     break;  
  24.                 case 3:  
  25.                     player.animate(new long[]{200200200}, 911true);  
  26.                     break;  
  27.             }  
  28.             }  
  29.   
  30.             @Override  
  31.             public void onPathWaypointFinished(PathModifier pPathModifier,  
  32.                     IEntity pEntity, int pWaypointIndex) {  
  33.                 // TODO Auto-generated method stub  
  34.                   
  35.             }  
  36.   
  37.             @Override  
  38.             public void onPathFinished(PathModifier pPathModifier,  
  39.                     IEntity pEntity) {  
  40.                 // TODO Auto-generated method stub  
  41.                   
  42.             }  
  43.   
  44.           
  45.               
  46.         },EaseSineInOut.getInstance());  
战士行走动作的变化是一个基础动画,所以需要Animate方法.但是战士在每个位置脸朝向不一样的,所以到了每个拐点的开始处都

需要修改动画的脸部朝向,这样就更加逼真点.从图片可以看出他们的对应关系.需要说明的是player.animate()函数:

[java] view plaincopy
  1. public void animate(final long[] pFrameDurations, final int pFirstTileIndex, final int pLastTileIndex, final boolean pLoop) {  
  2.         this.animate(pFrameDurations, pFirstTileIndex, pLastTileIndex, pLoop, null);  
  3.     }  
各个参数的含义为:(战士图片中可以看出:有4组动作,每组动作为3个画面)

   pFrameDurations:连续播放3个画面,三个long数据为每个动画的播放时间间隔,单位为毫秒;

   pFirstTileIndex:动画的其起始列,按照"Z"字形排列计数.
   pLastTileIndex:动画的结束序列,按照"Z"字形排列计数.
  pLoop:是否循环播放

为了让战士行走路径也能循环,在PathModifier的基础上增加LoopEntityModifier

[java] view plaincopy
  1. LoopEntityModifier loopModifier = new LoopEntityModifier(pathModifier, -1);  


有了loopModifier,当然是把它绑定到player上啦

[java] view plaincopy
  1. player.registerEntityModifier(loopModifier);  


至此,整个设计任务完成了,下面看看运行的结果图片:








本例子源代码:http://download.csdn.net/detail/cen616899547/4705911



转自:http://blog.csdn.net/cen616899547/article/details/8131842

原创粉丝点击