android飛機遊戲敵機移動路徑

來源:互聯網
上載者:User

基礎android的飛機類遊戲,與前人一樣,由surfaceView繪製遊戲畫面,另起線程式控制制繪製時間間隔達到動態效果。這裡附上最近自己寫的敵機自動飛行路徑代碼。請大家給點意見。

        在敵機管理模組,加入此段代碼。movePingXing記錄該飛機直線軌跡運行時,每次canvas繪製的x、y的偏量值。moveYuanHu記錄該飛機按圓形軌跡運行時,每次canvas繪製的x、y的偏量值。String中,“、”前面得是x方向座標位移量,後面得是y方向座標位移量。

 

 

private static String[] movePingXing = { 5 + "," + 0, 5 + "," + 0, 5 + "," + 0,             5 + "," + 0, 5 + "," + 0, 5 + "," + 0, 5 + "," + 0,             5 + "," + 0, 5 + "," + 0, 5 + "," + 0, 5 + "," + 0,             5 + "," + 0, 5 + "," + 0, 5 + "," + 0, 5 + "," + 0,             5 + "," + 0, 5 + "," + 0, 5 + "," + 0, 5 + "," + 0, 5 + "," + 0 };     private static String[] moveYuanHu = { 5 + "," + 1, 5 + "," + 1,         4 + "," + 2, 4 + "," + 2,          3 + "," + 3,3 + "," + 3,             2 + "," + 4, 2 + "," + 4,             1 + "," + 5, 1 + "," + 5,             -1 + "," + 5,-1 + "," + 5,             -2 + "," + 4,-2 + "," + 4,             -3 + "," + 3, -3 + "," + 3,             -4 + "," + 2, -4 + "," + 2,             -5 + "," + 1, -5 + "," + 1,             -5 + "," + -1,-5 + "," + -1,             -4 + "," + -2,-4 + "," + -2,             -3 + "," + -3,-3 + "," + -3,             -2 + "," + -4,-2 + "," + -4,             -1 + "," + -5,-1 + "," + -5,             1 + "," + -5,1 + "," + -5,             2 + "," + -4,2 + "," + -4,             3 + "," + -3,3 + "," + -3,             4 + "," + -2,4 + "," + -2,             5 + "," + -1,5 + "," + -1}; private static String[] movePingXing = { 5 + "," + 0, 5 + "," + 0, 5 + "," + 0,   5 + "," + 0, 5 + "," + 0, 5 + "," + 0, 5 + "," + 0,   5 + "," + 0, 5 + "," + 0, 5 + "," + 0, 5 + "," + 0,   5 + "," + 0, 5 + "," + 0, 5 + "," + 0, 5 + "," + 0,   5 + "," + 0, 5 + "," + 0, 5 + "," + 0, 5 + "," + 0, 5 + "," + 0 }; private static String[] moveYuanHu = { 5 + "," + 1, 5 + "," + 1,  4 + "," + 2, 4 + "," + 2,  3 + "," + 3,3 + "," + 3,   2 + "," + 4, 2 + "," + 4,   1 + "," + 5, 1 + "," + 5,   -1 + "," + 5,-1 + "," + 5,   -2 + "," + 4,-2 + "," + 4,   -3 + "," + 3, -3 + "," + 3,   -4 + "," + 2, -4 + "," + 2,   -5 + "," + 1, -5 + "," + 1,   -5 + "," + -1,-5 + "," + -1,   -4 + "," + -2,-4 + "," + -2,   -3 + "," + -3,-3 + "," + -3,   -2 + "," + -4,-2 + "," + -4,   -1 + "," + -5,-1 + "," + -5,   1 + "," + -5,1 + "," + -5,   2 + "," + -4,2 + "," + -4,   3 + "," + -3,3 + "," + -3,   4 + "," + -2,4 + "," + -2,   5 + "," + -1,5 + "," + -1}; 

然後給出路徑添加方法,把這些座標位移量加入到moveList1。moveList1裡的內容一定要充足,必須保證在每次canvas繪製時,飛機都能得到一個有效路徑的String。否則會出現null 指標異常。

 

 

public static boolean initMoveList1() {         addPingXing();         addYuanHu();         addPingXing();         addPingXing();         addPingXing();         return true;     }     public static void addPingXing(){         Map<String, String> map;         for (int i = 0; i < movePingXing.length; i++) {             map = new HashMap<String, String>();             map.put("way", movePingXing[i]);             moveList1.add(map);         }     }          public static void addYuanHu(){         Map<String, String> map;         for (int i = 0; i < moveYuanHu.length; i++) {             map = new HashMap<String, String>();             map.put("way", moveYuanHu[i]);             moveList1.add(map);         }     } public static boolean initMoveList1() {  addPingXing();  addYuanHu();  addPingXing();  addPingXing();  addPingXing();  return true; } public static void addPingXing(){  Map<String, String> map;  for (int i = 0; i < movePingXing.length; i++) {   map = new HashMap<String, String>();   map.put("way", movePingXing[i]);   moveList1.add(map);  } }  public static void addYuanHu(){  Map<String, String> map;  for (int i = 0; i < moveYuanHu.length; i++) {   map = new HashMap<String, String>();   map.put("way", moveYuanHu[i]);   moveList1.add(map);  } } 

     調用initMoveList1()方法後,敵機管理類就可獲得一個記錄敵機飛行軌跡的位移量的ArrayList了。

        在敵機移動的時候,插入下面代碼,實現每次繪製canvas時,讓敵機按自己設定的路徑動起來。我這裡設計時只是簡單的直線——圓行——直線飛機路徑。

 

 

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

     上面currentSecond是一個int型變數,是敵機的屬性,記錄敵機在畫面中出現的時間。

        望高手給點意見,看有什麼地方能改進下。

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.