編輯:關於Android編程
基礎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。否則會出現空指針異常。
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型變量,是敵機的屬性,記錄敵機在畫面中出現的時間。
望高手給點意見,看有什麼地方能改進下。
今年3月,Google 破天荒提前半年發布了 Android N 開發者預覽版。當然,作為一個不合格的谷粉並沒有第一時間體驗安裝,因為至今仍然能夠回憶起來去年今日此門中(
和其他地圖一樣,都要先去官網注冊成為開發者,然後獲取Key。下面直接上代碼。效果圖: package com.example.gaodemap;i
1、 題外話 相信大家對LayoutInflate都不陌生,特別在ListView的Adapter的getView方法中基本都會出現,使用
復制代碼 代碼如下:<FrameLayout