?? ufo.java
字號(hào):
/*
* UFO.java
*
* Copyright 2001 SkyArts. All Rights Reserved.
*/
import javax.microedition.lcdui.*;
/**
* UFO類
*
* @author Hideki Yonekawa
* @version 1.0
*/
class UFO extends Sprite {
/** 儲(chǔ)存自機(jī)對(duì)象的變量 */
private MyShip myShip;
/** 儲(chǔ)存UFO圖像的類變量 */
private int missileCount = 0;
/** 儲(chǔ)存UFO爆炸圖像的類變量 */
private int direction;
/** 代表向右的常量 */
static final int DIRECTION_RIGHT = 0;
/** 代表向左的常量 */
static final int DIRECTION_LEFT = 1;
/** 儲(chǔ)存UFO圖像的類變量 */
private static Image ufoImg = null;
/** 儲(chǔ)存UFO爆炸圖像的類變量 */
private static Image burstImg = null;
/**
* 構(gòu)造函數(shù)
* @param myShip 自機(jī)對(duì)象
*/
UFO(MyShip myShip) {
this.myShip = myShip;
if(ufoImg == null) {
//UFO圖像為null時(shí)就取得圖像
try {
ufoImg = Image.createImage("/ufo.png");
burstImg = Image.createImage("/burst.png");
}catch(Exception e) {}
}
//設(shè)定寬度與高度
width = ufoImg.getWidth();
height = ufoImg.getHeight();
}
/**
* 設(shè)定Alive狀態(tài)的方法
* @param isAlive 是Alive狀態(tài)就為true,不是Alive狀態(tài)就為false
*/
void setAlive(boolean isAlive) {
if(isAlive) {
//由于在變成Alive狀態(tài)時(shí)會(huì)將這個(gè)UFO復(fù)活,因此要把值予以初始化
this.isHit = false;
missileCount = 0;
tickCount = 0;
}
super.setAlive(isAlive);
}
/** 要移動(dòng)Sprite時(shí)所調(diào)用的方法 */
void doMove() {
if(this.isAlive) { //Alive時(shí)
if(isHit()) { //Hit時(shí)
tickCount++;
//Tick計(jì)數(shù)為5的話就會(huì)從畫面消失
if(tickCount > 4) {
setAlive(false);
}
}else { //不是Hit時(shí)
//針對(duì)行進(jìn)方向來(lái)移動(dòng)UFO
switch(direction) {
case DIRECTION_RIGHT:
x = x + (width / 2) + 1;
break;
case DIRECTION_LEFT:
x = x - (width / 2) - 1;
break;
}
}
}
}
/** 在描繪Sprite的時(shí)候所調(diào)用的方法 */
void doDraw(Graphics g) {
if(isAlive) {
if(isHit()) {
//Hit時(shí)就描繪爆炸圖像
g.drawImage(burstImg, x, y, Graphics.TOP|Graphics.LEFT);
}else {
g.drawImage(ufoImg, x, y, Graphics.TOP|Graphics.LEFT);
}
}
}
/**
* 設(shè)定UFO方向的方法
* @param direction DIRECTION_RIGHT丄DIRECTION_LEFT偺偳偪傜偐
*/
void setDirection(int direction) {
this.direction = direction;
}
/**
* 用來(lái)判定UFO是否發(fā)出飛彈的方法
* @return boolean 發(fā)射飛彈時(shí)為true、沒有發(fā)射時(shí)為false
*/
boolean isDropBomb() {
if(missileCount < 3) {
//飛彈的發(fā)射數(shù)比3發(fā)還少時(shí)
//切割出UFO的中央坐標(biāo)
int center = x + (width /2);
if(center >= myShip.getX() && center <= myShip.getX() + myShip.getWidth()) {
//UFO的中央坐標(biāo)與自機(jī)的X坐標(biāo)重疊時(shí)
//發(fā)射飛彈
missileCount++;
return true;
}
}
//處理到這個(gè)地方就不發(fā)射飛彈
return false;
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -