📄 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) {
try {
ufoImg = Image.createImage("/ufo.png");
burstImg = Image.createImage("/burst.png");
}catch(Exception e) {}
}
width = ufoImg.getWidth();
height = ufoImg.getHeight();
}
void setAlive(boolean isAlive) {
if(isAlive) {
this.isHit = false;
missileCount = 0;
tickCount = 0;
}
super.setAlive(isAlive);
}
/** 要移动Sprite时所调用的方法 */
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;
}
}
}
}
/** 在描绘Sprite的时候所调用的方法 */
void doDraw(Graphics g) {
if(isAlive) {
if(isHit()) {
//Hit时就描绘爆炸图像
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 + -