📄 mainsprite.java
字号:
package zonja;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
public class MainSprite extends ZonSprite {
public MainSprite(int scrWidth, int scrHeight) {
super(scrWidth, scrHeight);
setRealSize(20,30);
bloodCount = 4;
lifeCount = 3;
}
// static final
private int bloodCount = 0;
public int lifeCount = 0;
public static final int KEY_LEFT = 0x00000001;
public static final int KEY_RIGHT = 0x00000002;
public static final int KEY_LEFT_MASK = 0x00010000;
public static final int KEY_RIGHT_MASK = 0x00020000;
public static final int KEY_UP = 0x00000004;
public static final int KEY_DOWN = 0x00000008;
public static final int KEY_UP_MASK = 0x00040000;
public static final int KEY_DOWN_MASK = 0x00080000;
public static final int KEY_FIRE = 0x00000010;
public static final int KEY_FIRE_MASK = 0x00100000;
public static final int INVINCIBLE_COUNT = 16;
public static final int JUMP_HEIGHT = -12;
public static final int VELOCITY_MAX = 9;
//variable
public int keyStatus = 0;
private boolean inVincible = false;
boolean isDeading = false;
private boolean isOnStone = false;
private boolean isOnFloat = false;
private boolean isObstructed = false;
private boolean suddenDeath = false;
private int inVincibleCount = 0;
private int inDeadingCount = 0;
public int score = 0;
private int keySymbol = 0;
private int stepType = StepStone.STEPTYPE_STONE;
//other instance
Image numImage = null;
Image bloodImage = null;
Image headImage = null;
ZonBullet [] spriteBullet = null;
//methods
public boolean saveData(DataOutputStream dos) {
if(!super.saveData(dos))
return false;
try {
dos.writeByte(bloodCount);
dos.writeByte(lifeCount);
dos.writeByte(stepType);
dos.writeBoolean(inVincible);
dos.writeBoolean(isDeading);
dos.writeBoolean(isOnStone);
dos.writeBoolean(isObstructed);
dos.writeBoolean(suddenDeath);
dos.writeInt(inVincibleCount);
dos.writeInt(inDeadingCount);
dos.writeInt(score);
dos.writeInt(keySymbol);
}
catch(Exception e) {
return false;
}
return true;
}
public final boolean loadData(DataInputStream dis) {
if(!super.loadData(dis))
return false;
try {
bloodCount = dis.readByte();
lifeCount = dis.readByte();
stepType = dis.readByte();
inVincible = dis.readBoolean();
isDeading = dis.readBoolean();
isOnStone = dis.readBoolean();
isObstructed = dis.readBoolean();
suddenDeath = dis.readBoolean();
inVincibleCount = dis.readInt();
inDeadingCount = dis.readInt();
score = dis.readInt();
keySymbol = dis.readInt();
}
catch(Exception e) {
return false;
}
return true;
}
protected final void initImage(){
headImage = ZonLayer.findImage("bubblehead");
bloodImage = ZonLayer.findImage("bloodnumber");
numImage = ZonLayer.findImage("bloodnumber");
spriteBullet = new ZonBullet[3];
for(int i =0; i < 3; i++){
spriteBullet[i] = new ZonBullet(scrWidth, scrHeight);
spriteBullet[i].setVisible(false);
spriteBullet[i].initSprite("bubblehead", 16, 16, false);
spriteBullet[i].setFrameSequence(0, 0);
spriteBullet[i].setMap(map);
}
}
private final void drawFlagImage(Graphics g){
//blood count
for(int i = 0; i < bloodCount; i++){
g.setClip(10 + i * 4, 1, 8, 8);
g.drawImage(bloodImage, 10 + i * 4, 1 - 80, Graphics.LEFT | Graphics.TOP);
}
//boy flag
g.setClip(scrWidth / 3, 1, 16, 16);
g.drawImage(headImage, scrWidth / 3, 1 - 64, Graphics.LEFT | Graphics.TOP);
//life count
g.setClip(scrWidth / 3 + 20, 7, 8, 8);
g.drawImage(numImage, scrWidth / 3 + 20, 7 - lifeCount * 8, Graphics.LEFT | Graphics.TOP);
//score
//last bit here
g.setClip(scrWidth - 8, 1, 6, 8);
g.drawImage(numImage, scrWidth - 8, 1, Graphics.LEFT | Graphics.TOP);
//other bits
for(int i = 0; i < 5; i++){
int a = 0;
switch(i){
case 0:
a = score % 10;
break;
case 1:
a = (score % 100) / 10;
break;
case 2:
a = (score % 1000) / 100;
break;
case 3:
a = (score % 10000) / 1000;
break;
case 4:
a = (score % 100000) / 10000;
break;
}
g.setClip(scrWidth - 8 - (i + 1) * 6, 1, 6, 8);
g.drawImage(numImage, scrWidth - 8 - (i + 1) * 6, 1 - a * 8, Graphics.LEFT | Graphics.TOP);
}
}
public final void paint(Graphics g, boolean bReverse, boolean bWrap) {
// TODO Auto-generated method stub
drawFlagImage(g);
for(int i = 0; i< 3; i++){
if(spriteBullet[i].isVisible)
spriteBullet[i].paint(g, false, false);
}
if(isVisible){
g.setClip(offsetX, offsetY, frameWidth, frameHeight);
int x = 0, y = 0, index = (forceFrameIndex == -1) ? frameIndex
: forceFrameIndex;
x = offsetX;
y = offsetY - index * frameHeight;
g.drawImage(bReverse ? reverseImage : sourceImage, x, y, Graphics.TOP
| Graphics.LEFT);
}
}
private final void spriteInSky() {
if(isOnFloat)
return;
if (vVelocity < 0) {
isStanding = false;
if (moveDirection == MOVEDIRECTION_LEFT) {
setFrameSequence(15, 15);
} else if (moveDirection == MOVEDIRECTION_RIGHT) {
setFrameSequence(4, 4);
}
} else if (!isStanding && vVelocity == 0) {
if (moveDirection == MOVEDIRECTION_LEFT) {
setFrameSequence(16, 16);
} else if (moveDirection == MOVEDIRECTION_RIGHT) {
setFrameSequence(5, 5);
}
} else if (!isStanding && vVelocity > 0) {
if (moveDirection == MOVEDIRECTION_LEFT) {
setFrameSequence(17, 17);
} else if (moveDirection == MOVEDIRECTION_RIGHT) {
setFrameSequence(6, 6);
}
}
if((keyStatus & KEY_FIRE) == KEY_FIRE && !isStanding){
if(moveDirection == MOVEDIRECTION_RIGHT){
if(actionFirstIndex != 7 || actionLastIndex != 7)
setFrameSequence(7, 7);
}
else{
if(actionFirstIndex != 18 || actionLastIndex != 18)
setFrameSequence(18, 18);
}
}
}
private final void spriteFire() {
//bullet action
for(int i = 0; i < 3; i++)
spriteBullet[i].mainAction();
//have not attack ability when injured
if ((keyStatus & KEY_FIRE_MASK) == KEY_FIRE_MASK) {
keyStatus &= ~KEY_FIRE_MASK;
for(int i = 0; i < spriteBullet.length; i++){
if(!spriteBullet[i].isVisible){
if(!inVincible)
spriteBullet[i].isVisible = true;
else {
spriteBullet[i].isVisible = false;
break;
}
if(moveDirection == MOVEDIRECTION_RIGHT){
spriteBullet[i].setPosition(offsetX + 29, offsetY + 13);
spriteBullet[i].frameIndex = 0;
spriteBullet[i].setMoveDirection(MOVEDIRECTION_RIGHT);
}else{
spriteBullet[i].setPosition(offsetX - 4, offsetY + 13);
spriteBullet[i].frameIndex = 3;
spriteBullet[i].setMoveDirection(MOVEDIRECTION_LEFT);
}
break;
}
}
switch (moveDirection) {
case MOVEDIRECTION_LEFT:
if (isStanding) {
if (actionFirstIndex != 19 || actionLastIndex != 19)
setFrameSequence(19, 19);
} else {
if (actionFirstIndex != 18 || actionLastIndex != 18)
setFrameSequence(18, 18);
}
break;
case MOVEDIRECTION_RIGHT:
if (isStanding) {
if (actionFirstIndex != 8 || actionLastIndex != 8)
setFrameSequence(8, 8);
} else {
if (actionFirstIndex != 7 || actionLastIndex != 7)
setFrameSequence(7, 7);
}
break;
}
} else {
switch (moveDirection) {
case MOVEDIRECTION_LEFT:
if (isStanding){
if((keyStatus & KEY_LEFT) == 0 && (keyStatus & KEY_RIGHT) == 0 ){
if (actionFirstIndex != 11 || actionLastIndex != 11)
setFrameSequence(11, 11);
}else{
if (actionFirstIndex != 11 || actionLastIndex != 14)
setFrameSequence(11, 14);
}
}
break;
case MOVEDIRECTION_RIGHT:
if (isStanding){
if((keyStatus & KEY_LEFT) == 0 && (keyStatus & KEY_RIGHT) == 0 ){
if (actionFirstIndex != 0 || actionLastIndex != 0)
setFrameSequence(0, 0);
}else{
if (actionFirstIndex != 0 || actionLastIndex != 3)
setFrameSequence(0, 3);
}
}
break;
}
}
}
private final void spriteMove() {
if ((keyStatus & KEY_UP_MASK) == KEY_UP_MASK) {
keyStatus &= ~KEY_UP_MASK;
if (isStanding) {
setVelocity(DIRECTION_VERTICAL, JUMP_HEIGHT);
}
}
if ((keyStatus & KEY_LEFT) == KEY_LEFT) {
if ((keyStatus & KEY_LEFT_MASK) == KEY_LEFT_MASK)
setFrameSequence(11, 14);
keyStatus &= ~KEY_LEFT_MASK;
setMoveDirection(MOVEDIRECTION_LEFT);
if((!isOnStone && !isOnFloat) || stepType == StepStone.STEPTYPE_GREENLEAF)
setVelocity(DIRECTION_HORIZONTAL, -4);
else{
hVelocity = hVelocity - 4;
}
//is Obstructed by barrier
if(isObstructed)
hVelocity = 0;
} else if ((keyStatus & KEY_RIGHT) == KEY_RIGHT) {
if ((keyStatus & KEY_RIGHT_MASK) == KEY_RIGHT_MASK)
setFrameSequence(0, 3);
keyStatus &= ~KEY_RIGHT_MASK;
setMoveDirection(MOVEDIRECTION_RIGHT);
if((!isOnStone && !isOnFloat) || stepType == StepStone.STEPTYPE_GREENLEAF)
setVelocity(DIRECTION_HORIZONTAL, 4);
else
hVelocity = hVelocity + 4;
//is Obstructed by barrier
if(isObstructed)
hVelocity = 0;
}else {
if(!isOnStone && !isOnFloat)
setVelocity(DIRECTION_HORIZONTAL, 0);
else if(stepType == StepStone.STEPTYPE_GREENLEAF)
setVelocity(DIRECTION_HORIZONTAL, 0);
if(moveDirection == MOVEDIRECTION_RIGHT){
setFrameSequence(0, 0);
}else {
setFrameSequence(11, 11);
}
}
spriteFire();
//sprite action in the sky
spriteInSky();
}
//check the lead is live or dead: return true / dead
private final boolean checkDeath(){
if(bloodCount < 1){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -