在C#中使用IntersectsWith时出错。 [英] Got an error using IntersectsWith in C#.

查看:104
本文介绍了在C#中使用IntersectsWith时出错。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮助我如何检测两个物体之间的碰撞。起初我认为IntersectsWith会有所帮助,但我得到错误,说我的解决方案不包含IntersectsWith的定义。这是我的代码,它给了我错误:



Can someone help me on how i can detect collision between two objects. At first i thought IntersectsWith would help but i get the error saying my solution doesnot contain the definition for IntersectsWith. Here is my code that gives me errors:

class Car
    {
        private int _x,_y, _width, _height,_xvel;

     
        public Car(Random r, int y, int width, int height, int xvel)
        {
            this._x = r.Next(500)+20;
            //this._x = x;
            this._y = y;
            this._width = width;
            this._height = height;
            this._xvel = xvel;
        }

        public int X { get { return _x; } }
        public int Y { get { return _y; } }
        public int Width { get { return _width; } }
        public int Height{ get { return _height; } }
        public int Xvel { get { return _xvel; } }
    
        public void DrawCar(Graphics g)
        {
            g.DrawRectangle(Pens.Blue, new Rectangle(X, Y, Width, Height));            
        }
     
        public void MoveCar(int gamewidth)
        {
            if( _x + _width >= gamewidth)
            {              
                _x = 0;
            }
            _x = _x + _xvel;        
        }        
    }

    class Player
    {
         private int _x, _y, _width, _height, _xvel,_yvel;  

       
         public Player(int x, int y, int width,int height, int xvel, int yvel)
        {
            this._x = x;
            this._y = y;
            this._width = width;
            this._height = height;
            this._xvel = xvel;
            this._yvel = yvel;
        }

        public int X { get { return _x; } }
        public int Y { get { return _y; } }
        public int Width { get { return _width; } }
        public int Height{ get { return _height; } }
        public int Xvel { get { return _xvel; } }
        public int Yvel { get { return _yvel; } }
    
        public void DrawPlayer(Graphics g)
        {
            g.DrawRectangle(Pens.Red, new Rectangle(X, Y, Width, Height));            
        }

        public void CollisionDetection(Rectangle player, Rectangle car)
        {
            if (player.IntersectsWith(car))
            {
                MessageBox.Show("You Lose!");                
            }

        }      

        public void MovePlayerLeft(int gamewidth)
        {
            if (_x > 0)
            {
                _x -= _xvel;
            }
        }
    }

public partial class Form1 : Form
    {     
      
        Car cars;

       Player player;    

        public Form1()
        {
            InitializeComponent();
          
            player = new Player(((Width / 2) - 15), (Height - 75),30, 30, 10, 10);
            
            Random r = new Random();
            cars = new Car(r, 200, 30, 30, 10);         
            
        }
      
        private void Form1_Paint(object sender, PaintEventArgs e)
        {            
                Graphics g = e.Graphics;
                g.Clear(Color.White);
                cars.DrawCar(g);
                player.DrawPlayer(g);
                player.CollisionDetection(player, cars); //This is the part that implements collision detection
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            cars.MoveCar(this.Width);
            this.Invalidate();
        }
    }

推荐答案

看起来非常直接,也许你首先想要绘制图形?

http://msdn.microsoft.com/en- us / library / system.drawing.rectangle.intersectswith.aspx [ ^ ]
Looks pretty straight fporward, perhaps you ned to draw the graphics first?
http://msdn.microsoft.com/en-us/library/system.drawing.rectangle.intersectswith.aspx[^]


这里你似乎有点困惑:

You seem a little confused here:
player.CollisionDetection(player, cars);




Car cars;
Player player;




class Player
{
...
    public void CollisionDetection(Rectangle player, Rectangle car)
    {
        if (player.IntersectsWith(car))
...

所以,因为播放器与Rectangle无关,你不能将它传递给你的碰撞检测方法。

这可能与你得到的错误信息有关:你的Player类没有实现IntersectsWith所以你不能称之为。



我认为你需要退后一步,看看从基类派生你的一些类,可能是矩形的(虽然我怀疑这将是一个糟糕的举动 - 我会有一个抽象的Sprite类或类似的来自两者,可能恰好包含一个矩形,或者可能是一个区域供以后使用,并且它会进行碰撞检测派生类的离子)

So, since Player isn''t related to Rectangle, you can''t pass it to your collision detection method.
Which is probably something to do with the error message you get: your Player class does not implement IntersectsWith so you can''t call it.

I think you need to take a step back, and look at deriving some of your classes from base classes, possibly rectangle (though I suspect that would be a poor move - I would have an abstract "Sprite" class or similar to derive both from, which might happen to contain a rectangle, or possibly a Region for later use, and which does the collision detection for derived classes)


这篇关于在C#中使用IntersectsWith时出错。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆