📄 ship.java
字号:
import java.io.IOException;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
public class Ship extends Actor {
public Ship(){
world = World.getInstance();
}
public void initData() {
try {
shipImage = Image.createImage("/ship.png");
} catch (IOException e) {
e.printStackTrace();
}
initXYSpeed();
}
public void changeMyState(){
int [] data = getIProcessData();
if(processCount < getIProcess()){
if (++processTimeCount <= data[processCount * 3 + 0]) {
int orbitType = data[processCount * 3 + 1];
int bulletType = data[processCount * 3 + 2];
updateXY(orbitType,bulletType);
}else{
processCount++;
processTimeCount=0;
}
}
}
public void updateXY(int orbitType, int bulletType) {
int xPos = getIPosX();
int yPos = getIPosY();
int speed = getSpeed() << 8;
switch (orbitType) {
case 12: // 顺时针向下
setIPosY(yPos + speed);
if (bulletType != 0) {
if (processTimeCount == 1) {
// 求朝屏幕中心运动的X,Y的运动的速度
int centerX = GameConstant.iSCREEN_WIDTH >> 1;
int centerY = GameConstant.iSCREEN_HEIGHT >> 1;
int bulletX = (xPos >> 8) - 5;
int bulletY = (yPos >> 8) - 23;
int dis = Tool.sqrt(Tool.getDistance(centerX, centerY,
bulletX, bulletY));
int xSpeed = (4 << 8) / dis * (centerX - bulletX);
int ySpeed = -(4 << 8) / dis * (centerY - bulletY);
bullet = world.newBullet(bulletType, xPos - (5 << 8), yPos
- (23 << 8), xSpeed, ySpeed);
}
if (processTimeCount == 20) {
// fire(bulletType, bullet.getIPosX(), bullet.getIPosY());
// bullet.setYSpeed( - 100 << 8); //让他消失
// bullet.setXSpeed(secXSpeed[0]);
// bullet.setYSpeed(secYSpeed[0]);
//world.getHelicopterBulletPool().releasUsed(bullet); // 让他消失
for (int idx = 0; idx < 8; idx++) {
world.newBullet(bulletType, bullet.getIPosX(), bullet
.getIPosY(), secXSpeed[idx], secYSpeed[idx]);
}
}
}
break;
case 0: // 原地不动
if (processTimeCount == 1) {
for (int idx = 0; idx < 5; idx++) {
world.newBullet(bulletType, xPos - (5<<8), yPos - (23 << 8),
xSpeed[idx], ySpeed[idx]);
}
}
break;
}
}
public void draw(Graphics graphics){
graphics.drawImage(shipImage, getIPosX() >> 8, getIPosY() >> 8,
GameConstant.CENTER);
}
private void initXYSpeed(){
//已300度到240度的角
xSpeed = Tool.getXSpeedByAngle(300,5,15,4);
ySpeed = Tool.getYSpeedByAngle(300,5,15,4);
secXSpeed = Tool.getXSpeedByAngle(360,8,45,5);
secYSpeed = Tool.getYSpeedByAngle(360,8,45,5);
}
private int [] xSpeed = new int[5];
private int [] ySpeed = new int [5];
private int [] secXSpeed = new int [8];
private int [] secYSpeed = new int [8];
private Image shipImage;
private Bullet bullet = null;
private World world;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -