私は 1 つのプレイヤー スプライトといくつか (約 30) の他のスプライトが画面上で衝突することなくランダムに移動するゲームを作成する必要があります。衝突を検出するとすべての移動スプライトがロックされるため、衝突を回避するのに問題があります。以下は私のコードです..私のコードでは、2つのクラスを作成しました.1つはMainActivity
(これは、クラスを使用してBaseGameActivity
約30を拡張および作成し、NewSpriteオブジェクトの静的配列リスト「playerSprite」を含むメインクラスです(ランダムに作成された約30のオブジェクト) 2 つ目は(メソッドを拡張およびオーバーロードして、これらの移動が画面外に移動したり、それらが衝突したりするのを防ぎます)。AnimatedSprites
NewSprite
NewSprite
AnimatedSprite
onManagedUpdate()
AnimatedSprites
NewSprite
私はクラスだけを示しました..
public class NewSprite extends AnimatedSprite{
public PhysicsHandler physicsHandler;
Random randPos;
public NewSprite(final float pX, final float pY, final ITiledTextureRegio pTiledTextureRegion, final VertexBufferObjectManager pVertexBufferObjectManager) {
super(pX, pY, pTiledTextureRegion, pVertexBufferObjectManager);
randPos = new Random();
float x1;
float y1;
while((x1 = randPos.nextFloat()) < 0.8f);
while((y1 = randPos.nextFloat()) < 0.8f);
if(randPos.nextInt() % 2 == 0)
x1 *= -1;
if(randPos.nextInt() % 2 == 0)
y1 *= -1;
physicsHandler = new PhysicsHandler(this);
this.registerUpdateHandler(physicsHandler);
physicsHandler.setVelocity(x1 * 100, y1 * 100);
}
@Override
protected void onManagedUpdate(float pSecondsElapsed) {
// TODO Auto-generated method stub
if(this.mX < 0){
this.move++;
float x2,y2;
while((x2 = randPos.nextFloat()) < 0.8f);
while((y2 = randPos.nextFloat()) < 0.8f);
if(randPos.nextInt() % 2 == 0)
y2 *= -1;
physicsHandler.setVelocity( x2*40, y2*40);
this.prevX = this.mX;
this.prevY = this.mY;
}
else if(this.mY < 0){
this.move++;
float x2,y2;
while((x2 = randPos.nextFloat()) < 0.8f);
while((y2 = randPos.nextFloat()) < 0.8f);
if(randPos.nextInt() % 2 == 0)
x2 *= -1;
physicsHandler.setVelocity( x2*40, y2*40);
this.prevX = this.mX;
this.prevY = this.mY;
}
else if(this.mX > (MainActivity.CAMERA_WIDTH - MainActivity.MONSTER_WIDTH)){
this.move++;
float x2,y2;
while((x2 = randPos.nextFloat()) < 0.8f);
while((y2 = randPos.nextFloat()) < 0.8f);
x2 *= -1;
if(randPos.nextInt() % 2 == 0)
y2 *= -1;
physicsHandler.setVelocity( x2*40, y2*40);
this.prevX = this.mX;
this.prevY = this.mY;
}
else if(this.mY > (MainActivity.CAMERA_HEIGHT - MainActivity.MONSTER_HEIGHT)){
this.move++;
float x2,y2;
while((x2 = randPos.nextFloat()) < 0.8f);
while((y2 = randPos.nextFloat()) < 0.8f);
if(randPos.nextInt() % 2 == 0)
x2 *= -1;
y2 *= -1;
physicsHandler.setVelocity( x2*40, y2*40);
this.prevX = this.mX;
this.prevY = this.mY;
}
for(int i=0 ; i < MainActivity.playerSprite.size(); i++){
if(( MainActivity.playerSprite.get(i)).collidesWith(this) || this.collidesWith(MainActivity.playerSprite.get(i))){
float x2, y2;
while((x2 = randPos.nextFloat()) < 0.8f);
while((y2 = randPos.nextFloat()) < 0.8f);
if(randPos.nextInt() % 2 == 0)
x2 *= -1;
if(randPos.nextInt() % 2 == 0)
y2 *= -1;
this.physicsHandler.setVelocity( x2*100, y2*100);
break;
}
}
super.onManagedUpdate(pSecondsElapsed);
}