📄 sub.java
字号:
/*
*
* 创建日期 2006-5-17
*
* TODO 要更改此生成的文件的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
package net.wangsong;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
/** * @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 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;
}
}
/** 初始化鱼雷图层
* @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 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;
}
}
/**
* @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 + -