📄 plane.java
字号:
package com.j2medev.chapter5.example.hold;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;
import com.j2medev.chapter5.utility.ImageUtilities;
public class Plane extends Sprite {
final static public int STATE_NORMAL = 1;// 正常状态
final static public int STATE_REST = 2;// 无敌状态
final static public int STATE_EXPLOSION = 3;// 爆炸状态
final static public int STATE_DEAD = 4;// 爆炸状态
public int state = 0;// 状态变量
public int bombnum;
public Sprite rest;// 无敌效果
public Sprite explosion;
public GameWorld world;
protected Plane(Image img, int w, int h) {
super(img, w, h);
}
public void update() {
switch (state) {// 根据状态,更新逻辑和画面
case STATE_NORMAL:
input();// 处理输入
setVisible(true);
rest.setVisible(false);
explosion.setVisible(false);
break;
case STATE_REST:
input();// 处理输入
rest.setVisible(true);
rest.nextFrame();
rest.setRefPixelPosition(this.getRefPixelX(), this.getRefPixelY());
world.bullets.killbullets(this, 28);
if (rest.getFrame() == rest.getFrameSequenceLength() - 1) {
state = STATE_NORMAL;
}
break;
case STATE_EXPLOSION:
input();// 处理输入
explosion.setVisible(true);
explosion.nextFrame();
explosion.setRefPixelPosition(this.getRefPixelX(), this
.getRefPixelY());
if (explosion.getFrame() == explosion.getFrameSequenceLength() - 1) {
state = STATE_DEAD;
}
break;
case STATE_DEAD:
explosion.setVisible(false);
rest.setVisible(false);
setVisible(false);
break;
}
}
public void reset() {
state = STATE_NORMAL;
setFrame(0);
setPosition(world.width / 2, world.height / 2);
defineReferencePixel(getWidth() / 2, getHeight() / 2);
rest.setFrame(rest.getFrameSequenceLength() - 1);
rest.setVisible(false);
rest.defineReferencePixel(rest.getWidth() / 2, rest.getHeight() / 2);
explosion.setFrame(explosion.getFrameSequenceLength() - 1);
explosion.setVisible(false);
explosion.defineReferencePixel(explosion.getWidth() / 2, explosion
.getHeight() / 2);
bombnum = 3;
}
public void collides() {
state = STATE_EXPLOSION;
}
public static Plane createPlane(GameWorld world) {
Image img = ImageUtilities.createImage(("/plane.png"));
Plane p = new Plane(img, 40, 40);
p.world = world;
img = ImageUtilities.createImage(("/rest.png"));
Sprite s = new Sprite(img, 65, 65);
p.rest = s;
img = ImageUtilities.createImage(("/explosion.png"));
s = new Sprite(img, 71, 100);
p.explosion = s;
p.reset();
return p;
}
public void input() {
boolean hasInput = false;
if (world.upAction.isPressed()) {
hasInput = true;
if (getY() - 3 >= 0)
move(0, -3);
setFrame(0);
setTransform(Sprite.TRANS_NONE);
}
if (world.downAction.isPressed()) {
hasInput = true;
if (getY() + getHeight() + 3 < world.viewport_height)
move(0, 3);
setFrame(0);
setTransform(Sprite.TRANS_NONE);
}
if (world.leftAction.isPressed()) {
hasInput = true;
if (getX() - 3 >= 0)
move(-3, 0);
setFrame(1);
setTransform(Sprite.TRANS_NONE);
}
if (world.rightAction.isPressed()) {
hasInput = true;
if (getX() + getWidth() + 3 < world.viewport_width)
move(3, 0);
setFrame(1);
setTransform(Sprite.TRANS_MIRROR);
}
if (world.fireAction.isPressed()) {
hasInput = true;
if (state == STATE_NORMAL && bombnum > 0) {
state = STATE_REST;
bombnum--;
}
System.out.println("fire");
}
if (!hasInput) {
setFrame(0);
setTransform(Sprite.TRANS_NONE);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -