📄 torpedo.java~51~
字号:
package mysubmarine;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;
/**
* <p>Title: 一个简单的潜艇游戏</p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2005</p>
*
* <p>Company: Star Group</p>
*
* @author not attributable
* @version 1.0
*/
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 + -