Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何让碰撞检测工作?_C#_Silverlight_Oop_Collision Detection - Fatal编程技术网

C# 如何让碰撞检测工作?

C# 如何让碰撞检测工作?,c#,silverlight,oop,collision-detection,C#,Silverlight,Oop,Collision Detection,我已经尝试了一段时间,我似乎无法让我的碰撞检测工作,目前我有它的评论,因为它不工作的事实 以下是我在取消注释时遇到的错误 错误 Argument 1: cannot convert from 'System.Collections.Generic.List<OOPigEatingApple.Apple>' to 'System.Windows.Controls.ContentControl' The best overloaded method match for 'OOPigEa

我已经尝试了一段时间,我似乎无法让我的碰撞检测工作,目前我有它的评论,因为它不工作的事实

以下是我在取消注释时遇到的错误

错误

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
参数1:无法从“System.Collections.Generic.List”转换为“System.Windows.Controls.ContentControl”
与“OOPIGEATIGAPPLE.MainPage.DetectCollision(System.Windows.Controls.ContentControl,System.Windows.Controls.ContentControl)”匹配的最佳重载方法具有一些无效参数
与“OOPigEatingApple.MainPage.RemoveApple(OOPigEatingApple.Apple)”匹配的最佳重载方法具有一些无效参数
问题是,由于缺乏理解,我无法修复这些错误,任何有助于纠正这些问题的帮助都将是非常棒的

代码

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);
        }
    }
}
名称空间游戏
{
公共部分类主页面:UserControl
{
猪myPig;
列出我的苹果;
私有int appleTimer=0;
//int appleCount=0;
公共主页()
{
初始化组件();
myPig=新的Pig();
myapples=新列表();
Image myImg=新图像();
myImg.Source=新的位图图像(新的Uri(“pig3.png”,UriKind.Relative));
myImg.宽度=80;
myImg.高度=60;
myPig.Content=myImg;
LayoutRoot.Children.Add(myPig);
Canvas.SetLeft(myPig,100);
帆布。机顶盒(myPig,50);
foreach(我的苹果中的苹果a)
{
LayoutRoot.Children.Add(a);
}
CompositionTarget.Rendering+=新事件处理程序(CompositionTarget\u Rendering);
}
苹果公司(苹果a公司)的公共空间
{
添加(a);
LayoutRoot.Children.Add(a);
}
公共无效删除(苹果a)
{
我的苹果。移除(a);
LayoutRoot.Children.Remove(a);
}
public void CompositionTarget_呈现(对象发送方,事件参数e)
{
appleTimer+=1;
如果(appleTimer>60)
{
appleTimer=0;
AddApple(新苹果());
}
for(int indx=0;indx
这里

您正在传递一个苹果列表作为第一个参数。然而,你的方法

public bool DetectCollision(ContentControl ctrl1, ContentControl ctrl2)
需要一个内容控件作为第一个参数。这就是编译器抱怨的意思

参数1:无法从“System.Collections.Generic.List”转换为“System.Windows.Controls.ContentControl”


那不行。内容控件是用户界面元素,而列表是数据结构。它们是两个根本不同的东西。可能是您的苹果源于内容控件,在这种情况下,您仍然需要将一个苹果传递给该方法,而不是整个列表。

错误发生在哪一行?谢谢,太棒了。谢谢你在回复中的解释
            if (collided) {
                // the apple and the pig have collided. Do something here.
                RemoveApple(myapples[indx]);
                indx --;
            }
if (DetectCollision(myapples, myPig))
public bool DetectCollision(ContentControl ctrl1, ContentControl ctrl2)