我是一个新手分享下我写的精灵类~有问题的多多指教~
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
class Sprite {
/**********************全局变量*************************************/
private int picture_getWidth;
private int picture_getHeight;
private int picture_movex;
private int picture_movey;
private int setFrameX;
private int setFrameY;
private int time = 0;
private int setFrameSequence[];
private Image picture;
private boolean setVisible_apen = true;
private int specifiedx;
private int specifiedy;
private int specifiedgetWidth;
private int specifiedgetHeight;
private boolean fanhui;
/***************************创建精灵*******************************/
Sprite(Image im, int getWidth, int getHeight) {
if (im != null) {
if (im.getWidth() % getWidth == 0 &&
im.getHeight() % getHeight == 0) {
picture_getWidth = getWidth;
picture_getHeight = getHeight;
picture = im;
} else {
IllException();
}
} else {
NULLException();
}
}
Sprite(Image im) {
if (im == null) {
NULLException();
} else {
picture_getWidth = im.getWidth();
picture_getHeight = im.getHeight();
picture = im;
}
}
/****************************异常***********************************/
private void NULLException() {
throw new NullPointerException("图片为空");
}
private void IllException() {
throw new IllegalArgumentException("参数异常");
}
/***************************画方法************************************/
public void paint(Graphics g) {
if (setVisible_apen) {
g.setClip(picture_movex, picture_movey, picture_getWidth,
picture_getHeight);
g.drawImage(picture, 0 + picture_movex + setFrameX,
0 + picture_movey + setFrameY, 0);
}
}
/***************************设定位置**********************************/
public void setPosition(int a, int b) {
if (setVisible_apen) {
picture_movex = a;
picture_movey = b;
}
}
/*****************************移动**********************************/
public void move(int a, int b) {
if (setVisible_apen) {
picture_movex += a;
picture_movey += b;
}
}
/**************************位置输出********************************/
public int getX() {
return picture_movex;
}
public int getY() {
return picture_movey;
}
/*************************指定帧数**************************************/
public int setFrame(int a) {
if (setVisible_apen) {
setFrameY = (a / (picture.getWidth() / picture_getWidth)) *
-picture_getHeight;
setFrameX = (a -
((a / (picture.getWidth() / picture_getWidth)) *
(picture.getWidth() / picture_getWidth))) *
-picture_getWidth;
if (a / (picture.getWidth() / picture_getWidth) >
(picture.getHeight() / picture_getHeight) - 1 ||
a -
((a / (picture.getWidth() / picture_getWidth)) *
(picture.getWidth() / picture_getWidth)) >
picture.getWidth() / picture_getWidth || a < 0) {
IllException();
}
return a;
} else {
return 0;
}
}
/**************************指定数组***********************************/
public void setFrameSequence(int a[]) {
if (setVisible_apen) {
setFrameSequence = a;
setFrameY = (a[0] / (picture.getWidth() / picture_getWidth)) *
-picture_getHeight;
setFrameX = (a[0] -
((a[0] / (picture.getWidth() / picture_getWidth)) *
(picture.getWidth() / picture_getWidth))) *
-picture_getWidth;
}
}
/***************************下一帧************************************/
public void nextFrame() {
if (setVisible_apen) {
if (setFrameSequence != null) {
if (time < setFrameSequence.length - 1) {
time++;
} else {
time = 0;
}
setFrameY = (setFrameSequence[time] /
(picture.getWidth() / picture_getWidth)) *
-picture_getHeight;
setFrameX = (setFrameSequence[time] -
((setFrameSequence[time] /
(picture.getWidth() / picture_getWidth)) *
(picture.getWidth() / picture_getWidth))) *
-picture_getWidth;
} else {
setFrameX -= picture_getWidth;
if (setFrameX + picture.getWidth() <= picture_movex) {
setFrameX = 0;
setFrameY -= picture_getHeight;
}
if (setFrameY + picture.getHeight() <= picture_movey) {
setFrameY = 0;
}
}
}
}
/*************************得到当前帧数********************************/
public int getFrame() {
if (setVisible_apen) {
if (setFrameSequence != null) {
return setFrameSequence[time];
} else {
return -(((setFrameY / picture_getHeight) *
(picture.getWidth() / picture_getWidth)) +
(setFrameX / picture_getWidth));
}
} else {
return 0;
}
}
/*************************精灵和精灵的碰撞*********************************/
public boolean collidesWith(Sprite s) {
if (setVisible_apen) {
if (picture_movex + (picture_getWidth + specifiedx) >
s.picture_movex &&
picture_movex + specifiedgetWidth < s.picture_movex +
s.picture_getWidth &&
picture_movey + (picture_getHeight + specifiedy) >
s.picture_movey &&
picture_movey + specifiedgetHeight < s.picture_movey +
s.picture_getHeight) {
return true;
} else {
return false;
}
} else {
return false;
}
}
/*************************精灵和图片的碰撞*********************************/
public boolean collidesWith(Image s, int x, int y) {
if (setVisible_apen) {
if (picture_movex + (picture_getWidth + specifiedx) > x &&
picture_movex + specifiedgetWidth < x + s.getWidth() &&
picture_movey + (picture_getHeight + specifiedy) > y &&
picture_movey + specifiedgetHeight < y + s.getHeight()) {
return true;
} else {
return false;
}
} else {
return false;
}
}
/*************************精灵和地图的碰撞*********************************/
public boolean collidesWith(TiledLayer t) {
fanhui = false;
if (setVisible_apen) {
for (int i = 0; i < t.map_hua.length; i++) {
for (int l = 0; l < t.map_hua[0].length; l++) {
if (t.map_hua[i][l] != 0) {
if (picture_movex + (picture_getWidth + specifiedx) >
l * t.Width &&
picture_movex + specifiedgetWidth < l * t.Width +
t.Width &&
picture_movey + (picture_getHeight + specifiedy) >
i * t.Height &&
picture_movey + specifiedgetHeight < i * t.Height +
t.Height) {
fanhui = true;
if (fanhui) {
break;
}
}
}
}
if (fanhui) {
break;
}
}
return fanhui;
} else {
return fanhui;
}
}
/*************************设计精灵的可见度*********************************/
public boolean setVisible(boolean a) {
setVisible_apen = a;
return setVisible_apen;
}
/*************************得到精灵的可见度*********************************/
public boolean isVisible() {
return setVisible_apen;
}
/*************************设计精灵得碰撞区域*********************************/
public void defineCollisionRectangle(int x, int y, int width, int height) {
specifiedx = x * -1;
specifiedy = y * -1;
specifiedgetWidth = picture_getWidth - width;
specifiedgetHeight = picture_getHeight - height;
}
}