2016-05-16 3 views
0

こんにちは仲間のプログラマー!私はあなたを気にして申し訳ありませんが、ゲームを作るための学校プロジェクトがあります。私は自分のキャラクター(プレイヤー)を敵の弾丸からダメージを受けさせることだけです。敵からのダメージを受ける方法(箇条書き)

ここに私の敵のクラスがあります。私の敵のクラスは、敵が銃弾を撃つようにします。 (私は敵を作るためにこれらのチュートリアルに従いましたhttps://www.youtube.com/watch?v=_TlnUM-uhSIhttps://www.youtube.com/watch?v=tfiKwOo_4xo

あなたが私に飛び乗る前に私は答えを探していましたが、 XNAは作成したいものが非常に限られているので、それは私にとって非常に困難です。

class Enemies 
{ 
    public Texture2D texture; 
    public Vector2 position; 
    public Vector2 velocity; 

    public bool isVisible = true; 

    Random random = new Random(); 
    int randX, randY; 
    float temp_bulletenemyX; 
    float temp_bulletenemyY; 

    // bullets 
    private List<Bullets> bullets = new List<Bullets>(); 
    Texture2D bulletTexture; 

    public Enemies(Texture2D NewTexture, Vector2 NewPosition, Texture2D newBulletTexture) 
    { 
     texture = NewTexture; 
     position = NewPosition; 

     randY = random.Next(-4, 4); 
     randX = random.Next(-4, -1); 

     velocity = new Vector2(randX, randY); 

     bulletTexture = newBulletTexture; 
    } 

    float shoot = 0; 

    public void update(GraphicsDevice graphics, GameTime gameTime) 
    { 
     KeyboardState keyboardState = Keyboard.GetState(); 
     position += velocity; 

     if (position.Y <= 0 || position.Y >= graphics.Viewport.Height - texture.Height) 
      velocity.Y = -velocity.Y; 

     if (position.X < 0 - texture.Width) 
      isVisible = false; 

     shoot += (float)gameTime.ElapsedGameTime.TotalSeconds; 
     if (shoot > 1) 
     { 
      shoot = 0; 
      Shootbullet(); 

     } 
     updateBullets(); 

    } 

    public void updateBullets() 
    { 
     foreach (Bullets bullet in bullets) 
     { 
      bullet.position += bullet.velocity; 
      temp_bulletenemyX = bullet.position.X; 
      temp_bulletenemyY = bullet.position.Y; 
      if (bullet.position.X < 0) 
       bullet.isVisible = false; 

    } 
     for (int i = 0; i < bullets.Count; i++) 
      if(!bullets[i].isVisible) 
      { 
       bullets.RemoveAt(i); 
       i--; 
      } 
} 
    public void Shootbullet() 
    { 
     Bullets newBullet = new Bullets(bulletTexture); 
     newBullet.velocity.X = velocity.X - 3f; 
     newBullet.position = new Vector2(position.X + newBullet.velocity.X, position.Y + (texture.Height/2) - (bulletTexture.Height/2)); 

     newBullet.isVisible = true; 
     if (bullets.Count() < 3) // hur många skott den skall skjuta 
      bullets.Add(newBullet); 


    } 
    public void draw(SpriteBatch spriteBatch) 
    { 
     foreach (Bullets bullet in bullets) 
      bullet.Draw(spriteBatch); 
     spriteBatch.Draw(texture, position, Color.White); 

    } 

    public float PosX 
    { 
     get 
     { 
      return position.X; 
     } 
    } 
    public Vector2 Pos 
    { 
     get 
     { 
      return position; 
     } 
    } 
    public float PosY 
    { 
     get 
     { 
      return position.Y; 
     } 
    } 

    public List<Bullets> GetbulletList 
    { 
     get{ 
      return bullets; 
     } 
    } 

    public Texture2D text 
    { 
     get 
     { 
      return texture; 
     } 



    } 

    public Texture2D BulletText 
    { 
     get 
     { 
      return bulletTexture; 
     } 

私が作ろうとしている基本的なGame1コードは、私のキャラクターがダメージを受ける可能性があると主張しています。すべてが台無しである場合

  player.rectangle = new Rectangle(Convert.ToInt32(player.PosX), Convert.ToInt32(player.PosY), player.text.Width, player.text.Height); 

    foreach (Enemies bullet in enemies.ToList()) 
     { 

       rec_bullet = new Rectangle(Convert.ToInt32(bullet.GetbulletList.ElementAt(i).position.X), Convert.ToInt32(bullet.GetbulletList.ElementAt(i).position.Y), nyenemy.BulletText.Width, nyenemy.BulletText.Height); 
       hit = CheckCollision(rec_bullet, player.rectangle); 
       if (hit == true) 
       { 

       player.health -= 10; 
        hit = false; 

      } 
      i++; 

、私は非常に申し訳ありませんが、アイブ氏は、多くのチュートリアルと私自身のコーディングの一部を以下介して一緒にすべてを入れています。私がフォーラムのルールに違反している場合、私はまた非常に残念です。私はここで新しいです。

ベストアンサーKiar。

+0

あなたは私たちにCheckCollisionのコードを渡す必要がありますし、あなたのコードがどのように機能しないのかも教えてください。弾丸はあなたの選手を通過するだけですか、エラーが表示されるのですか、それとも何かが起こっていますか?あなたの弾丸は飛んでいるか、立っていますか?あなたのプレーヤーは動くことができますか?あるいは、それをどのように一緒に働かせるかのアイデアの問題ですか?問題についてより正確に記述してください。 – Monset

+0

また、デバッガを使用してソースを実行してください。 Visual Studioを使ったデバッグは非常に簡単で、値が何か、何が起こっているのか把握するのに役立ちます。また、WindowsやVisual StudioのConsole.WriteLine()では、変数を出力することができます:弾丸がアクティブなときはX/Yであり、死んでいるときにも表示されます。 – Neil

答えて

2

私は勇気づけられ、次のことを提案します - コードの抜粋を見てください。

Game1のコードでは、あなたは敵の道を進んでいますが、敵ごとに弾丸のリストを調べていないようです。あなたはElementAt(..)に 'i'をつけています。それに1を加え、次の敵に行き、ElementAt(..)だけをインクリメントした 'i'で取得します。

私の理解から - コードの流れは次のようになります。

for each enemy in enemy list 
    for each bullet in enemy bullet list 
     Check Collision with Player 
      if collision then adjust player health 
0

あなたのコードと間違っていくつかあります。まず、Bulletクラスには、foreachループで呼び出されるbulletUpdate()メソッドが含まれている必要があります。

foreach (Bullet b in bullets) 
{ 
b.update(/*parameters here*/); 
} 

あなたの弾丸リストは、Game1.csまたはEnemyManagerクラスに格納された公開リストである必要があります。各敵ではありません。それぞれの敵にそのリストへの参照を渡します。これにより、発射時にリストに箇条書きを追加することができます。

さらに、衝突テストは、各箇条書きと自分との間の交差テストに過ぎません。もう一度、別のforeachループ。

foreach(Bullet b in bullets) 
{ 
//where playerRect is the hitbox for your player and b.Rect is the hitbox for your bullet 
if(playerRect.Intersects(b.Rect) 
player.healh-=damage; 
} 

希望します。

関連する問題