2014-12-03 2 views
0

개체 목록과 총알 목록이 있습니다.목록으로 충돌 감지

list 개체는 Game1 클래스에 있습니다.

나는이 방법으로 목록에 원수를 추가 및 삭제 :

public void LoadEnemies() { 
     int Y = 100; 
     if (spawn >= 1) 
     { 
      spawn = 0; 
      if (objects.Count() < 4) 
       objects.Add(new enemyObjects(Content.Load<Texture2D>("obstacle"), new Vector2(1100, Y))); 
     } 

     for (int i = 0; i < objects.Count; i++) 
     { 
      if (!objects[i].isVisible) 
      { 
       //If the enemy is out of the screen delete it 
       objects.RemoveAt(i); 
       i--; 
      } 
     } 
    } 

그리고 나 또한 글 머리 기호 목록을 가지고 : public List<Bullet> bullets = new List<Bullet>(); 나는 총알 발사 방법 :

private void ShootFireBall() { 
     if (mCurrentState == State.Walking) 
     { 
      bool aCreateNew = true; 
      foreach (Bullet aBullet in bullets) 
      { 
       if (aBullet.bulletVisible == false) 
       { 
        aCreateNew = false; 
        aBullet.Fire(position + new Vector2(Size.Width/2, Size.Height/2), 
         new Vector2(200, 0), new Vector2(1, 0)); 
       } 
      } 

      if (aCreateNew) 
      { 
       Bullet aBullet = new Bullet(); 
       aBullet.LoadContent(contentManager, "bullet"); 
       aBullet.Fire(position + new Vector2(Size.Width/2, Size.Height/2), 
         new Vector2(200, 0), new Vector2(1, 0)); 
       bullets.Add(aBullet); 
      } 
     } 
    } 

을 문제는 사각형이 필요하므로 충돌이 있는지 확인할 수 있습니다. 2 목록과의 충돌을 어떻게 확인할 수 있습니까? 사각형으로 변환 할 수있는 방법이 있습니까? 몇 시간 동안 붙어있어 정말 알아낼 수 없습니다.

답변

2

나는 보통 모든 스프라이트를 일반적인 유형에서 파생 시켰습니다. Sprite, GameEntity, 뭐든간에.

public abstract class Sprite 
{ 
    public Vector2 Location { get; set; } 
    public Rectangle Bounds 
    { 
     get 
     { 
      return new Rectangle((int)Location.X, (int)Location.Y, 
           _texture.Width, _texture.Height); 
     } 
    } 

    private Texture2D _texture; 

    public Sprite(Texute2D texture) 
    { 
     _texture = texture; 
    } 
} 

public class enemyObjects : Sprite 
{ 

    // enemy-specific properties go here 

    public enemyObjects(Texture2D texture) 
     : base(texture) 
    { 
    } 
} 

public class Bullet : Sprite 
{ 

    // Bullet-specific properties go here 

    public Bullet(Texture2D texture) 
     : base(texture) 
    { 
    } 
} 

그런 다음 당신은 단순히 객체를 포함하는 사각형을 얻을 수 objects[i].Bounds을 사용할 수 있습니다 : 그 기본 형식 등, Bounds, Location 추천하기 추천

뭔가 속성을 노출합니다.