2014-02-28 3 views
0

나는 지금 잠시 동안 노력해 왔고, 나는 그 충돌 탐지가 작동하지 않는 것처럼 보였다. 순간적으로 나는 그것이 작동하지 않는다는 사실 때문에 주석 처리했다.충돌 감지 작동 방법

다음은 내가 주석을 제거 할 때 발생하는 오류입니다.

오류

Argument 1: cannot convert from 'System.Collections.Generic.List<OOPigEatingApple.Apple>' to 'System.Windows.Controls.ContentControl' 

The best overloaded method match for 'OOPigEatingApple.MainPage.DetectCollision(System.Windows.Controls.ContentControl, System.Windows.Controls.ContentControl)' has some invalid arguments 

The best overloaded method match for 'OOPigEatingApple.MainPage.RemoveApple(OOPigEatingApple.Apple)' has some invalid arguments 

문제는 내가 인해 이해의 부족으로 이러한 오류를 고칠 수있다, 이러한 문제를 정류 도움이 환상적 일 것입니다.

코드

namespace game 
{ 
    public partial class MainPage : UserControl 
    { 
     Pig myPig; 
     List<Apple> myapples; 

     private int appleTimer = 0; 
     //int appleCount = 0; 

     public MainPage() 
     { 
      InitializeComponent(); 
      myPig = new Pig(); 
      myapples = new List<Apple>(); 

      Image myImg = new Image(); 
      myImg.Source = new BitmapImage(new Uri("pig3.png", UriKind.Relative)); 
      myImg.Width = 80; 
      myImg.Height = 60; 
      myPig.Content = myImg; 
      LayoutRoot.Children.Add(myPig); 
      Canvas.SetLeft(myPig,100); 
      Canvas.SetTop(myPig, 50); 

      foreach (Apple a in myapples) 
      { 
       LayoutRoot.Children.Add(a); 
      } 
      CompositionTarget.Rendering += new EventHandler(CompositionTarget_Rendering); 
     } 

     public void AddApple(Apple a) 
     { 
      myapples.Add(a); 
      LayoutRoot.Children.Add(a); 
     } 

     public void RemoveApple(Apple a) 
     { 
      myapples.Remove(a); 
      LayoutRoot.Children.Remove(a); 
     } 

     public void CompositionTarget_Rendering(object sender, EventArgs e) 
     { 
      appleTimer += 1; 
      if (appleTimer > 60) 
      { 
       appleTimer = 0; 
       AddApple(new Apple()); 
      } 

      for (int indx = 0; indx < myapples.Count; indx++) 
      { 
       myapples[indx].Update(LayoutRoot); 

      } 

      // if (DetectCollision(myapples, myPig)) 
      { 
      //  RemoveApple(myapples); 
      }  
     } 




     private void UserControl_KeyDown(object sender, KeyEventArgs e) 
     { 
      if (e.Key == Key.Up) 
       this.myPig.Move(Direction.Up); 
      if (e.Key == Key.Down) 
       this.myPig.Move(Direction.Down); 
      if (e.Key == Key.Left) 
       this.myPig.Move(Direction.Left); 
      if (e.Key == Key.Right) 
       this.myPig.Move(Direction.Right); 
     } 

     public bool DetectCollision(ContentControl ctrl1, ContentControl ctrl2) 

     { 

      Rect ctrl1Rect = new Rect(
        new Point(Convert.ToDouble(ctrl1.GetValue(Canvas.LeftProperty)), 
             Convert.ToDouble(ctrl1.GetValue(Canvas.TopProperty))), 
           new Point((Convert.ToDouble(ctrl1.GetValue(Canvas.LeftProperty)) + ctrl1.ActualWidth), 
             (Convert.ToDouble(ctrl1.GetValue(Canvas.TopProperty)) + ctrl1.ActualHeight)) 
         ); 

      Rect ctrl2Rect = new Rect(
     new Point(Convert.ToDouble(ctrl2.GetValue(Canvas.LeftProperty)), 
             Convert.ToDouble(ctrl2.GetValue(Canvas.TopProperty))), 
           new Point((Convert.ToDouble(ctrl2.GetValue(Canvas.LeftProperty)) + ctrl2.ActualWidth), 
             (Convert.ToDouble(ctrl2.GetValue(Canvas.TopProperty)) + ctrl2.ActualHeight)) 
         ); 

      ctrl1Rect.Intersect(ctrl2Rect); 
      return !(ctrl1Rect == Rect.Empty); 
     } 
    } 
} 
+0

오류가 발생한 줄은 무엇입니까? – MikeH

답변

1
 for (int indx = 0; indx < myapples.Count; indx++) 
     { 
      myapples[indx].Update(LayoutRoot); 
      bool collided = DetectCollision(myapples[indx], myPig); 
      if (collided) { 
       // the apple and the pig have collided. Do something here. 
      } 

     } 

이보십시오. 여기

  if (collided) { 
       // the apple and the pig have collided. Do something here. 
       RemoveApple(myapples[indx]); 
       indx --; 
      } 
+0

환상적입니다. 그리고 대답에 대한 설명을 주셔서 감사합니다 – Beep

1

if (DetectCollision(myapples, myPig)) 

:

편집 : 당신은 루프 내 사과를 제거하려는 경우 당신이 사과를 생략하지 않도록 또한, 확실히 감소 인덱서을 첫 번째 매개 변수로 사과 목록을 전달하고 있습니다. 그러나 사용자의 방법

public bool DetectCollision(ContentControl ctrl1, ContentControl ctrl2) 

은 첫 번째 매개 변수로 콘텐츠 컨트롤이 필요합니다. 'System.Windows.Controls.ContentControl'가

작동하지 않습니다 'System.Collections.Generic.List'에서 변환 할 수 없습니다에 : 즉,

인수 한 불평 할 때 컴파일러가 무엇을 의미하는지의 . 콘텐츠 컨트롤은 사용자 인터페이스 요소이며 목록은 데이터 구조입니다. 그들은 근본적으로 다른 두 가지입니다. 사과 은 사과가 콘텐츠 컨트롤에서 파생 된 일 수 있습니다.이 경우 전체 목록 대신 사과 하나를 사과로 전달해야합니다.

+0

설명 주셔서 감사합니다. 매우 도움이되는 – Beep