In actual Android development projects, especially when writing games, we must determine the bullet issue. This is very important. If you do not judge, it will bring poor visual effects to players.
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 ){
// Enemy_bullets [I]. setVisible (false );}
If (enemy_bullets [I]. isVisible () = false ){
// When the bullet is invisible, set it to visible. The position is the corresponding enemy_bullets [I]. setVisible (true); x = enemys [I]. getX ();
Y = enemys [I]. getY ();
// This is the player's direction in the game (top, right, bottom, 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 + 16, y + 5); enemy_bullets [I]. setTransform (Sprite. TRANS_ROT270 );
Break;
Default: break;
}
}
Else {// when the bullet is visible
If (isBullerMeetTank (enemy_bullets [I]. getX (), enemy_bullets [I]. getY (), player. getX (), player. getY ()))
{
// If you encounter a player, the two will not see enemy_bullets [I] at the same time. setVisible (false); 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 whether the bullet can pass through enemy_bullets [I]. setVisible (false );
If (background. getCell (enemy_bullets [I]. getY ()/16, enemy_bullets [I]. getX ()/16 )! = 2 ){
Background. setCell (enemy_bullets [I]. getY ()/16, 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 to move forward or backward. 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 millisecond
}
} Catch (Exception e) {e. printStackTrace ();
} Finally {if (c! = Null) {holder. unlockCanvasAndPost (c );
}
}
}
}