在做安卓開發實際項目當中,尤其是在編寫游戲時,我們一定要判斷一下子彈的問題,應為這個是非常重要的。如果不判斷的話,就會給玩家視覺上帶來不好的效果。
Java代碼:
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) {
// 子彈不可見時,設為可見,位置為對應敵人發射處 enemy_bullets[i].setVisible(true); x = enemys[i].getX();
y = enemys[i].getY();
//這個是玩家在游戲當中的方向(上、右、下、左) 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 { // 子彈可見時
if (isBullerMeetTank(enemy_bullets[i].getX(), enemy_bullets[i].getY(), player.getX(),player.getY()))
{
// 如果遇上玩家,兩者同時不見 enemy_bullets[i].setVisible(false); player.setVisible(false); explodes[4].setVisible(true);//爆炸 explodes[4].setPosition(player.getX(), player.getY()); }
if (enemy_bullets[i].isVisible()&& ! canBulletPass(enemy_bullets[i].getX(), enemy_bullets[i].getY()))
{
// 判斷子彈是否能穿過 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()) {
// 延原來方向move前進或後退 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);//時間為毫秒
}
} catch (Exception e) { e.printStackTrace();
} finally { if (c != null) { holder.unlockCanvasAndPost(c);
}
}
}
}