?? sub.java
字號:
/*
* Author: Huang ye(www.hyweb.net)
* 代碼開源, 引用請注明出處
*
* 創建日期 2005-2-25
*
* TODO 要更改此生成的文件的模板,請轉至
* 窗口 - 首選項 - Java - 代碼樣式 - 代碼模板
*/
package net.hyweb;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
//
//import java.util.*;
/** * @author user
*
* TODO 要更改此生成的類型注釋的模板,請轉至
* 窗口 - 首選項 - Java - 代碼樣式 - 代碼模板 */
public class Sub extends Sprite implements SubObject {
/**
*
* @uml.property name="subCanvas"
* @uml.associationEnd multiplicity="(0 1)"
*/
private SubCanvas subCanvas;
private LayerManager layerManager;
private boolean lifeState = false; //生命狀態
private int troop = 0;
private int levelState = 0; //自身以及子彈速度狀態. 51 - 5
private int directionState = 1; //當前圖形方向. 0為左, 1為右
boolean hurtBoolean = false;
//private Vector collideables = new Vector();
private int keyState = 0;
private int hpLife = 15; //玩家生命值HP
private int xStep = 15;
private int yStep = 15;
private int x = getX();
private int y = getY();
private int w = getWidth();
private int h = getHeight();
private int oldX;
private int oldY;
public Sub(SubCanvas subCanvas, Image image, int xPosstion, int yPosstion, LayerManager layerManager){
super(image);
this.subCanvas = subCanvas;
this.setPosition(xPosstion, yPosstion);
this.layerManager = layerManager;
lifeState = true;
//設定敵我識別
this.troop = SubCanvas.TROOP_PLAYER;
this.levelState = 3;
this.directionState = 1;
}
/** 根據傳入的鍵參數執行移動
* @param keyState
*/
public void movePosition(int keyState){
this.keyState = keyState;
//判斷走向, 執行移動
x = getX();
y = getY();
oldX = x;
oldY = y;
w = getWidth();
h = getHeight();
if((keyState & GameCanvas.DOWN_PRESSED) != 0 && subCanvas.gameState == subCanvas.GAME_RUN){
//下移
y = y + yStep;
}else if((keyState & GameCanvas.UP_PRESSED) != 0 && subCanvas.gameState == subCanvas.GAME_RUN){
//上移
y = y - yStep;
}else if((keyState & GameCanvas.LEFT_PRESSED) != 0 && subCanvas.gameState == subCanvas.GAME_RUN){
//左移
if(this.directionState == 1){
setTransform(Sprite.TRANS_MIRROR);
this.directionState = 0;
}else{
x = x - xStep;
}
}else if((keyState & GameCanvas.RIGHT_PRESSED) != 0 && subCanvas.gameState == subCanvas.GAME_RUN){
//右移
if(this.directionState == 0){
setTransform(Sprite.TRANS_NONE);
this.directionState = 1;
}else{
x = x + xStep;
}
}
// else if((keyState & GameCanvas.FIRE_PRESSED) != 0 && subCanvas.gameState == subCanvas.GAME_RUN){
// this.fire();
// }
/** 初始化魚雷圖層
* @param subCanvas Canvas類
* @param image 魚雷圖片
* @param xPosition 初始化坐標起點
* @param yPosition 初始化坐標終點
* @param troop 敵我標識
* @param levelState 速度水平
* @param directionState 初始化方向
*/
//確保圖形在游戲區域
if(x > SubCanvas.WORLD_WIDTH - w){
x = SubCanvas.WORLD_WIDTH - w;
}
if(x < 0){
x = 0;
}
if(y > SubCanvas.WORLD_HEIGHT - h){
y = SubCanvas.WORLD_HEIGHT - h;
}
if(y < 0){
y = 0;
}
this.setPosition(x, y);
//處理碰撞事件
collideStuff();
//當受到傷害時
if(hurtBoolean){
this.hurt();
}
//重新設置圖層顯示區域
subCanvas.adjustViewWindow(x, y, getWidth(), getHeight());
}
public void fire(){
if(subCanvas.tinfishCollectionVector.size() <= 10){
//開火
Image image = SubMIDlet.createImage("/res/tinfish3.png");
if(this.directionState == 0){
//魚雷方向向左
Tinfish tinfish = new Tinfish(subCanvas, image, x, y + h / 2,
this.troop, this.levelState, this.directionState);
//添加魚雷到數組以及圖層上
layerManager.insert(tinfish, 0);
subCanvas.tinfishCollectionVector.addElement(tinfish);
}else{
//魚雷方向向左
Tinfish tinfish = new Tinfish(subCanvas, image, x + w, y + h / 2,
this.troop, this.levelState, this.directionState);
//添加魚雷到數組以及圖層上
layerManager.insert(tinfish, 0);
subCanvas.tinfishCollectionVector.addElement(tinfish);
}
image = null;
}
}
/**
* 根據內部存儲的碰撞物件數組, 處理碰撞事件
*/
public void collideStuff(){
Sprite collideable = null;
hurtBoolean = false;
// //碰撞 檢測是否撞擊到障礙物
// for(int i = 0; i < collideables.size(); i++){
// collideable = (Sprite)collideables.elementAt(i);
// //象素級碰撞檢測
// if(this.collidesWith(collideable, true)){
// //撞擊到普通物件, 不能通過, 保持原先位置
// this.setPosition(oldX, oldY);
// x = oldX;
// y = oldY;
// }
// }
for(int j = 0; j < subCanvas.enemyCollectionVector.size(); j++)
{
collideable = (Sprite) subCanvas.enemyCollectionVector.elementAt(j);
if (collidesWith(collideable, true))
{
hurtBoolean = true;
}
}
}
private void hurt(){
this.hpLife --;
if(hpLife <= 0){
lifeState = false;
}
}
// public void addCollideable(Sprite sprite)
// {
// collideables.addElement(sprite);
// }
/**
* @return 返回 lifeState。
*/
public boolean isLifeState() {
return lifeState;
}
/** 獲取玩家生命值
* @return 返回 hpLife。
*/
public int getHpLife() {
return hpLife;
}
/**
* @param hpLife 要設置的 hpLife。
*/
public void setHpLife(int hpLife) {
this.hpLife = hpLife;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -