Title:Bullets
*Description: This Class used to create a group of random bullets and check collision with the plane. First Step,Create int[][] bulletsPos to recorded the info of the bullets. bulletsPos[0]----the type of the bullet. bulletsPos[1]----the X position of the bullet bulletsPos[2]----the Y position of the bullet Then,use initBulletsPos() to filled the info of bulletsPos[][]; newPos()---return a random bullets info. In the End,refreshBulletsPos(Sprite plane) to refresh the bullets Position,and check the collision. NOW THE QUESTION IS: Only a few bullets colliding with plane can be detect correct.The most of bullets can cross through the plane. I doubt the array,but i have no answer.Any reply will be thankful. *
Tony.Ming Email:Yesming@126.com
*2004
* */ import java.util.*; import javax.microedition.lcdui.*; import javax.microedition.lcdui.game.*; public class Bullets { private static final int STOP=0; private static final int MOVE=1; private int TotalBullets=8;//The total number of bullets private int SPEED=2; private int collisionLength=10; private int ScreenWidth; private int ScreenHeight; private BulletSprite bulletSprite; private int[][] bulletsPos; private Random rdm; private boolean isInitPos=false; private Runtime rt; public Bullets(int ScreenWidth,int ScreenHeight) { System.out.println("begin to create Bullets...."); this.ScreenWidth=ScreenWidth; this.ScreenHeight=ScreenHeight; this.bulletImage=Img; //init random num rdm=new Random(); //init Bullets Positon this.bulletsPos=new int[this.TotalBullets][3]; this.initBulletsPos(); //init bullet sprite this.bulletSprite=new BulletSprite(Img,Img.getWidth(),Img.getHeight()); System.out.println("init Bullets...ok"); } private int getRNDLength(boolean LengthWidth) { //create a random num,the boolean control the range of the random num. int rnd; if(LengthWidth) rnd=(rdm.nextInt()&0x7fffffff)%this.ScreenWidth; else rnd=(rdm.nextInt()&0x7fffffff)%this.ScreenHeight; return rnd; } private int getRNDBulletType() { //create a random bullet type. int BulletType; int rnd=(rdm.nextInt()&0x7fffffff)%100; if(rnd<25) BulletType=BulletSprite.BULLET_TYPE_LEFT; else if(rnd<75) BulletType=BulletSprite.BULLET_TYPE_TOP; else BulletType=BulletSprite.BULLET_TYPE_RIGHT; return BulletType; } private void initBulletsPos() { for(int i=0;i<this.TotalBullets;i++) { int[] tmp=this.newBullets(); bulletsPos[0]=tmp[0]; bulletsPos[1]=tmp[1]; bulletsPos[2]=tmp[2]; } this.isInitPos=true; System.out.println("init bullets pos...ok!"); } private int[] newBullets() { //create a random bullet info. int[] Pos=new int[3]; Pos[0]=this.getRNDBulletType(); switch (Pos[0]) { case BulletSprite.BULLET_TYPE_LEFT: Pos[1]=-5; Pos[2]=this.getRNDLength(false); break; case BulletSprite.BULLET_TYPE_TOP: Pos[1]=this.getRNDLength(true); Pos[2]=-5; break; case BulletSprite.BULLET_TYPE_RIGHT: Pos[1]=this.ScreenWidth+5; Pos[2]=this.getRNDLength(false); break; } return Pos; } private boolean isCollision(Sprite plane,int x,int y,int r) { boolean result=false; int planeXCenter=plane.getX()+12; int planeYCenter=plane.getY()+12; int bulletXCenter=x+3; int bulletYCenter=y+3; if(Math.abs(planeXCenter-bulletXCenter)<r) { if(Math.abs(planeYCenter-bulletYCenter)<r) { result=true; } } return result; } public boolean refreshBulletsPos(Sprite plane) { //System.out.println("Beginto refresh Bullets Pos"+this.nowBullets); boolean result=false;//is|is NOT collison with the plane if(!this.isInitPos) this.initBulletsPos(); //refresh the bullets pos,and check the collision for(int i=0;i<this.TotalBullets;i++) { switch (bulletsPos[0]) { case BulletSprite.BULLET_TYPE_LEFT: bulletsPos[1]+=SPEED; bulletsPos[2]+=SPEED; if(bulletsPos[2]>this.ScreenHeight) { int[] tmp=this.newBullets(); bulletsPos[0]=tmp[0]; bulletsPos[1]=tmp[1]; bulletsPos[2]=tmp[2]; } result=this.isCollision(plane,bulletsPos[1],bulletsPos[2],collisionLength); break; case BulletSprite.BULLET_TYPE_TOP: bulletsPos[2]+=SPEED; if(bulletsPos[2]>this.ScreenHeight) { int[] tmp=this.newBullets(); bulletsPos[0]=tmp[0]; bulletsPos[1]=tmp[1]; bulletsPos[2]=tmp[2]; } result=this.isCollision(plane,bulletsPos[1],bulletsPos[2],collisionLength); break; case BulletSprite.BULLET_TYPE_RIGHT: bulletsPos[1]-=SPEED; bulletsPos[2]-=SPEED; if(bulletsPos[2]<0) { int[] tmp=this.newBullets(); bulletsPos[0]=tmp[0]; bulletsPos[1]=tmp[1]; bulletsPos[2]=tmp[2]; } result=this.isCollision(plane,bulletsPos[1],bulletsPos[2],collisionLength); break; } } return result; } //paint the bullets public void paint(Graphics g) { for(int i=0;i<this.TotalBullets;i++) { this.bulletSprite.setPosition(this.bulletsPos[1],this.bulletsPos[2]); this.bulletSprite.paint(g); } } }