フォーム内のオブジェクトがフォーム自体よりも大きくなるタイミングを検出しようとしています。その場合、MessageBoxが「ゲームオーバー」というテキストとともに表示されます。
これは私がしたことです:
x軸の場合
private void CollisionXForward()
{
int x = this.Width; //the width of the form is 493
//if the position of x-axis of the rectangle goes over the limit of the form...
if (rc.PositionX >= x )
{
//...game over
MessageBox.Show("Game over");
}
else
{
//move the object +5 every time i press right arrow
rc.MoveXForward();
}
問題は、長方形がフレーム自体よりも一歩進んだために消えてしまうことです。私は次のステートメントを使用して問題を「修正」しました。
if (rc.PositionX >= x - (rc.Width * 2))
コードに表示される通常のものの代わりに。しかし、y軸で同じことをしたり、長方形のサイズを変更したりすると、機能しません。
試してみてください:
if (rc.PositionX + rc.Width >= ClientRectangle.Width)
そして
if (rc.PositionY + rc.Height >= ClientRectangle.Height)
編集:
private void CollisionXForward()
{
rc.MoveXForward();
//if the position of x-axis of the rectangle goes over the limit of the form...
if (rc.PositionX + rc.Width >= ClientRectangle.Width )
{
//...game over
MessageBox.Show("Game over");
}
}
または
private void CollisionXForward()
{
//if the position of x-axis of the rectangle goes over the limit of the form...
if (rc.PositionX + step + rc.Width >= ClientRectangle.Width ) //step is 5 in your case
{
//...game over
MessageBox.Show("Game over");
}
else
{
//move the object +5 every time i press right arrow
rc.MoveXForward();
}
}
ヴァルター
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加