基于J2ME WTK的2D手机游戏开发(英文文献翻译) 第12页
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;
import javax.microedition.lcdui.game.TiledLayer;
public class Role extends Sprite {
int way = 0;// 定1为右方向
static final int[] STOP_SEQ = { 0 };// 停
static final int[] WALK_SEQ = { 0, 1, 2, 3 };// 走
static final int[] JUMP_SEQ = { 4, 5 };// 跳
static final int[] SQUAT_SEQ = { 6 };// 蹲
static final int[] STAGGER_SEQ = { 7, 8, 9, 10 };// 踉跄
static final int[] BEIDA_SEQ = { 0 };// 背
static final int[] LUO_SEQ = { 5 };
static byte state; // 状态
public static final byte STOP = 0, WALK = 1, JUMP = 2, SQUAT = 3,
STAGGER = 4, BEI_DA = 5, DEATH = 6;
public static final byte up_block = 0, down_block = 1, left_block = 3,
right_block = 4, foot_block = 5;
private byte state_c = down_block;
private byte state_move;
private int speed_x = 6; // 人物x移动速度
private int speed_y = 0; // 人物y移动速度
private int jump_x = 6;
private int g = 4;// 人物加速度向上
static int man_x = 0;// 人物初始x坐标
static int man_y = 0;// 人物初始y坐标
int x = 0, y = 0;// 人物移动坐标
int vy = 0;// 人物y移动速度
int height, width;
static int p = 0;// 判断人是否在石头
static int baozi_num = 1;// 包子数量
public Role(Image image, int frameWidth, int frameHeight, TiledLayer tuceng) {
super(image, frameWidth, frameHeight);
spr(tuceng);
}
private void spr(TiledLayer tuceng) {
height = tuceng.getHeight();
width = tuceng.getWidth();
man_y = 32 * 7 - this.getHeight();
setPosition(man_x, man_y);
setNextState(STOP);
setNextDir(Tools.RIGHT); // 设置人物方向
this.defineReferencePixel(getWidth() >> 1, 0); // 设置镜像点
}
public void update() {
switch (state) {
case STOP:
break;
case WALK:
if (state_move == Tools.RIGHT) {
nextFrame(); // 播放下一帧
remove();
state_c = right_block;
} else if (state_move == Tools.LEFT) {
nextFrame(); // 播放下一帧
remove();
state_c = left_block;
}
collide(getMap(state_c));
state_c = foot_block;
collide(getMap(foot_block));
break;
case JUMP:
vy += g;
if (vy < 0) {
state_c = up_block;
} else {
if (vy >= 32) {
vy = 31;
}
state_c = down_block;
}
move(0, vy);
collide(getMap(state_c));
if (way == 0) {
move(jump_x, 0);
state_c = right_block;
} else if (way == 1) {
move(-(jump_x), 0);
// collide(getMap(state_c));
state_c = left_block;
}
collide(getMap(state_c));
break;
case SQUAT:
nextFrame();
break;
case STAGGER:
break;
}
// collide(getMap(state_c));
}
private void remove() {
switch (state_move) {
case Tools.LEFT:
move(-speed_x, 0);
break;
case Tools.RIGHT:
move(speed_x, 0);
break;
}
}
public void setNextDir(byte nextstate_move) {
if (state_move != nextstate_move) {
switch (nextstate_move) {
case Tools.RIGHT:
this.setTransform(Sprite.TRANS_NONE);// 不翻转
way = 0;
break;
case Tools.LEFT:
this.setTransform(Sprite.TRANS_MIRROR);// 沿水平线翻转
way = 1;
break;
}
state_move = nextstate_move;
}
}
public void setNextState(byte nextState) {
if (state != nextState) {
switch (nextState) {
case STOP:
setFrameSequence(STOP_SEQ);
break;
case WALK:
setFrameSequence(WALK_SEQ);
break;
case JUMP:
vy = -22;
speed_y = 0;
jump_x = 6;
setFrameSequence(JUMP_SEQ);
break;
case SQUAT:
setFrameSequence(SQUAT_SEQ);
break;
case STAGGER:
setFrameSequence(STAGGER_SEQ);
break;
case BEI_DA:
setFrameSequence(BEIDA_SEQ);
break;
}
state = nextState;
}
}
public void input(int iAction) {
switch (iAction) {
case 0:
if (state != JUMP)
setNextState(STOP);
break;
case Tools.UP_TYPE:
setNextState(JUMP);
break;
case Tools.DOWN_TYPE:
if (state != JUMP) {
setNextState(SQUAT);
<< 上一页 [11] [12] [13] [14] [15] [16] [17] [18] [19] 下一页
基于J2ME WTK的2D手机游戏开发(英文文献翻译) 第12页下载如图片无法显示或论文不完整,请联系qq752018766