編輯:關於Android編程
基礎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。否則會出現空指針異常。
[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型變量,是敵機的屬性,記錄敵機在畫面中出現的時間。
望高手給點意見,看有什麼地方能改進下。
下載其實我個人建議:使用綠色版,以後升級也方便,當然以後換電腦,就是超級方便,解壓完後再打開即用。綠色版也和chrome一樣,分Canary版,Dev版,Beta版,但是
本文來自http://blog.csdn.net/yihongyuelan 轉載請務必注明出處 本文代碼以MTK平台Android 4.4為分析對象,與Google原
費了好久終於下載好了源碼4.4.2 (如果想只是了解下編譯過程 能用現成的最好了)下載好後 接下來開始編譯了我的虛擬機Ubuntu配置 源碼有16g+ (.repo 占了
在OC中,我們可以通過Category 對已有的類進行擴展,這得益於OC的Runtime機制,讓類可以‘動態’的添加方法以及實現。但是,在Cate