在这里给大家分享android游戏教程怎样让动起来,话不多说了,直接进入正题。
一. 准备工作
首先要准备好要使用的人物动作图和地形图。把它分割成16个不同的动作,循环播放同一行的4个不同动作就可以让人物动起来了。
二. 动画实现
按照简单的android游戏框架所述先搭建一个框架,接着编写如下类:
人物类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
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
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
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();
}
通过以上代码实现了逐帧动画的播放,很简单是不是?
现在动画是实现了,但是还不能自己控制,后面会有关于触屏控制和虚拟键盘。
觉得有用的朋友还请多多支持我,后面我还会上传更多有用的源码教程,或者直接访问我主办的棋牌社区查看,查看更多有用源码。