📄 piecesprite.java
字号:
}
int aniLength = currentAnimation.length;
// CHECK IF ANIMATION IS OVER
if (currentFrameNr + 1 == aniLength)
{
// IF REPEAT THEN SET FRAME 0
if (isAnimationRepeat)
{
return setFrame( (short) 0);
}
isEndOfAnimation = true;
return -1;
}
return setFrame( (short) (currentFrameNr + 1));
}
else
{
return playNextFrame();
}
}
/**
* 播放下一帧,该方法用于播放大量帧数据的动画
*/
public int playNextFrame()
{
// CHECK IF WE CAN SWITCH FOR NEXT FRAME (IS FINISHED FRAME TIME)
if (startFrameTime + currentFrameTime > System.currentTimeMillis())
{
return -1;
}
int aniLength = CurrentAnimationFrameLength;
// CHECK IF ANIMATION IS OVER
if (currentFrameNr + 1 == aniLength)
{
// IF REPEAT THEN SET FRAME 0
if (isAnimationRepeat)
{
try
{
dis.close();
// is.close();
is = null;
dis = null;
is = getClass().getResourceAsStream(m_strBinData);
dis = new DataInputStream(is);
currentFrameNr = 0;
FrameIndex = 0;
//读取动画长度
short PlayerAnimationsLength = ReadShortFromBCC(dis);
CurrentAnimationFrameLength = ReadShortFromBCC(dis);
}
catch (IOException ex)
{
ex.printStackTrace();
}
return 0; //setFrame((short)0);
}
else
{
try
{
dis.close();
}
catch (IOException ex)
{
}
}
return -1;
}
return playNext();
}
int FrameIndex = 0;
private int playNext()
{
int globalOX = ReadShortFromBCC(dis) * m_rate / 100;
int globalOY = ReadShortFromBCC(dis) * m_rate / 100;
int time = ReadShortFromBCC(dis);
int value = ReadShortFromBCC(dis) * m_rate / 100;
int rx = ReadShortFromBCC(dis) * m_rate / 100;
int ry = ReadShortFromBCC(dis) * m_rate / 100;
int rx2 = ReadShortFromBCC(dis) * m_rate / 100;
int ry2 = ReadShortFromBCC(dis) * m_rate / 100;
//读取该Frame的包含的零件的长度
int CurrentFramePieceLength = ReadShortFromBCC(dis);
short frameData[][] = new short[CurrentFramePieceLength + 1][];
for (int k = 0; k < CurrentFramePieceLength + 1; k++)
{
if (k == 0)
{
frameData[k] = new short[8];
frameData[0][0] = (short) value;
frameData[0][1] = (short) time;
frameData[0][2] = (short) globalOX;
frameData[0][3] = (short) globalOY;
frameData[0][4] = (short) rx;
frameData[0][5] = (short) ry;
frameData[0][6] = (short) (rx2 - rx);
frameData[0][7] = (short) (ry2 - ry);
}
else
{
frameData[k] = new short[6];
int ox = ReadShortFromBCC(dis) * m_rate / 100;
int oy = ReadShortFromBCC(dis) * m_rate / 100;
rx = ReadShortFromBCC(dis) * m_rate / 100;
ry = ReadShortFromBCC(dis) * m_rate / 100;
rx2 = ReadShortFromBCC(dis) * m_rate / 100;
ry2 = ReadShortFromBCC(dis) * m_rate / 100;
frameData[k][0] = (short) rx;
frameData[k][1] = (short) ry;
frameData[k][2] = (short) (rx2 - rx);
frameData[k][3] = (short) (ry2 - ry);
frameData[k][4] = (short) ox;
frameData[k][5] = (short) oy;
}
}
currentFrameNr = (short) FrameIndex++;
currentFrame = frameData;
currentFrameValue = currentFrame[0][0];
currentFrameTime = currentFrame[0][1];
currentFrameGlobalX = currentFrame[0][2];
currentFrameGlobalY = currentFrame[0][3];
currentFrameCollisionX = currentFrame[0][4];
currentFrameCollisionY = currentFrame[0][5];
currentFrameCollisionW = currentFrame[0][6];
currentFrameCollisionH = currentFrame[0][7];
startFrameTime = System.currentTimeMillis();
return currentFrameValue;
}
public void paint(Graphics g, int x, int y, int Way)
{
if (currentFrame == null)
{
return;
}
int sx = x;
int sy = y;
// SKIP FIRST ARRAY (THIS IS FRAME SETTINGS DATA)
for (int i = 1; i < currentFrame.length; i++)
{
if (Way == TRANS_NONE)
{
// ADD GLOBAL AND FRAME OFFSET X
x = sx + currentFrameGlobalX + currentFrame[i][4];
// ADD GLOBAL AND FRAME OFFSET Y
y = sy + currentFrameGlobalY + currentFrame[i][5];
//这里得改进bug,如果绘制图片没有的部分将溢出
g.drawRegion(sprImage, currentFrame[i][0], currentFrame[i][1],
currentFrame[i][2], currentFrame[i][3], Way, x, y,
Graphics.LEFT | Graphics.TOP);
}
if (Way == TRANS_MIRROR)
{
// ADD GLOBAL AND FRAME OFFSET X
x = sx + currentFrameGlobalX - currentFrame[i][4] -
currentFrame[i][2];
// ADD GLOBAL AND FRAME OFFSET Y
y = sy + currentFrameGlobalY + currentFrame[i][5];
//using midp2.0 api
g.drawRegion(sprImage, currentFrame[i][0], currentFrame[i][1],
currentFrame[i][2], currentFrame[i][3], Way,
x, y, Graphics.LEFT | Graphics.TOP);
}
}
}
/**
* drawRegion
*
* @param g Graphics
* @param dg DirectGraphics
* @param img Image
* @param x_src int 图上的x作标
* @param y_src int 图上的y作标
* @param width int 图 的width
* @param height int 图 的height
* @param transform int 变换方式
* @param x_dest int 绘制坐标x
* @param y_dest int 绘制坐标Y
* @param anchor int 锚点
*/
static void drawRegion(Graphics g, Image img,
int x_src,
int y_src,
int width,
int height,
int transform,
int x_dest,
int y_dest,
int anchor)
{
int x = 0, y = 0;
int manipulation = 0;
int imgWidth = img.getWidth();
int imgHeight = img.getHeight();
int offsetx, offsety;
offsetx = 0;
offsety = 0;
if ( (anchor & HCENTER) != 0)
{
offsetx = - (width >> 1);
}
else if ( (anchor & RIGHT) != 0)
{
offsetx = -width;
}
if ( (anchor & VCENTER) != 0)
{
offsety = - (height >> 1);
}
else if ( (anchor & BOTTOM) != 0)
{
offsety = -height;
}
if (transform == TRANS_ROT90 || transform == TRANS_ROT270 ||
transform == TRANS_MIRROR_ROT90 || transform == TRANS_MIRROR_ROT270)
{
g.setClip(x_dest + offsetx, y_dest + offsety, height, width);
g.clipRect(x_dest + offsetx, y_dest + offsety, height, width);
}
else
{
g.setClip(x_dest + offsetx, y_dest + offsety, width, height);
g.clipRect(x_dest + offsetx, y_dest + offsety, width, height);
}
switch (transform)
{
case TRANS_NONE:
x = x_dest - x_src;
y = y_dest - y_src;
manipulation = 0;
break;
}
g.drawImage(img, x + offsetx, y + offsety, Graphics.LEFT | Graphics.TOP);
g.setClip(0, 0, g.getClipWidth(), g.getClipHeight());
}
public boolean isCollision(int sprite1PosX, int sprite1PosY,
PieceSprite sprite2, int sprite2PosX,
int sprite2PosY)
{
int x1 = sprite1PosX + currentFrameCollisionX + currentFrameGlobalX;
int y1 = sprite1PosY + currentFrameCollisionY + currentFrameGlobalY;
int w1 = currentFrameCollisionW;
int h1 = currentFrameCollisionH;
int x2 = sprite2PosX + sprite2.currentFrameCollisionX +
sprite2.currentFrameGlobalX;
int y2 = sprite2PosY + sprite2.currentFrameCollisionY +
sprite2.currentFrameGlobalY;
int w2 = sprite2.currentFrameCollisionW;
int h2 = sprite2.currentFrameCollisionH;
return! (x1 + w1 < x2 || x1 > x2 + w2 || y1 + h1 < y2 || y1 > y2 + h2);
}
/**
* 检测当前动画是否播放完成
*/
public boolean bAnimationOver()
{
return isEndOfAnimation;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -