您如何在C#没有XNA插件中进行冲突? [英] how do you do a collison in C# no XNA plugin?

查看:67
本文介绍了您如何在C#没有XNA插件中进行冲突?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我进行了一个简单的运动测试.我有一个图片框,可以用W,D,S,A键移动.我还得到了一个简单的碰撞检测,如果图片框与另一个图片框碰撞,它将变为黑色.我很难使图片框不通过其他框,有人知道吗?这是我的代码.

Hi, I got a simple movement test going on. I got a picturebox to move with the W,D,S,A keys. I also got a simple collision detection, if the picturebox collides with another picturebox it changes to the color black. I''m having a hard time making the picturebox not go through the other one does anyone knows how? here is my code.

bool move = true;


  private void Form1_KeyDown(object sender, KeyEventArgs e)
  {
      int x = pictureBox1.Location.X;
      int y = pictureBox1.Location.Y;

      if (e.KeyCode == Keys.W) y -= 5;
      else if (e.KeyCode == Keys.S) y += 5;
      else if (e.KeyCode == Keys.A) x -= 5;
      else if (e.KeyCode == Keys.D) x += 5;

      {
          pictureBox1.Location = new Point(x, y);
      }

          if (move == false)
          {


              pictureBox1.Location = new Point(0, 0);



          }
      }





  private void Form1_Load(object sender, EventArgs e)
  {
      timer1.Interval = 50;

      timer1.Start();

  }

  private void timer1_Tick(object sender, EventArgs e)
  {



      if (pictureBox1.Bounds.IntersectsWith(pictureBox2.Bounds))
      {
          pictureBox1.BackColor = Color.Black;

          move = false;

      }
      else
      {
          pictureBox1.BackColor = Color.Blue;


      }
  }





感谢所有帮助.





Will appreciate all help thanks.

推荐答案

我不确定您为什么要使用计时器来检查碰撞?可能导致碰撞的运动.我强烈建议您在编写一行代码之前,先拿出流程图说明您应该执行的代码.似乎最合乎逻辑的流程将是这样的:

获取按键
移动盒
发生碰撞了吗?
反向移动
更改颜色
其他
继续注意按键

您不需要状态管理就可以通过上述更通用的逻辑来进行移动.因此,摆脱愚蠢的计时器,例如,将所有逻辑放入方法调用MoveBlock(Keys key)InCollisionState()中.然后再试一次.

您确实意识到,由于您的计时器控制着检查,因此您冒着在处理计时器事件之前进行多次移动的风险吗????该计时器事件将在任何未决的按键动作之后排队.
I''m not sure why you are using a timer to check your collision when it is your movement that can cause the collision. I highly suggest that before you write a single line of code you come up with a flowchart of just what your code should be doing. It would seem that the most logical flow would be something like this:

Get Keystroke
Move box
Has collision occured?
reverse move
change color
Else
continue to watch for a keystroke

You have no need for state management of whether or not move is capable with more generic logic like above. So get rid of the stupid timer, put all logic into method calls MoveBlock(Keys key) InCollisionState() for example. Then try it once again.

You do realize that since your timer controls the check you run a risk of making several moves before the timer event is handled???? That timer event will queue behind any pending key stroke moves.


这篇关于您如何在C#没有XNA插件中进行冲突?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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