首页 > 移动开发 > android飞机游戏敌机移动路径是什么样的

android飞机游戏敌机移动路径是什么样的

基础android的飞机类游戏,与前人一样,由surfaceView绘制游戏画面,另起线程控制绘制时间间隔达到动态效果。这里附上最近自己写的敌机自动飞行路径代码。请大家给点意见。

在敌机管理模块,加入此段代碼。movePingXing记录该飞机直线轨迹运行时,每次canvas绘制的x、y的偏量值。moveYuanHu记录该飞机按圆形轨迹运行时,每次canvas绘制的x、y的偏量值。String中,“、”前面得是x方向坐标偏移量,后面得是y方向坐标偏移量。

 

[java][/java] view plaincopy

  1. private static String[] movePingXing = { 5 + "," + 0, 5 + "," + 0, 5 + "," + 0,
  2.             5 + "," + 0, 5 + "," + 0, 5 + "," + 0, 5 + "," + 0,
  3.             5 + "," + 0, 5 + "," + 0, 5 + "," + 0, 5 + "," + 0,
  4.             5 + "," + 0, 5 + "," + 0, 5 + "," + 0, 5 + "," + 0,
  5.             5 + "," + 0, 5 + "," + 0, 5 + "," + 0, 5 + "," + 0, 5 + "," + 0 };
  6.     private static String[] moveYuanHu = { 5 + "," + 1, 5 + "," + 1,
  7.         4 + "," + 2, 4 + "," + 2,
  8.         3 + "," + 3,3 + "," + 3,
  9.             2 + "," + 4, 2 + "," + 4,
  10.             1 + "," + 5, 1 + "," + 5,
  11.             -1 + "," + 5,-1 + "," + 5,
  12.             -2 + "," + 4,-2 + "," + 4,
  13.             -3 + "," + 3, -3 + "," + 3,
  14.             -4 + "," + 2, -4 + "," + 2,
  15.             -5 + "," + 1, -5 + "," + 1,
  16.             -5 + "," + -1,-5 + "," + -1,
  17.             -4 + "," + -2,-4 + "," + -2,
  18.             -3 + "," + -3,-3 + "," + -3,
  19.             -2 + "," + -4,-2 + "," + -4,
  20.             -1 + "," + -5,-1 + "," + -5,
  21.             1 + "," + -5,1 + "," + -5,
  22.             2 + "," + -4,2 + "," + -4,
  23.             3 + "," + -3,3 + "," + -3,
  24.             4 + "," + -2,4 + "," + -2,
  25.             5 + "," + -1,5 + "," + -1};

 

 

然后给出路径添加方法,把这些坐标偏移量加入到moveList1。moveList1里的内容一定要充足,必须保证在每次canvas绘制时,飞机都能得到一个有效路径的String。否则会出现空指针异常。

 

[java][/java] view plaincopy

  1. public static boolean initMoveList1() {
  2.         addPingXing();
  3.         addYuanHu();
  4.         addPingXing();
  5.         addPingXing();
  6.         addPingXing();
  7.         return true;
  8.     }
  9.     public static void addPingXing(){
  10.         Map<String, String> map;
  11.         for (int i = 0; i < movePingXing.length; i++) {
  12.             map = new HashMap<String, String>();
  13.             map.put("way", movePingXing[i]);
  14.             moveList1.add(map);
  15.         }
  16.     }
  17.     public static void addYuanHu(){
  18.         Map<String, String> map;
  19.         for (int i = 0; i < moveYuanHu.length; i++) {
  20.             map = new HashMap<String, String>();
  21.             map.put("way", moveYuanHu[i]);
  22.             moveList1.add(map);
  23.         }
  24.     }

 

 

调用initMoveList1()方法后,敌机管理类就可获得一个记录敌机飞行轨迹的偏移量的ArrayList了。

在敌机移动的时候,插入下面代码,实现每次绘制canvas时,让敌机按自己设定的路径动起来。我这里设计时只是简单的直线——圆行——直线飞机路径。

 

[java][/java] view plaincopy

  1. Map<String, String> map= moveList1.get(enemy.getCurrentSecond());
  2.                 String moveWay = map.get("way");
  3.                 String[] zuobiao= moveWay.split(",");
  4.                 enemy.x += Integer.parseInt(zuobiao[0]);
  5.                 enemy.y += Integer.parseInt(zuobiao[1]);

 

上面currentSecond是一个int型变量,是敌机的属性,记录敌机在画面中出现的时间。

望高手给点意见,看有什么地方能改进下。


本文固定链接: http://www.devba.com/index.php/archives/1458.html | 开发吧

报歉!评论已关闭.