2014-02-28 65 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) 

預計內容控件作爲第一個參數。這時候,它抱怨

參數1編譯器的意思:無法從「System.Collections.Generic.List」轉換爲「System.Windows.Controls.ContentControl」

這是行不通的。內容控件是用戶界面元素,而List是數據結構。他們是兩個根本不同的東西。它可能是,你的蘋果派生自內容控件,在這種情況下,你仍然需要通過一個蘋果來代替的方法,而整個列表

+0

謝謝你的解釋。非常有幫助 – Beep