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