📄 cactor.java
字号:
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
public class CActor implements Const {
// x坐标
public int x = 0;
// y坐标
public int y = 0;
// 帧的索引值
public int frameindex = 0;
// 每帧的宽
public int frame_w = 0;
// 每帧的高
public int frame_h = 0;
// 碰撞区域左x坐标
public int col_lx = 0;
// 碰撞区域右x坐标
public int col_rx = 0;
// 碰撞区域上y坐标
public int col_uy = 0;
// 碰撞区域下y坐标
public int col_dy = 0;
// 是否碰撞
public boolean s_isCol = true;
// 角色图片
public Image img = null;
// 帧序列
public int frame[];
// 角色移动速度
public int move_speed;
// 物理层地图数据
public int[][] colmapdata;
// 方向布尔值
public boolean dir_up = false;
public boolean dir_down = false;
public boolean dir_left = false;
public boolean dir_right = false;
// 方向上碰撞布尔值
public boolean isCollisionUp = false;
public boolean isCollisionDown = false;
public boolean isCollisionLeft = false;
public boolean isCollisionRight = false;
// 是否死亡
public boolean islive = true;
/*
* 判断是否与地图数组碰撞
*/
boolean CollisionArray(int col, int map[]) {
for (int i = 0; i < map.length; i++) {
if (col == map[i]) {
return true;
}
}
return false;
}
/*
* 判断是否与地图碰撞
*/
void CollisionMap() {
if (dir_up) {
int temp1 = CMap.mapdata_overpl[getNextCol(true, y)][getRow(col_lx)];
int temp2 = CMap.mapdata_overpl[getNextCol(true, y)][getRow(col_rx)];
if (CollisionArray(temp1, CMap.mapcol)
|| CollisionArray(temp2, CMap.mapcol))
isCollisionUp = true;
}
if (dir_down) {
int temp1 = CMap.mapdata_overpl[getNextCol(false, y)][getRow(col_lx)];
int temp2 = CMap.mapdata_overpl[getNextCol(false, y)][getRow(col_rx)];
if (CollisionArray(temp1, CMap.mapcol)
|| CollisionArray(temp2, CMap.mapcol))
isCollisionDown = true;
}
if (dir_left) {
int temp1 = CMap.mapdata_overpl[getCol(col_uy)][getNextRow(true, x)];
int temp2 = CMap.mapdata_overpl[getCol(col_dy)][getNextRow(true, x)];
if (CollisionArray(temp1, CMap.mapcol)
|| CollisionArray(temp2, CMap.mapcol))
isCollisionLeft = true;
}
if (dir_right) {
int temp1 = CMap.mapdata_overpl[getCol(col_uy)][getNextRow(false, x)];
int temp2 = CMap.mapdata_overpl[getCol(col_dy)][getNextRow(false, x)];
if (CollisionArray(temp1, CMap.mapcol)
|| CollisionArray(temp2, CMap.mapcol))
isCollisionRight = true;
}
}
/*
* 得到角色碰撞区域坐标
*/
public void getColArea() {
col_lx = x;
col_rx = x + frame_w;
col_uy = y;
col_dy = y + frame_h;
}
/*
* 得到角色当前所在列
*/
public int getCol(int y) {
return y / CGame.mp.block_h;
}
/*
* 得到角色当前所在行
*/
public int getRow(int x) {
return x / CGame.mp.block_w;
}
/*
* 设置物理层地图数据
*/
public void setCollisionMap(int mapdata[][]) {
colmapdata = mapdata;
}
/*
* 得到角色行走的下一列数
*/
public int getNextRow(boolean left, int x) {
if (left)
return (x - move_speed) / CGame.mp.block_w;
else
return (x + move_speed + CGame.mp.block_w) / CGame.mp.block_w;
}
/*
* 得到角色行走的下一行数
*/
public int getNextCol(boolean up, int y) {// 返回下一行数
if (up)
return (y - move_speed) / CGame.mp.block_h;
else
return (y + move_speed + CGame.mp.block_h) / CGame.mp.block_h;
}
/*
* 设置动画下一帧
*/
public void setFrame(int[] frame) {
this.frame = frame;
}
/*
* 循环播放动画
*/
int i = 0;
void nextFrame() {
if (i < frame.length - 1)
i++;
else
i = 0;
frameindex = frame[i];
}
/*
* 设置角色图片
*/
void setImage(Image img) {
this.img = img;
}
/*
* 绘制角色
*/
void draw(Graphics g) {
if (islive) {
g.setClip(x, y, frame_w, frame_h);
int tempx, tempy;
tempx = frameindex % (img.getWidth() / frame_w);
tempy = frameindex / (img.getWidth() / frame_w);
g.drawImage(img, x - frame_w * tempx, y - frame_h * tempy, 0);
}
}
/*
* 设置角色位置
*/
void setPosition(int newx, int newy) {
x = newx;
y = newy;
}
/*
* 设置角色位置
*/
void setSpeed(int newSpeed) {
move_speed = newSpeed;
}
/*
* 角色移动,并控制其位于屏幕范围内
*/
void move(int dx, int dy) {
if ((x + dx <= SCREEN_WIDTH - MP_FRAME_W) && (x + dx >= 0))
x += dx;
if ((y + dy <= SCREEN_HEIGHT - MP_FRAME_H) && (y + dy >= 0))
y += dy;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -