2012-01-09 3 views
0

G'day 전체 게임,XNA Game.Components의 하위 그룹을 선택하십시오.

내 작은 게임에는 5 개의 튀는 공과 1 명의 플레이어가 있습니다. 처음에 내가 먼저 수신 거부 공에 대한 코드를 작성하고 각 공 충돌 검출 방법이 있습니다

foreach (Bouncer bouncer in Game.Components) //For each bouncer component in the game... 
     { 
      if (bouncer != this)// Don't collide with myself 
      { 
       if (bouncer.collisionRectangle.Intersects(this.collisionRectangle)) 
       { 
        // How far apart of the positions of the top right hand corners of the sprites when they hit? 
        int deltaX = Math.Abs((int)this.position.X - (int)bouncer.position.X); 
        int deltaY = Math.Abs((int)this.position.Y - (int)bouncer.position.Y); 

        // This is the width and height of a sprite so when two sprites touch this is how far the corners are from each other. 
        int targetWidth = 80; 
        int targetHeight = 80; 

        // The following determins the type of collision (vert hit vs horiz hit) 
        // Because the app is driven by a game based timer the actual amount of sprite overlap when the collision detection occurs is variable. 
        // This bit of simple logic has a 10 pixel tollerance for a hit. 
        // If target - delta is > 10 it will be interpreted as overlap in the non-colliding axis. 
        // If both if statements are triggered it is interpreted as a corner collision resulting in both sprites rebounding back along the original paths. 

        if (targetWidth - deltaX < 10) // The hit is a side on hit. 
        { 
         this.velocity.X *= -1; 
        } 

        if (targetHeight - deltaY < 10) // The hit is a vertical hit 
        { 
         this.velocity.Y *= -1; 
        } 

        this.numberOfCollisions = this.numberOfCollisions + 1; 
       } 
      } 
     } 

     base.Update(gameTime); 
    } 

는 그럼 난 내 플레이어 구성 요소를 추가하고, 바퀴가 떨어졌다. 응용 프로그램은 OK 컴파일하지만 난 그것을 실행할 때 나는 InvalidCastException이와 메시지를 얻을 :이 충돌 검출기에서 플레이어 오브젝트를 포함하지 않을

Unable to cast object of type 'Bounce2.Player' to type 'Bounce2.Bouncer'. 

합니다.

Bouncer 개체를 통해 열거하고 다른 개체를 제외 할 수있는 방법이 있습니까?

감사합니다. Andrew.

답변

1

이 사용할 수 있습니다 : 당신이 너무 다른 목록에서 경비원 인스턴스를 저장할 수 있습니다

foreach (Bouncer bouncer in Game.Components.OfType<Bouncer>()) 

참고.

+0

감사합니다. 그것은 내가 필요한 것입니다. 고마워. –

관련 문제