?? ufo.java
字號:
import javax.microedition.lcdui.*;
class UFO extends Sprite {
private MyShip myShip;
private int missileCount = 0;
private int direction;
static final int DIRECTION_RIGHT = 0;
static final int DIRECTION_LEFT = 1;
private static Image ufoImg = null;
private static Image burstImg = null;
UFO(MyShip myShip) {
this.myShip = myShip;
if(ufoImg == null) {
//UFO圖像為null時就取得圖像
try {
ufoImg = Image.createImage("/z4.png");
burstImg = Image.createImage("/imgenemybomb.png");
}catch(Exception e) {}
}
//設定寬度與高度
width = ufoImg.getWidth();
height = ufoImg.getHeight();
}
void setAlive(boolean isAlive) {
if(isAlive) {
//由于在變成Alive狀態時會將這個UFO復活,因此要把值予以初始化
this.isHit = false;
missileCount = 0;
tickCount = 0;
}
super.setAlive(isAlive);
}
void doMove() {
if(this.isAlive) { //Alive時
if(isHit()) { //Hit時
tickCount++;
//Tick計數為5的話就會從畫面消失
if(tickCount > 4) {
setAlive(false);
}
}else { //不是Hit時
//針對行進方向來移動UFO
switch(direction) {
case DIRECTION_RIGHT:
x = x + (width / 2) + 1;
break;
case DIRECTION_LEFT:
x = x - (width / 2) - 1;
break;
}
}
}
}
void doDraw(Graphics g) {
if(isAlive) {
//System.out.println(isHit);
if(isHit()) {
//Hit時就描繪爆炸圖像
//System.out.println("BOMB!!!!");
g.drawImage(burstImg, x, y, Graphics.TOP|Graphics.LEFT);
}else {
g.drawImage(ufoImg, x, y, Graphics.TOP|Graphics.LEFT);
}
}
}
void setDirection(int direction) {
this.direction = direction;
}
boolean isDropBomb() {
if(missileCount < 3) {
//飛彈的發射數比3發還少時
//切割出UFO的中央坐標
int center = x + (width /2);
if(center >= myShip.getX() && center <= myShip.getX() + myShip.getWidth()) {
//UFO的中央坐標與自機的X坐標重疊時
//發射飛彈
missileCount++;
return true;
}
}
//處理到這個地方就不發射飛彈
return false;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -