Similar to previous players, basic android aircraft Games use surfaceView to draw game images and control the draw interval to achieve dynamic effects. The self-written automatic flight path code of the enemy plane is attached here. Please give us some comments.
Add this segment to the enemy plane management module. MovePingXing records the deviation values of x and y drawn each time when the aircraft runs in a straight line. MoveYuanHu records the deviation values of x and y drawn by the canvas each time when the plane runs on a circular track. In String, "," must be followed by the x-direction coordinate offset, followed by the y-direction coordinate offset.
[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,
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,
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,
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,
4 + "," +-2, 4 + "," +-2,
5 + "," +-1, 5 + "," +-1 };
Then, the path adding method is provided to add these coordinate offsets to movelist1. The content in moveList1 must be sufficient. Each time the canvas is drawn, the plane can get a valid String path. Otherwise, a null pointer exception occurs.
[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 );
}
}
After the initMoveList1 () method is called, the enemy management class can obtain an ArrayList that records the offset of the flight track of the enemy.
When the enemy machine moves, insert the following code to let the enemy machine move according to its own path every time the canvas is drawn. Here I design a simple straight line-circular line-linear aircraft path.
[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]);
The above currentSecond is an int variable, which is an attribute of the enemy's machine and records the time when the enemy's machine appeared on the screen.
Hope you can give us some comments and see what can be improved.