2017-11-28 10 views
2

私はゲームを作ろうとしています。私はそれぞれに撃つ射撃砲(ボール)がオブジェクトとの個々の衝突とゲーム画面のマージンを持っていることを望んでいますが、今ではボールがゲーム画面のマージンに衝突するとボールごとの速度xまたはvelocity.yヒットの速度が変更されている個々のボールだけではなく、衝突をシミュレートするために-1が乗算されます。 (1つのボールがヒットすると、すべてのボールの速度が影響を受けます)。発射物はGameWorldクラスのArrayListに格納され、射撃時にはすべて同じ速度が与えられます。あなたは射撃する方向を選ぶと、すべてのボールが射撃されるまで、ボールはその方向に0.3秒ごとに射撃されます。ここでArrayList内のプロジェクタは、衝突時に個別に変更されません。どうすれば修正できますか?

は私の発射クラスからのコードです:ここでは

package com.trimble.gameobjects; 

import com.badlogic.gdx.Gdx; 
import com.badlogic.gdx.math.Vector2; 
import com.trimble.gameworld.GameWorld; 

public class Projectile { 
    private Vector2 velocity, acceleration; 
    private Vector2 position; 
    private final float RADIUS = 3f; 
    private boolean active, shot; 
    private GameWorld world; 
    private float theta; 

    public Projectile(float x, GameWorld world) { 
     this.world = world; 
     this.position = new Vector2(); 
     this.position.x = x; 
     this.position.y = world.getGameRect().y - 3; 
     // hitbox = new Circle(position, 3f); 
     this.velocity = new Vector2(); 
     this.acceleration = new Vector2(0, 0.5f); 
     this.active = false; 
     this.shot = false; 
    } 

    public void update(float delta) { 
     if (active) { 
      position.add(velocity.cpy().scl(delta)); 
      velocity.add(acceleration.cpy().scl(delta)); 

      // left 
      if (this.position.x <= 3) { 
       Gdx.app.log("hit", " left"); 
       this.position.x = 3; 
       this.velocity.x *= -1; 
      } 
      // right 
      else if (this.position.x >= world.getGameRect().width - 3) { 
       Gdx.app.log("hit", " right"); 
       this.position.x = world.getGameRect().width - 3; 
       this.velocity.x *= -1; 
      } 
      // top 
      if (this.position.y < world.getGameRect().y + world.getGameRect().height + 3) { 
       Gdx.app.log("hit", " top"); 
       this.position.y = world.getGameRect().y + world.getGameRect().height + 3; 
       this.velocity.y *= -1; 
      } 
      // bottom 
      else if (this.position.y > world.getGameRect().y - 3 && velocity.y > 0) { 
       Gdx.app.log("hit", " bottom"); 
       if (!this.world.hasTouched()) { 
        this.world.getBaseCircle().setPositionX(position.x); 
        this.world.setTouched(true); 
       } 
       zeroVelocity(); 
       this.active = false; 
       this.position = world.getBaseCirclePos(); 
       this.world.addInactive(); 
      } 
     }  
    } 

    public Vector2 getVelocity() { 
     return velocity; 
    } 

    public float getVelocityX() { 
     return velocity.x; 
    } 

    public float getVelocityY() { 
     return velocity.y; 
    } 

    public void setVelocity(Vector2 velocity) { 
     this.velocity = velocity; 
    } 

    public void setVelocityX(float x) { 
     this.velocity.x = x; 
    } 

    public void setVelocityY(float y) { 
     this.velocity.y = y; 
    } 

    public float getTheta() { 
     return theta; 
    } 

    public void setTheta(float theta) { 
     this.theta = theta; 
    } 

    public void zeroVelocity() { 
     this.velocity.x = 0; 
     this.velocity.y = 0; 
    } 

    public Vector2 getPosition() { 
     return position; 
    } 

    public void setPositionY(float y) { 
     this.position.y = y; 
    } 

    public void setPositionX(float x) { 
     this.position.x = x; 
    } 

    public void setPosition(Vector2 position) { 
     this.position = position; 
    } 

    public boolean isActive() { 
     return active; 
    } 

    public void setActive(boolean active) { 
     this.active = active; 
    } 

    public float getR() { 
     return RADIUS; 
    } 

    public boolean wasShot() { 
     return shot; 
    } 

    public void setShot(boolean shot) { 
     this.shot = shot; 
    } 
} 

は、プレイヤーが撮影したときに呼び出されGameWorldのコードです:

public void transitionHasShot() { 
    if (currentState == GameState.READY) { 
     currentState = GameState.HASSHOT; 

     velocity.y = (float) (-165 * Math.sin(theta)); 
     velocity.x = (float) (165 * Math.cos(theta)); 

     baseCircle.setActive(false); 
     for (Projectile p : projectiles) { 
      p.setVelocity(velocity); 
     } 
     projectiles.get(0).setActive(true); 
    } 
} 

は、あなたがこれ以上の情報が必要な場合は、私に教えてください。

+1

あなたが(に発射を追加する)あなたはどのように定義するか追加して作成できますあなたのprojectiles ArrayList?あなたが「彼らが撃たれたときに同じ速度を与えられた」と言うとき。すべてのProjectileが1つの 'Vector2 velocity'オブジェクトインスタンスを共有していますか?それは確かにあなたの記述された行動を説明するでしょう –

+0

私は問題は、すべての速度がGameWorldに属する同じ速度オブジェクトを参照していたと思います。私はすべての発射物がGameWorldの速度のコピーを受け取るようにそれを作ったが、これは問題を修正した、あなたの時間をありがとう。 –

答えて

0

I)は、(transitionHasShotにコードを変更することで問題を解決:に

変更

for (Projectile p : projectiles) { 
      p.setVelocity(velocity); 
} 

を:

for (Projectile p : projectiles) { 
      p.setVelocity(velocity.cpy()); 
} 
関連する問題