li b.
今天我们用zgf框架做一个人物运动的demo,这是个简单的例子代码不多功能简陋,通过这个例子可以学会用zgf框架加载游戏图片和实现逐帧动画,通过这些就可以让一张图片变成一段简单的动画。
一. 准备工作![Android <wbr>游戏教程:让人物动起来](https://i-blog.csdnimg.cn/blog_migrate/a4c26d1e5885305701be709a3d33442f.gif)
这张是动作图,把它分割成16个不同的动作,循环播放同一行的4个不同动作就可以让人物动起来了。
- 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 -
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<<SPAN>0?0:x; -
x=x>GameAct.GetDispW()?GameAct.GetDispW():x; -
y=y<<SPAN>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; -
}
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是图像的横向和纵向编号。
- 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 -
for(int j=0;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 -
dist=Constants.LEFT; -
else if(time<</SPAN>2*timer) -
dist=Constants.DOWN; -
else if(time<</SPAN>3*timer) -
dist=Constants.RIGHT; -
else if(time<</SPAN>4*timer) -
dist=Constants.UP; -
else if(time>=4*timer) -
time=0; -
time++; -
-
per.move(dist); -
per.act(); -
}
- 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(); -
}
通过以上代码实现了逐帧动画的播放,很简单是不是?