📄 bosscayman.java
字号:
package game;
import lib.*;
import javax.microedition.lcdui.*;
//鳄鱼
public class BossCayman extends Sprite{
//鳄鱼下沉
public static final int ACT_DOWN=ACT_USER;
//鳄鱼上浮
public static final int ACT_UP=ACT_USER+1;
//鳄鱼所有的动作切片
private static Rectangle[][] caymanRect=new Rectangle[2][5];
static{
//鳄鱼面向左游1
caymanRect[0][0]=new Rectangle(0,0,93,22);
//鳄鱼面向左游2
caymanRect[0][1]=new Rectangle(94,0,90,24);
//鳄鱼面向左下沉1
caymanRect[0][2]=new Rectangle(185,0,93,22);
//鳄鱼面向左下沉2
caymanRect[0][3]=new Rectangle(279,0,93,19);
//鳄鱼面向左下沉3
caymanRect[0][4]=new Rectangle(373,0,93,16);
//鳄鱼面向右游1
caymanRect[1][0]=new Rectangle(0,24,93,22);
//鳄鱼面向右游2
caymanRect[1][1]=new Rectangle(94,24,90,24);
//鳄鱼面向右下沉1
caymanRect[1][2]=new Rectangle(185,24,93,22);
//鳄鱼面向右下沉2
caymanRect[1][3]=new Rectangle(279,24,93,19);
//鳄鱼面向右下沉3
caymanRect[1][4]=new Rectangle(373,24,93,16);
}
//当前动画帧
private int currAnimIndex=0;
private Resource resource=Resource.getResource();
//鳄鱼在游的过程中动画帧切换时间
private Time animTime=new Time(240);
//鳄鱼游的时间
private Time swimTime=new Time(2000);
//鳄鱼下沉时间
private Time downTime=new Time(2000);
//鳄鱼停止时间
private Time stopTime=new Time(600);
//左极限
private int left;
//右极限
private int right;
//是否下沉
private boolean down;
private int life=20;
private int num;
public BossCayman(int x,int y,int left,int right,int num){
this.x=x;
this.y=y;
this.left=left;
this.right=right;
this.width=93;
this.height=22;
this.num=num;
setAct(ACT_RUN);
setVelocityX(-1);
}
public void paint(Graphics g,int offsetX,int offsetY){
if(down) return;
int ox=x;
if(faceTo==FACETO_RIGHT)
ox=x+(width-caymanRect[faceTo][currAnimIndex].width);
Toolkit.drawRegion(g,resource.imgCayman,caymanRect[faceTo][currAnimIndex],ox+offsetX,y+offsetY);
}
public void update(int elapsedTime){
//更新鳄鱼位置
x=x+vx;
if(vx>0){
if(x>right){
setAct(ACT_STAND);
vx=0;
}
}
else if(vx<0){
if(x<left){
setAct(ACT_STAND);
vx=0;
}
}
//更新鳄鱼动画
switch(getAct()){
//游
case ACT_RUN:
if(animTime.update(elapsedTime)){
currAnimIndex=currAnimIndex==0?1:0;
}
if(swimTime.update(elapsedTime)){
setAct(ACT_DOWN);
currAnimIndex=2;
y+=2;
vx=0;
animTime.restart();
}
break;
case ACT_STAND:
if(animTime.update(elapsedTime)){
currAnimIndex=currAnimIndex==0?1:0;
}
if(stopTime.update(elapsedTime)){
if(faceTo==FACETO_LEFT){
vx=1;
faceTo=FACETO_RIGHT;
setAct(ACT_RUN);
}
else{
vx=-1;
faceTo=FACETO_LEFT;
setAct(ACT_RUN);
}
}
break;
//下沉
case ACT_DOWN:
if(animTime.update(elapsedTime)){
switch(currAnimIndex){
case 2:
y+=2;
currAnimIndex=3;
break;
case 3:
y+=2;
currAnimIndex=4;
break;
case 4:
down=true;
break;
}
}
if(downTime.update(elapsedTime)){
setAct(ACT_UP);
down=false;
y-=2;
}
break;
//上浮
case ACT_UP:
if(animTime.update(elapsedTime)){
switch(currAnimIndex){
case 4:
y-=2;
currAnimIndex=3;
break;
case 3:
y-=2;
currAnimIndex=2;
break;
case 2:
setAct(ACT_RUN);
currAnimIndex=0;
vx=faceTo==FACETO_LEFT?-1:1;
break;
}
}
break;
}
}
public boolean isFly(){
return true;
}
public boolean isFixed(){
return true;
}
public boolean isDown(){
return down;
}
public int getNum(){
return num;
}
public int removeLife(){
life--;
return life;
}
public int getLife(){
return life;
}
public boolean isNeedRemove(){
return life==0;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -