一. 准备工作
二. 动画实现
首先我们要事先准备好要使用的人物动作图和地形图
这张是动作图,把它分割成16个不同的动作,循环播放同一行的4个不同动作就可以让人物动起来了。
按照
简单的android游戏框架——zgf 所述先搭建一个框架,接着编写如下类:
人物类Person主要代码如下:
- private int x;
- private int y;
- private int dist;//行走方向
- private int spd;//行走速度
- private boolean isMove;
- private Bitmap img;
- private int imgXId;//图片的横向切片编号
- private int imgYId;//图片的纵向切片编号
- private int actTime;//动画间隔时间
- public Person(Context con,int x,int y) {
- //人物中心点
- this.x=x;
- this.y=y;
- dist=Constants.DOWN;
- isMove=false;
- spd=2;
- img=GraphicUtil.ReadBitMap(con, R.drawable.bb);
- imgXId=0;
- imgYId=0;
- }
- public void draw(Canvas c, Paint p) {
- //图片的左上角坐标
- int ix=x-Constants.PERSON_WIDTH/2;
- int iy=y-Constants.PERSON_HEIGHT/2;
- GraphicUtil.DrawClipById(c, p, Constants.PERSON_WIDTH, Constants.PERSON_HEIGHT,
- img, imgXId, imgYId, ix, iy);
- }
- public void act() {
- if(isMove) {
- if(actTime<Constants.ACT_TIME)
- actTime++;
- else if(actTime>=Constants.ACT_TIME)
- actTime=0;
- imgXId=actTime*actTime/Constants.ACT_TIME;
- switch(this.dist) {
- case Constants.UP:
- y-=spd;
- imgYId=3;
- break;
- case Constants.DOWN:
- y+=spd;
- imgYId=0;
- break;
- case Constants.RIGHT:
- x+=spd;
- imgYId=2;
- break;
- case Constants.LEFT:
- x-=spd;
- imgYId=1;
- break;
- }
- //控制行走范围不超出屏幕
- x=x<0?0:x;
- x=x>GameAct.GetDispW()?GameAct.GetDispW():x;
- y=y<0?0:y;
- y=y>GameAct.GetDispH()?GameAct.GetDispH():y;
- }
- }
- public void move(int dist) {
- this.dist=dist;
- this.isMove=true;
- }
- public void stop() {
- this.isMove=false;
- actTime=0;
- }
打开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类,主要代码如下:
- private Bitmap texture;
- public BackMap(Context con) {
- texture=GraphicUtil.ReadBitMap(con, R.drawable.texture);
- }
- public void draw(Canvas c, Paint p) {
- int imgX=0;
- int imgY=0;
- int wc=GameAct.GetDispW()/Constants.TILE_WIDTH;
- int hc=GameAct.GetDispH()/Constants.TILE_WIDTH;
- //将图像铺满屏幕
- for(int i=0;i<wc;i++) {
- for(int j=0;j<hc;j++) {
- int x=i*Constants.TILE_WIDTH;
- int y=j*Constants.TILE_WIDTH;
- GraphicUtil.DrawClipById(c, p, Constants.TILE_WIDTH, Constants.TILE_WIDTH,
- texture, imgX, imgY, x, y);
- }
- }
- }
然后把它们在一个类里面实例化对象:
- BackMap bm;
- Person per;
- int time=0;
- public Scene(Context con) {
- init(con);
- }
- private void init(Context con) {
- bm=new BackMap(con);
- int px=GameAct.GetDispW()-GameAct.GetDispW()/6;
- int py=GameAct.GetDispH()/6;
- per=new Person(con,px,py);
- }
- public void draw(Canvas c,Paint p) {
- bm.draw(c, p);
- per.draw(c, p);
- }
- public void act() {
- //人物逆时针行走
- int dist=Constants.NONE;
- int timer=100;
- if(time<timer)
- dist=Constants.LEFT;
- else if(time<2*timer)
- dist=Constants.DOWN;
- else if(time<3*timer)
- dist=Constants.RIGHT;
- else if(time<4*timer)
- dist=Constants.UP;
- else if(time>=4*timer)
- time=0;
- time++;
- per.move(dist);
- per.act();
- }
最后编写Game.java:
- private Scene scene;
- public Game(Context con) {
- super(con);
- scene=new Scene(con);
- }
- @Override
- public void run(Context con, Canvas c, Paint p) {
- scene.draw(c,p);
- scene.act();
- }
完成,运行效果如下:
通过以上代码实现了逐帧动画的播放,很简单是不是?
现在动画是实现了,但是还不能自己控制,下一篇的内容将会是关于触屏控制和虚拟键盘。