Android 游戏教程:让人物动起来-网摘

今天我们用zgf框架做一个人物运动的demo,这是个简单的例子代码不多功能简陋,通过这个例子可以学会用zgf框架加载游戏图片和实现逐帧动画,通过这些就可以让一张图片变成一段简单的动画。

一. 准备工作

     首先我们要事先准备好要使用的人物动作图和地形图
 
 
 
这张是动作图,把它分割成16个不同的动作,循环播放同一行的4个不同动作就可以让人物动起来了。
 

二. 动画实现

     按照 简单的android游戏框架——zgf 所述先搭建一个框架,接着编写如下类:
     人物类Person主要代码如下:
  1. private int x;  
  2.     private int y;  
  3.     private int dist;//行走方向  
  4.     private int spd;//行走速度  
  5.     private boolean isMove;  
  6.     private Bitmap img;  
  7.     private int imgXId;//图片的横向切片编号  
  8.     private int imgYId;//图片的纵向切片编号  
  9.     private int actTime;//动画间隔时间  
  10.       
  11.     public Person(Context con,int x,int y) {  
  12.         //人物中心点  
  13.         this.x=x;  
  14.         this.y=y;  
  15.           
  16.         dist=Constants.DOWN;  
  17.         isMove=false;  
  18.         spd=2;  
  19.         img=GraphicUtil.ReadBitMap(con, R.drawable.bb);  
  20.         imgXId=0;  
  21.         imgYId=0;  
  22.     }  
  23.       
  24.     public void draw(Canvas c, Paint p) {  
  25.         //图片的左上角坐标  
  26.         int ix=x-Constants.PERSON_WIDTH/2;  
  27.         int iy=y-Constants.PERSON_HEIGHT/2;  
  28.           
  29.         GraphicUtil.DrawClipById(c, p, Constants.PERSON_WIDTH, Constants.PERSON_HEIGHT,  
  30.                 img, imgXId, imgYId, ix, iy);  
  31.     }  
  32.       
  33.     public void act() {  
  34.         if(isMove) {  
  35.             if(actTime<Constants.ACT_TIME)   
  36.                 actTime++;  
  37.             else if(actTime>=Constants.ACT_TIME)  
  38.                 actTime=0;  
  39.             imgXId=actTime*actTime/Constants.ACT_TIME;  
  40.           
  41.             switch(this.dist) {  
  42.                 case Constants.UP:  
  43.                     y-=spd;  
  44.                     imgYId=3;  
  45.                     break;  
  46.                 case Constants.DOWN:  
  47.                     y+=spd;  
  48.                     imgYId=0;  
  49.                     break;  
  50.                 case Constants.RIGHT:  
  51.                     x+=spd;  
  52.                     imgYId=2;  
  53.                     break;  
  54.                 case Constants.LEFT:  
  55.                     x-=spd;  
  56.                     imgYId=1;  
  57.                     break;  
  58.             }  
  59.               
  60.             //控制行走范围不超出屏幕  
  61.             x=x<0?0:x;  
  62.             x=x>GameAct.GetDispW()?GameAct.GetDispW():x;  
  63.             y=y<0?0:y;  
  64.             y=y>GameAct.GetDispH()?GameAct.GetDispH():y;  
  65.         }  
  66.     }  
  67.       
  68.     public void move(int dist) {  
  69.         this.dist=dist;  
  70.         this.isMove=true;  
  71.     }  
  72.       
  73.     public void stop() {  
  74.         this.isMove=false;  
  75.         actTime=0;  
  76.     }  
 
 
 打开api文档,找到 com.zxx43.commen.graphic.GraphicUtil 可以看到  ReadBitMap(java.lang.String src) 和 ReadBitMap(Context context, int resId) 两个方法,通过这两个方法可以获得bitmap对象,前一个方法是根据文件路径加载图片,后一个方法是根据资源id加载图片。
   DrawClipById(Canvas c, Paint p, int cw, int ch, Bitmap file, int idX, int idY, int x, int y) 方法绘制切片,cw和ch是图片的切片宽度和高度,idX和idY是图像的横向和纵向编号。

    
 接着绘制背景,新建BackMap类,主要代码如下:
 
  1. private Bitmap texture;  
  2.       
  3.     public BackMap(Context con) {  
  4.         texture=GraphicUtil.ReadBitMap(con, R.drawable.texture);  
  5.     }  
  6.       
  7.     public void draw(Canvas c, Paint p) {  
  8.         int imgX=0;  
  9.         int imgY=0;  
  10.         int wc=GameAct.GetDispW()/Constants.TILE_WIDTH;  
  11.         int hc=GameAct.GetDispH()/Constants.TILE_WIDTH;  
  12.         //将图像铺满屏幕  
  13.         for(int i=0;i<wc;i++) {  
  14.             for(int j=0;j<hc;j++) {  
  15.                 int x=i*Constants.TILE_WIDTH;  
  16.                 int y=j*Constants.TILE_WIDTH;  
  17.                 GraphicUtil.DrawClipById(c, p, Constants.TILE_WIDTH, Constants.TILE_WIDTH,   
  18.                         texture, imgX, imgY, x, y);  
  19.             }  
  20.         }  
  21.     }  

 

 然后把它们在一个类里面实例化对象:

  1. BackMap bm;  
  2.     Person per;  
  3.     int time=0;  
  4.       
  5.     public Scene(Context con) {  
  6.         init(con);  
  7.     }  
  8.       
  9.     private void init(Context con) {  
  10.         bm=new BackMap(con);  
  11.         int px=GameAct.GetDispW()-GameAct.GetDispW()/6;  
  12.         int py=GameAct.GetDispH()/6;  
  13.         per=new Person(con,px,py);  
  14.     }  
  15.       
  16.     public void draw(Canvas c,Paint p) {  
  17.         bm.draw(c, p);  
  18.         per.draw(c, p);  
  19.     }  
  20.       
  21.     public void act() {  
  22.         //人物逆时针行走  
  23.         int dist=Constants.NONE;  
  24.         int timer=100;  
  25.         if(time<timer)  
  26.             dist=Constants.LEFT;  
  27.         else if(time<2*timer)  
  28.             dist=Constants.DOWN;  
  29.         else if(time<3*timer)  
  30.             dist=Constants.RIGHT;  
  31.         else if(time<4*timer)  
  32.             dist=Constants.UP;  
  33.         else if(time>=4*timer)  
  34.             time=0;  
  35.         time++;  
  36.               
  37.         per.move(dist);  
  38.         per.act();  
  39.     }  

 

       最后编写Game.java:

  1. private Scene scene;  
  2.       
  3.     public Game(Context con) {  
  4.         super(con);  
  5.         scene=new Scene(con);  
  6.     }  
  7.       
  8.     @Override  
  9.     public void run(Context con, Canvas c, Paint p) {  
  10.         scene.draw(c,p);  
  11.         scene.act();  
  12.     }  

  完成,运行效果如下:

 

 

通过以上代码实现了逐帧动画的播放,很简单是不是?

     源代码下载
     现在动画是实现了,但是还不能自己控制,下一篇的内容将会是关于触屏控制和虚拟键盘。
 

猜你喜欢

转载自yaolunwei.iteye.com/blog/1972617