?? torpedo.java
字號:
package mysubmarine;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;
public class Torpedo extends MySprite implements Collidion {
private boolean bLive = false; //生命狀態
private byte troop = 0; //敵我識別標識
private int levelState = 0; //速度狀態
private boolean flagUp = true; //當前圖形方向. true為向上, false向下
private int yStep = 15; //物體在y軸移動時的步進長度
private int ySpeed = 0; //物體在y軸移動時的速度
/** 初始化魚雷圖層
* @param subCanvas Canvas類
* @param image 魚雷圖片
* @param xPosition 初始化坐標起點
* @param yPosition 初始化坐標終點
* @param troop 敵我標識
* @param levelState 速度水平
* @param directionState 初始化方向
*/
public Torpedo(SubCanvas subCanvas, Image image,
int xPos, int yPos, byte troop, int levelState,
boolean bUp) {
super(image);
this.subCanvas = subCanvas;
//設定魚雷出現位置
this.x = xPos;
this.y = yPos;
this.setPosition(xPos, yPos);
this.levelState = levelState;
this.bLive = true;
this.troop = troop;
this.flagUp = bUp;
//根據初始方向判斷圖形是否需要轉向, 并設定速度方向
if (flagUp) {
setTransform(Sprite.TRANS_ROT270);
ySpeed = ( -1) * this.yStep;
} else {
setTransform(Sprite.TRANS_ROT90);
ySpeed = this.yStep;
}
}
/**
* 觸發函數
* 在生存狀態中, 根據速度水平(循環次數)移動魚雷
*/
public void tick() {
int i = 0;
//當 當前生命狀態為真, 并且速度狀態尚沒有結束(還可以繼續運行時)
while (bLive && (i < this.levelState)) {
movePosition();
i++;
}
}
/**
* 魚雷的位置移動函數
* 以一個步長為單位移動圖形, 如果超出邊界則置生存狀態為false
* 移動之后檢查碰撞事件, 如果需要消失也同樣置生存狀態為false
*/
public void movePosition() {
if (this.troop == SubCanvas.PLAYER) {
this.y = y + this.ySpeed;
} else {
this.y = y - this.ySpeed;
}
//確保圖形在游戲區域
if (y > SubCanvas.mainHeight - h) {
this.bLive = false;
}
if ((y - h/2 ) < SubCanvas.SHIP_HEIGHT) {
this.bLive = false;
}
this.setPosition(x, y);
//處理碰撞事件
checkCollidion();
}
/**
* 碰撞檢測函數
* 如果發生了碰撞,魚雷的生命周期結束
*/
public void checkCollidion() {
Sprite ship = null;
Sprite sub = null;
if (this.troop == SubCanvas.PLAYER) {
//當魚雷是玩家潛艇釋放時
for (int i = 0; i < subCanvas.enemyVector.size(); i++) {
sub = (Sprite) subCanvas.enemyVector.elementAt(i);
if (collidesWith(sub, false)) {
this.bLive = false;
}
}
} else {
//當魚雷是敵人潛艇釋放時
ship = subCanvas.getShip();
if (collidesWith(ship, false)) {
this.bLive = false;
}
}
sub = null;
ship = null;
}
/**
* 獲取魚雷的敵我屬性
*/
public byte getTroop() {
return this.troop;
}
/**
* 獲取魚雷的生命狀態
*/
public boolean getLifeState() {
return this.bLive;
}
/**
* 獲取當前的游戲等級信息
*/
public int getLevel() {
return this.levelState;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -