|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
令人可喜的是java现在已经开源了,所以我想我上述的想法也许有一天会实现,因为java一直都是不断创新的语言,每次创新都会给我们惊喜,这也是我喜欢java的一个原因。
/**
为MIDP1.0写的Sprite类的晋级版本*<p>Title:精灵类</p>**<p>Description:</p>**<p>Copyright:Copyright(c)2004</p>**<p>Company:</p>**@authornotattributable*@version1.0*/importjavax.microedition.lcdui.*;publicclassMySprite{finalpublicstaticintByCollideXY=0;finalpublicstaticintByCollideArea=1;finalpublicstaticintByCollideRadius=2;publicImageimage;publicintpositionX;publicintpositionY;publicintnumFrames;publicintcurrentFrame;publicint[][]framesInfo;//本版本吧各个帧的尺寸存到数组中,而不是象1.0中等年夜publicbooleanvisible=true;publicintDX;publicintDY;publicintcollideX=0;//当两个脚色交织横向间隔凌驾collideX时以为已碰撞publicintcollideY=0;//当两个脚色交织纵向间隔凌驾collideY时以为已碰撞publicintcollideArea=0;//当两个脚色相互进侵面积凌驾collideArea时以为已碰撞(注重:这里的进侵面积!=交织面积)publicintcollideRadius=0;//碰撞半径publicintstate=0;
publicMySprite(Imageimage,intnumFrames)throwsException{this.image=image;this.numFrames=numFrames;//setframeInfoframesInfo=newint[numFrames][4];for(inti=0;i<numFrames;i++){framesInfo[i][0]=i*image.getWidth()/numFrames;//左上角X坐标framesInfo[i][1]=0;//左上角Y坐标framesInfo[i][2]=image.getWidth()/numFrames;//本帧宽度framesInfo[i][3]=image.getHeight();//本帧高度}///////////////this.currentFrame=0;}publicvoiddraw(CanvastheCanvas,Graphicsg){if(visible){g.setClip(positionX,positionY,framesInfo[currentFrame][2],framesInfo[currentFrame][3]);g.drawImage(image,positionX-framesInfo[currentFrame][0],positionY-framesInfo[currentFrame][1],0);g.setClip(0,0,theCanvas.getWidth(),theCanvas.getHeight());}}publicvoidstep(){if(visible){this.positionX+=DX;this.positionY+=DY;}}publicbooleancollidesWith(MySpritesprite){returnthis.collidesWith(sprite,ByCollideArea);}publicbooleancollidesWith(MySpritesprite,intCheckType){if(!visible||!sprite.visible){returnfalse;}if(CheckType==ByCollideArea){//当两个脚色相互进侵面积凌驾collideArea时以为已碰撞(注重:这里的进侵面积!=交织面积)intinX=sprite.positionX+sprite.framesInfo[sprite.currentFrame][2]-this.positionX>this.positionX+this.framesInfo[currentFrame][2]-sprite.positionX?this.positionX+this.framesInfo[currentFrame][2]-sprite.positionX:sprite.positionX+sprite.framesInfo[sprite.currentFrame][2]-this.positionX;intinY=sprite.positionY+sprite.framesInfo[sprite.currentFrame][3]-this.positionY>this.positionY+this.framesInfo[currentFrame][3]-sprite.positionY?this.positionY+this.framesInfo[currentFrame][3]-sprite.positionY:sprite.positionY+sprite.framesInfo[sprite.currentFrame][3]-this.positionY;if(inX>0&&inY>0&&inX*inY>this.collideArea+sprite.collideArea){returntrue;}else{returnfalse;}}elseif(CheckType==ByCollideXY){if(sprite.positionX+sprite.framesInfo[sprite.currentFrame][2]-sprite.collideX>this.positionX+this.collideX&&this.positionX+this.framesInfo[currentFrame][2]-this.collideX>sprite.positionX+sprite.collideX&&sprite.positionY+sprite.framesInfo[sprite.currentFrame][3]-sprite.collideY>this.positionY+this.collideY&&this.positionY+this.framesInfo[currentFrame][3]-this.collideY>sprite.positionY+sprite.collideY){returntrue;}else{returnfalse;}}else//if(CheckType==ByCollideRadius){intinX=Math.abs(this.positionX+this.framesInfo[currentFrame][2]/2-sprite.positionX-(sprite.framesInfo[sprite.currentFrame][2]+1)/2);intinY=Math.abs(this.positionY+this.framesInfo[currentFrame][3]/2-sprite.positionY-(sprite.framesInfo[sprite.currentFrame][3]+1)/2);if(inX*inX+inY*inY<(collideRadius+sprite.collideRadius)*(sprite.collideRadius+sprite.collideRadius)){returntrue;}else{returnfalse;}}}publicbooleancollidesWith(Imageimage,intx,inty){returnthis.collidesWith(image,x,y,ByCollideArea);}
publicbooleancollidesWith(Imageimage,intx,inty,intCheckType){if(!visible){returnfalse;}
if(CheckType==ByCollideArea){//当两个脚色相互进侵面积凌驾collideArea时以为已碰撞(注重:这里的进侵面积!=交织面积)intinX=x+image.getWidth()-this.positionX>this.positionX+this.framesInfo[currentFrame][2]-x?this.positionX+this.framesInfo[currentFrame][2]-x:x+image.getWidth()-this.positionX;intinY=y+image.getHeight()-this.positionY>this.positionY+this.framesInfo[currentFrame][3]-y?this.positionY+this.framesInfo[currentFrame][3]-y:y+image.getHeight()-this.positionY;if(inX>0&&inY>0&&inX*inY>this.collideArea){returntrue;}else{returnfalse;}}elseif(CheckType==ByCollideXY){if(x+image.getWidth()>this.positionX&&this.positionX+this.framesInfo[currentFrame][2]-this.collideX>x&&y+image.getHeight()>this.positionY&&this.positionY+this.framesInfo[currentFrame][3]-this.collideY>y){returntrue;}else{returnfalse;}}else//if(CheckType==ByCollideRadius){intinX=Math.abs(this.positionX+this.framesInfo[currentFrame][2]/2-x-(image.getWidth()+1)/2);intinY=Math.abs(this.positionY+this.framesInfo[currentFrame][3]/2-y-(image.getHeight()+1)/2);if(inX*inX+inY*inY<collideRadius*collideRadius){returntrue;}else{returnfalse;}}}}
JAVA是一种可以撰写跨平台应用软件的面向对象的程序设计语言,由升阳(SunMicrosystems)公司的詹姆斯·高斯林(JamesGosling)等人于1990年代初开发。 |
|