基礎android的飛機類遊戲,與前人一樣,由surfaceView繪製遊戲畫面,另起線程式控制制繪製時間間隔達到動態效果。這裡附上最近自己寫的敵機自動飛行路徑代碼。請大家給點意見。
在敵機管理模組,加入此段代碼。movePingXing記錄該飛機直線軌跡運行時,每次canvas繪製的x、y的偏量值。moveYuanHu記錄該飛機按圓形軌跡運行時,每次canvas繪製的x、y的偏量值。String中,“、”前面得是x方向座標位移量,後面得是y方向座標位移量。
[java]
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 指標異常。
[java]
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時,讓敵機按自己設定的路徑動起來。我這裡設計時只是簡單的直線——圓行——直線飛機路徑。
[java]
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型變數,是敵機的屬性,記錄敵機在畫面中出現的時間。
望高手給點意見,看有什麼地方能改進下。