📄 gamecharobj.java
字号:
package LR;
import javax.microedition.lcdui.game.Sprite;
import javax.microedition.lcdui.*;
public class gameCharObj extends Sprite
{
public final static boolean MOVE_LEFT=true;
public final static boolean MOVE_RIGHT=false;
public final static int Game_CHAR_NORM=0;
public final static int Game_CHAR_CONF=1;
public final static int Game_CHAR_SPEEDUP=2;
private final static int Game_CHAR_LIFE=3;
private final static int Game_CHAR_STATUS_TIME=150;
private int gameCharLife=Game_CHAR_LIFE;
private int gameCharStatus=Game_CHAR_NORM;
private static Image img=null;
private int x,y;
private int[] stand={0};
private int[] w_left={1,1,2,2,3,3,4,4};
private int[] w_right={5,5,6,6,7,7,8,8};
private int[] jump={9,9,10,10,11,11,12,12};
private int jump_height=8;
private int jump_distance=0;
private int state=0;
private int statusTime=0;
private boolean face=false;
static
{
try
{
img=Image.createImage("/char.png");
}
catch(Exception e){}
}
public gameCharObj(int X ,int Y)
{
super(img,16,17);
this.defineCollisionRectangle(1,16,15,1);
this.x=X;
this.y=Y-17;
this.setFrameSequence(stand);
this.setPosition(this.x,this.y);
}
private void resetCharStatus()
{
statusTime=0;
gameCharStatus=Game_CHAR_NORM;
}
private void statusTimeGo()
{
if(statusTime>0)
{
statusTime--;
}
else
{
resetCharStatus();
}
}
public void setCharStatus(int st)
{
switch(st)
{
case 0:
gameCharStatus=Game_CHAR_NORM;
break;
case 1:
gameCharStatus=Game_CHAR_CONF;
statusTime=Game_CHAR_STATUS_TIME;
break;
case 2:
gameCharStatus=Game_CHAR_SPEEDUP;
statusTime=Game_CHAR_STATUS_TIME;
break;
default:
return;
}
}
public int getCharStatus()
{
return gameCharStatus;
}
public void setCharHarm(boolean b)
{
if(b)
{
if(gameCharLife<3)
{
gameCharLife++;
}
}
else
{
gameCharLife--;
}
}
public int getCharLife()
{
return gameCharLife;
}
public void up(int Y)
{
y=Y-17;
this.setPosition(this.x,this.y);
}
public void go() //人物垂直移动,如果扔未到达指定跳跃高度就继续升,反之则降
{
if(jump_distance<jump_height)
{
jump_distance++;
y-=3;
}
else
{
y+=3;
}
statusTimeGo(); //控制人物状态;
this.setPosition(this.x,this.y);
}
public void left(boolean b) //左移,参数为是否在板块上
{
if(b)
{
if(gameCharStatus>0)
{
if(gameCharStatus==gameCharObj.Game_CHAR_CONF)
{
x+=3;
}
else
{
x-=5;
}
}
else
{
x-=3;
}
}
else
{
if(gameCharStatus>0)
{
if(gameCharStatus==gameCharObj.Game_CHAR_CONF)
{
x+=2;
}
else
{
x-=3;
}
}
else
{
x-=2;
}
}
if(x<8)
{
x=8;
}
if(x>148)
{
x=148;
}
this.setPosition(this.x,this.y);
}
public void right(boolean b) //右移,参数为是否在板块上
{
if(b)
{
if(gameCharStatus>0)
{
if(gameCharStatus==gameCharObj.Game_CHAR_CONF)
{
x-=3;
}
else
{
x+=5;
}
}
else
{
x+=3;
}
}
else
{
if(gameCharStatus>0)
{
if(gameCharStatus==gameCharObj.Game_CHAR_CONF)
{
x-=2;
}
else
{
x+=3;
}
}
else
{
x+=2;
}
}
if(x<8)
{
x=8;
}
if(x>148)
{
x=148;
}
this.setPosition(this.x,this.y);
}
public void jump() //使人物跳起
{
jump_distance=0;
y=y-5;
this.setPosition(this.x,this.y);
}
public void roll(boolean b) //真左移,假右移
{
if(b)
{
x--;
}
else
{
x++;
}
}
public boolean isDead() //是否死亡
{
if(y>240||y<40||gameCharLife<1)
{
return true;
}
else
{
return false;
}
}
public void setCharAct(int i,boolean b) //改变人物绘图
{
switch(i)
{
case 0:
if(state!=i)
{
this.setFrameSequence(w_left);
state=i;
}
break;
case 1:
if(state!=i)
{
this.setFrameSequence(w_right);
state=i;
}
break;
case 2:
if((state!=i)||(face!=b))
{
this.setFrameSequence(b?stand:jump);
state=i;
face=b;
}
break;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -