2 つのオブジェクト間の衝突を検出する方法を教えてください。最初は役立つと思いIntersectsWith
ましたが、ソリューションに IntersectsWith の定義が含まれていないというエラーが表示されます。エラーが発生するコードは次のとおりです。
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();
}
}