android 人物走动_android 学习: 人物行走

利用接口只暴露出两个函数出来,内部实现其功能.

1.接口

public interface Walker {

//显示人物

void show(Canvas canvas);

//目标坐标

void targetPostion(float x, float y);

}

2. 计算位置

/**

* 计算坐标位置, 方向.

* @author Administrator

*

*/

public class CalcWalk {

private boolean isFinish;

/**

*

* @param x 贴图位置x

* @param y 贴图位置y

* @param step 步长

*/

public CalcWalk(int x, int y, int step ) {

this.x = x;

this.y = y;

this.step = step;

direction = Direction.DOWN;

stepX = stepY = 0;

xTarget = yTarget= 0;

isFinish = true;

}

/**

* 得到目标位置坐标()

* @param f

* @param g

*/

public void targetXY(float f, float g) {

this.xTarget = f;

this.yTarget = g;

isFinish = false;

initData();

}

//得到下一步贴图位置

public void nextPostion() {

//System.out.println("dx="+(xTarget - x) +" sx="+stepX+ " dy="+(yTarget - y)+" sy"+stepY);

float tmX = x, tmY = y;

x = Math.abs(xTarget - x) > Math.abs(stepX) ? x+stepX : x;

y = Math.abs(yTarget - y) > Math.abs(stepY) ? y+stepY : y;

if (tmX == x && tmY == y) {

isFinish = true;

}

}

/**

* 初始化数据,对穿进来的目标位置进行处理.

* 得到偏移量,确定贴图行走的方向

* @param xTarget 目标坐标

* @param yTarget

*/

private void initData() {

float dx = xTarget - x;

float dy = yTarget - y;

float move;

if (Math.abs(dx) > Math.abs(dy)) {

stepX = Mat

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值