📄 enemy.java
字号:
import javax.microedition.lcdui.Graphics;
public class Enemy {
int Hp;
boolean isDide;
int curStatus;
int frameIndex;
int actionIndex;
boolean isRight;
boolean ischick;
Engine engine;
static final byte ST_RUN=0;//走动
static final byte ST_AWAY=1;//消失
static final byte ST_DIDE=2;//死亡
int x; //敌人的位置(相对地图的x坐标)
int y; //敌人的位置(相对地图的y坐标)
int w; //敌人的宽度(用来矩形碰撞处理)
int h; //敌人的宽度
public Enemy(boolean dir){
if(dir){
this.x=Map.leftTopX+20+176;
}else{
this.x=Map.leftTopX-20;
}
y=144;
ischick=false;
isDide=false;
setStatus(ST_RUN);
this.isRight=dir;
w=14;
h=36;
}
public void drawEnemy(Graphics g){
if (isDide) return;
Tools.drawNpcItemData(g,2,actionIndex,frameIndex,x,y,isRight);
EnemyMove();
}
public void EnemyMove(){ //怪物移动
switch(curStatus){
case ST_RUN:
if(isRight){
x-=2;
}else{
x+=2;
}
if(x<110&&isRight){
setStatus(ST_AWAY);
}
if(x>692&&!isRight){
setStatus(ST_AWAY);
}
break;
case ST_AWAY:
ischick=true;
if(aniIndex>=20)
isDide=true;
break;
case ST_DIDE:
ischick=true;
if(aniIndex>=15)
isDide=true;
break;
}
}
public void setStatus(byte status){
if(curStatus == status){
return;
}
aniIndex = 0;
frameIndex = 0;
switch (status){
case ST_RUN:
actionIndex=0;
break;
case ST_AWAY:
actionIndex=1;
break;
case ST_DIDE:
actionIndex=2;
break;
}
curStatus=status;
}
public void EnemyLogic(){
switch (curStatus){
case ST_RUN:
changeRoleFrameIndex(-1);
break;
case ST_AWAY:
changeRoleFrameIndex(-2);
break;
case ST_DIDE:
changeRoleFrameIndex(-2);
break;
}
}
/**
* nextState > 0 切换状态
* nextState = -1 循环播放
* nextState < -1 停留在最后一帧
* @param nextState byte
*/
private int aniIndex; //切换帧使用到的中间过渡变量
private final void changeRoleFrameIndex(int nextState)
{
if (aniIndex >= Data.frameItemIndex[2][actionIndex].length)
{
if (nextState >= 0)
{
aniIndex = 0;
frameIndex = Data.frameItemIndex[2][actionIndex][aniIndex];
setStatus((byte)nextState);
return;
}
else if (nextState == -1)
{
aniIndex = 0;
}
else
{
return;
}
}
frameIndex = Data.frameItemIndex[2][actionIndex][aniIndex];
aniIndex++;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -