简体   繁体   English

C#中对象的碰撞检测

[英]Collision detection on Objects in C#

Ok, so I have got the start of my game almost working I have a object( pig ) that I control and have objects( apple ) that moves right to left and the position is random. 好了,我有我的比赛几乎工作,我有一个对象(起始pig ),我控制和拥有的对象( apple )是移动从右到左的位置是随机的。 This all works now I am trying to get the collision working. 现在所有这些都有效,我正在尝试使碰撞起作用。 Problem is that it's not working. 问题是它不起作用。

I have commented out the collision coding that is not working if anyone knows why its not working and how to get working please can you let me know how. 如果有人知道为什么它不起作用以及如何起作用,我已经注释掉了不起作用的碰撞编码,请您让我知道如何。

error when I uncomment: 我取消注释时出现错误:

The best overloaded method match for 'System.Windows.PresentationFrameworkCollection.Add(System.Windows.UIElement)' has some invalid arguments 最佳重载方法匹配'System.Windows.PresentationFrameworkCollection.Add(System.Windows.UIElement)'有一些无效的参数

My Code (Mainpage) 我的代码(主页)

namespace game { public partial class MainPage : UserControl { Pig myPig; 命名空间游戏{公共局部类MainPage:UserControl {Pig myPig; List 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);
        //LayoutRoot.Children.Add(myapples);
        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))
       {                  
          // LayoutRoot.Children.Remove(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);
    }
}

} }

You have a list of elements, not an element. 您有一个元素列表,而不是一个元素。 Loop through the list and add each element: 遍历列表并添加每个元素:

foreach (Apple a in myapples) {
  LayoutRoot.Children.Add(a);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM