📄 bossunfixedmonkey.java
字号:
package game;
import lib.*;
import javax.microedition.lcdui.*;
//运动的猴子
public class BossUnfixedMonkey extends Sprite{
//扔香蕉皮
public static final int ACT_CHUCK=ACT_USER;
private int life=10;
private int num;
private Resource resource=Resource.getResource();
//猴子所有的动作切片
private static Rectangle[][] monkeyRect=new Rectangle[2][8];
static{
//面向左站立1
monkeyRect[0][0]=new Rectangle(0,0,25,21);
//面向左站立2
monkeyRect[0][1]=new Rectangle(26,1,24,20);
//面向左起跳
monkeyRect[0][2]=new Rectangle(51,5,30,16);
//面向左下落
monkeyRect[0][3]=new Rectangle(82,0,28,21);
//面向左扔香蕉皮1
monkeyRect[0][4]=new Rectangle(111,1,25,20);
//面向左扔香蕉皮2
monkeyRect[0][5]=new Rectangle(137,1,23,20);
//面向左被击中1
monkeyRect[0][6]=new Rectangle(161,0,24,21);
//面向左被击中2
monkeyRect[0][7]=new Rectangle(186,1,23,20);
//面向右站立1
monkeyRect[1][0]=new Rectangle(0,22,25,21);
//面向右站立2
monkeyRect[1][1]=new Rectangle(26,23,24,20);
//面向右起跳
monkeyRect[1][2]=new Rectangle(51,27,30,16);
//面向右下落
monkeyRect[1][3]=new Rectangle(82,22,28,21);
//面向右扔香蕉皮1
monkeyRect[1][4]=new Rectangle(111,23,25,20);
//面向右扔香蕉皮2
monkeyRect[1][5]=new Rectangle(137,23,23,20);
//面向右被击中1
monkeyRect[1][6]=new Rectangle(161,22,24,21);
//面向右被击中2
monkeyRect[1][7]=new Rectangle(186,23,23,20);
}
//当前帧
private int currAnimIndex;
//左极限
private int left;
//右极限
private int right;
//猴子跳的时间
private Time jumpTime=new Time(Toolkit.getInt(3000,5000));
//跳跃中途停顿时间
private Time stopTime=new Time(Toolkit.getInt(700,900));
//猴子站立时间
private Time standTime=new Time(1000);
//猴子在各种动画下帧的变换速度
private Time animTime=new Time(240);
//是否需要删除
private boolean needRemove;
//切换到ACT_STAND
private boolean toStand;
public BossUnfixedMonkey(int x,int y,int left,int right,int num){
this.x=x;
this.y=y;
this.width=24;
this.height=20;
this.left=left;
this.num=num;
this.right=right;
faceTo=Toolkit.getInt(0,1);
}
public void paint(Graphics g,int offsetX,int offsetY){
int ox=x;
if(faceTo==FACETO_RIGHT)
ox=x+(width-monkeyRect[FACETO_RIGHT][currAnimIndex].width);
int oy=y+(height-monkeyRect[faceTo][currAnimIndex].height);
Toolkit.drawRegion(g,resource.imgMonkey,monkeyRect[faceTo][currAnimIndex],ox+offsetX,oy+offsetY);
}
public void update(int elapsedTime){
switch(act){
case ACT_JUMP:
//设置帧
if(vy<0)
currAnimIndex=2;
else
currAnimIndex=3;
//对跳跃时间进行计时
if(!toStand && jumpTime.update(elapsedTime)){
toStand=true;
}
break;
case ACT_STAND:
if(toStand){
if(standTime.update(elapsedTime)){
toStand=false;
setAct(ACT_CHUCK);
currAnimIndex=4;
}
}
//跳跃
else{
//对跳跃时间进行计时
if(!toStand && jumpTime.update(elapsedTime)){
toStand=true;
}
if(!toStand && stopTime.update(elapsedTime)){
//继续向左跳
if(faceTo==FACETO_LEFT && x>(left+10)){
vx=-4;
vy=-3;
}
//调头向左跳
else{
faceTo=FACETO_RIGHT;
vx=4;
vy=-3;
}
//继续向右跳
if(faceTo==FACETO_RIGHT && x<(right-20)){
vx=4;
vy=-3;
}
//调头向左跳
else{
faceTo=FACETO_LEFT;
vx=-4;
vy=-3;
}
}
}
if(animTime.update(elapsedTime)){
currAnimIndex=currAnimIndex==0?1:0;
}
break;
case ACT_CHUCK:
if(animTime.update(elapsedTime)){
if(currAnimIndex==4){
currAnimIndex=5;
Sprite sprite=new Banana(getFaceTo()==0?getX():getX()+getWidth(),getY());
sprite.setVelocityX(getFaceTo()==0?-3:3);
sprite.setVelocityY(-8);
sprite.setTilesEngine(getTilesEngine());
getTilesEngine().addSprites(sprite);
}
else{
setAct(ACT_JUMP);
currAnimIndex=0;
}
}
break;
case ACT_DEAD:
if(animTime.update(elapsedTime)){
if(currAnimIndex==6){
currAnimIndex=7;
}
else{
needRemove=true;
}
}
break;
}
}
public void collideVertical(){
vx=0;
vy=0;
}
public boolean isFixed(){
if(act==ACT_CHUCK)
return true;
else
return false;
}
public boolean isAlive(){
return getAct()!=ACT_DEAD;
}
public void setAct(int act){
super.setAct(act);
if(act==ACT_DEAD){
currAnimIndex=6;
}
}
public boolean isNeedRemove(){
return needRemove;
}
public int removeLife(){
life--;
return life;
}
public int getLife(){
return life;
}
public int getNum(){
return num;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -