In the development of the actual Android project, especially in the writing game, we must judge the problem of a bullet, it is very important. If not judged, it will give the player visually bad effect.
Java code:
for (int i = 0; i < enemy_bullets.length; i++)
{
Enemy_bullets[i].paint (c);
if (Enemy_bullets[i].getx () < 0| | Enemy_bullets[i].getx ()-5 > Max_width
|| Enemy_bullets[i].gety () < 0| | Enemy_bullets[i].gety ()-5 > Max_height) {
A bullet crosses the border to process enemy_bullets[i].setvisible (false); }
if (enemy_bullets[i].isvisible () = = False) {
When the bullet is not visible, it is set to be visible, and the position is corresponding to the enemy launch Enemy_bullets[i].setvisible (true); x = Enemys[i].getx ();
y = enemys[i].gety ();
This is the player in the game in the direction (up, right, down, left) switch (Enemys[i].gettransform ()) {
Case Sprite.trans_none:enemy_bullets[i].setposition (x + 5, y + 16); Enemy_bullets[i].settransform (Sprite.trans_none);
Break
Case Sprite.trans_rot90:enemy_bullets[i].setposition (x-6, y + 5); Enemy_bullets[i].settransform (SPRITE.TRANS_ROT90);
Break
Case Sprite.trans_rot180:enemy_bullets[i].setposition (x + 5, y-6); Enemy_bullets[i].settransform (sprite.trans_rot180);
Break
Case Sprite.trans_rot270:enemy_bullets[i].setposition (x +, y + 5); Enemy_bullets[i].settransform (sprite.trans_rot270);
Break
Default:break;
}
}
else {//when bullets are visible
if (Isbullermeettank (Enemy_bullets[i].getx (), Enemy_bullets[i].gety (), Player.getx (), Player.gety ()))
{
If the player is met, both enemy_bullets[i].setvisible (false) are not seen; Player.setvisible (FALSE); Explodes[4].setvisible (TRUE);//Explosion explodes[4].setposition (Player.getx (), player.gety ()); }
if (enemy_bullets[i].isvisible () &&! Canbulletpass (Enemy_bullets[i].getx (), Enemy_bullets[i].gety ()))
{
Determine if the bullet can pass through enemy_bullets[i].setvisible (false);
if (Background.getcell (Enemy_bullets[i].gety ()/Enemy_bullets[i].getx ()/16)!= 2) {
Background.setcell (Enemy_bullets[i].gety ()/Enemy_bullets[i].getx ()/16, 0); Map[enemy_bullets[i].gety ()/16][enemy_bullets[i].getx ()/16] = 0; }
}
Switch (Enemy_bullets[i].gettransform ()) {
Extend the original direction move forward or back case sprite.trans_none:enemy_bullets[i].move (0, 16); Break
Case Sprite.trans_rot90:enemy_bullets[i].move (-16, 0); Break
Case Sprite.trans_rot180:enemy_bullets[i].move (0,-16); Break
Case Sprite.trans_rot270:enemy_bullets[i].move (16, 0); Break
Default:break; }
}
}
Player.paint (c);
Thread.Sleep (200);//Time is milliseconds
}
catch (Exception e) {e.printstacktrace ();
finally {if (c!= null) {holder.unlockcanvasandpost (c);
}
}
}
}