📄 piecesprite.java
字号:
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);
// for(int kk=0;kk<8;kk++)
// System.out.print( " "+frameData[0][kk]);
// System.out.println();
}
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;
// for(int kk=0;kk<6;kk++)
// System.out.print(" "+frameData[k][kk]);
// System.out.println();
//
}
}
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];
//#if (NOKIA_S40_V1||MOTO_E398||NOKIA_S60_V1||NOKIA_S40_V2 ||NOKIA_S60_V2||SAMSUNG_E108||SAMSUNG_D508||SE_K700||MOTO_V600||MOTO_E680)
//# drawRegion(g,sprImage, currentFrame[i][0], currentFrame[i][1], currentFrame[i][2], currentFrame[i][3] , Way, x, y, Graphics.LEFT | Graphics.TOP);
//#
//#else //这里得改进bug,如果绘制图片没有的部分将溢出
g.drawRegion(sprImage, currentFrame[i][0], currentFrame[i][1], currentFrame[i][2], currentFrame[i][3], Way, x, y, Graphics.LEFT | Graphics.TOP);
//#endif
}
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];
//#if (NOKIA_S40_V1 ||NOKIA_S60_V1||NOKIA_S40_V2 ||NOKIA_S60_V2)
//#
//# drawRegion(g,sprImage, currentFrame[i][0], currentFrame[i][1], currentFrame[i][2], currentFrame[i][3] , Way,
//# x, y, Graphics.LEFT | Graphics.TOP);
//#
//#elif (SAMSUNG_E108)
//#
//#
//#
//#else //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);
//#endif
}
}
}
/**
* 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)
{
//#if (NOKIA_S40_V1 ||NOKIA_S60_V1||NOKIA_S40_V2 ||NOKIA_S60_V2)
//# DirectGraphics dg = DirectUtils.getDirectGraphics(g);
//#endif
int x = 0, y = 0;
int manipulation = 0;
int imgWidth = img.getWidth();
int imgHeight = img.getHeight();
int offsetx, offsety ;
offsetx = 0;
offsety = 0;
// int oldclipWidth=g.getClipWidth();
// int oldclipHeighh=g.getClipHeight();
// int oldclipX=g.getClipX();
// int oldclipY=g.getClipY();
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;
//#if (NOKIA_S40_V1 ||NOKIA_S60_V1||NOKIA_S40_V2 ||NOKIA_S60_V2)
//# case TRANS_MIRROR: //flipx
//# x = x_dest + x_src + width - imgWidth;
//# y = y_dest - y_src;
//# manipulation = DirectGraphics.FLIP_HORIZONTAL;
//# break;
//# case TRANS_ROT90:
//# x = x_dest - y_src;
//# y = y_dest + x_src + width - imgWidth;
//# manipulation = DirectGraphics.ROTATE_90;
//# break;
//# case TRANS_ROT180:
//# x = x_dest + x_src + width - imgWidth;
//# y = y_dest + y_src + height - imgHeight;
//# manipulation = DirectGraphics.ROTATE_180;
//# break;
//# case TRANS_MIRROR_ROT180:
//# x = x_dest - x_src;
//# y = y_dest + y_src + height - imgHeight;
//# manipulation = DirectGraphics.FLIP_VERTICAL;
//# break;
//# case TRANS_MIRROR_ROT90: //flipy
//# x = x_dest -( imgHeight -y_src- height);
//# y = y_dest - (imgWidth -width-x_src);
//# manipulation = DirectGraphics.FLIP_HORIZONTAL | DirectGraphics.ROTATE_90;
//#
//# break;
//# case TRANS_ROT270: //Rotate 270
//# x = x_dest + y_src+height - imgHeight;
//# y = y_dest-x_src ;
//# manipulation = DirectGraphics.ROTATE_270;
//# break;
//# case TRANS_MIRROR_ROT270: //Rotate 270
//# x = x_dest -y_src ;
//# y = y_dest-x_src;
//# manipulation = DirectGraphics.ROTATE_270|DirectGraphics.FLIP_HORIZONTAL;
//# break;
//#endif
}
//#if (NOKIA_S40_V1 ||NOKIA_S60_V1||NOKIA_S40_V2 ||NOKIA_S60_V2)
//# dg.drawImage(img,x+offsetx,y+offsety,LEFT|TOP,manipulation);
//#else
g.drawImage(img,x+offsetx,y+offsety,Graphics.LEFT|Graphics.TOP);
//#endif
//g.setClip(oldclipX,oldclipY,oldclipWidth,oldclipHeighh);
g.setClip(0,0,SCREEN_WIDTH,SCREEN_HEIGHT);
// System.out.println("clip x "+ oldclipX+ " "+oldclipY+ " "+oldclipWidth + " "+oldclipHeighh);
}
// public void paint(Graphics g, int x, int y )
// {
// 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++)
// {
// x = sx + currentFrameGlobalX + currentFrame[i][4]; // ADD GLOBAL AND FRAME OFFSET X
// y = sy + currentFrameGlobalY + currentFrame[i][5]; // ADD GLOBAL AND FRAME OFFSET Y
//
// g.setClip(x, y, currentFrame[i][2], currentFrame[i][3]);
// g.drawImage(sprImage, x - currentFrame[i][0], y - currentFrame[i][1], 0);
// }
// }
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()
{
if (this.currentFrameNr==this.currentAnimation.length-1)
return true;
else return false;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -