?? enemysub.java
字號:
/*
* Author: Huang ye(www.hyweb.net)
* 代碼開源, 引用請注明出處
*
* 創建日期 2005-3-1
*
* TODO 要更改此生成的文件的模板,請轉至
* 窗口 - 首選項 - Java - 代碼樣式 - 代碼模板
*/
package net.hyweb;
import javax.microedition.lcdui.game.Sprite;
//import java.util.*;
import javax.microedition.lcdui.*;
/** * @author user
*
* TODO 要更改此生成的類型注釋的模板,請轉至
* 窗口 - 首選項 - Java - 代碼樣式 - 代碼模板 */
public class EnemySub extends Sprite implements SubObject {
/**
*
* @uml.property name="subCanvas"
* @uml.associationEnd multiplicity="(0 1)"
*/
private SubCanvas subCanvas;
// private Vector collideables = new Vector();
private boolean lifeState = false; //生命狀態
private int troop = 0; //敵我識別標識
private int levelState = 0; //速度狀態
private int xStep = 2;
private int yStep = 0;
private int vx = 0;
private int x = getX();
private int y = getY();
private int w = getWidth();
private int h = getHeight();
/** 初始化敵人圖層Sprite
*
* @param subCanvas
* @param image
* @param xPosition 敵人潛艇初始橫坐標
* @param yPosition 敵人潛艇初始縱坐標
* @param troop 敵我標識
* @param levelState 難度水平
*/
public EnemySub(SubCanvas subCanvas, Image image, int xPosition, int yPosition, int levelState){
super(image);
this.subCanvas = subCanvas;
//設定敵人潛艇出現位置
this.x = xPosition;
this.y = yPosition;
this.setPosition(xPosition, yPosition);
this.levelState = levelState * 3;
this.lifeState = true;
this.troop = SubCanvas.TROOP_ENEMY;
//根據初始方向判斷圖形是否需要轉向, 并設定速度方向
if(this.x < SubCanvas.WORLD_WIDTH / 2){
this.setTransform(Sprite.TRANS_NONE);
vx = this.xStep;
}else{
this.setTransform(Sprite.TRANS_MIRROR);
vx = (-1) * this.xStep;
}
}
/**
* 秒觸發
* 在生存狀態中, 根據速度水平(循環次數)移動魚雷
*/
public void tick(){
int i = 0;
//當 當前生命狀態為真, 并且速度狀態尚沒有結束(還可以繼續運行時)
while(lifeState && (i < this.levelState)){
movePosition();
i++;
}
}
/**
* 以一個步長為單位移動圖形, 如果超出邊界則置生存狀態為false
* 移動之后檢查碰撞事件, 如果需要消失也同樣置生存狀態為false
*/
protected void movePosition(){
this.x = x + this.vx;
//確保圖形在游戲區域, 當超出游戲區時, 在反向出現
if(x > SubCanvas.WORLD_WIDTH - w){
x = 0;
this.y = SubMIDlet.createRandom(SubCanvas.WORLD_HEIGHT);
}
if(x < 0){
x = SubCanvas.WORLD_WIDTH - w;
this.y = SubMIDlet.createRandom(SubCanvas.WORLD_HEIGHT);
}
if(y > SubCanvas.WORLD_HEIGHT - h){
this.y = 0;
}
if(y < 0){
this.y = SubCanvas.WORLD_HEIGHT - h;
}
this.setPosition(x, y);
//處理碰撞事件
collideStuff();
}
/**
* 處理 撞擊事件
*/
public void collideStuff(){
Tinfish collideable = null;
Sprite sprite = subCanvas.getMySub();
if(collidesWith(sprite, false)){
this.lifeState = false;
}
for(int i = 0; i < subCanvas.tinfishCollectionVector.size(); i++)
{
collideable = (Tinfish) subCanvas.tinfishCollectionVector.elementAt(i);
//對魚雷數組中的元素,當魚雷是玩家釋放的時候,才觸發碰撞事件
if(collideable.getTroop() == SubCanvas.TROOP_PLAYER){
if (collidesWith(collideable, false))
{
this.lifeState = false;
}
}
}
collideable = null;
}
/**
* @return 返回 troop。
*
* @uml.property name="troop"
*/
public int getTroop() {
return troop;
}
/**
* @param troop 要設置的 troop。
*
* @uml.property name="troop"
*/
public void setTroop(int troop) {
this.troop = troop;
}
/**
* @return 返回 lifeState。
*
* @uml.property name="lifeState"
*/
public boolean isLifeState() {
return lifeState;
}
// /** 添加可以響應撞擊事件的物體
// * @param enemySub
// */
// public void addCollideable(Sprite sprite){
// collideables.addElement(sprite);
// }
//
// /** 刪除可以響應撞擊事件的物體
// * @param enemySub
// */
// public void removeCollideable(Sprite sprite){
// collideables.removeElement(sprite);
// collideables.trimToSize();
// }
/**
* @return 返回 vx。
*
* @uml.property name="vx"
*/
public int getVx() {
return vx;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -