我目前正在研究使用C#语言开发XNA游戏。
我有两个类:主游戏处理程序和“ sprite”类。以下是一些基本的伪代码,我希望可以充分描述问题。
Game.cs
class game {
  sprite the_sprite;
  void update(time) {
    var mouse = mouse.state
    if(mouse.clicked) { this.the_sprite.moveTo(mouse.x, mouse.y) }
    this.the_sprite.update(time)
    base.update(time)
  }
}
Sprite.cs
class sprite {
  vector2 location;
  vector2 move_to;
  void moveTo(x, y) { this.move_to = new vector2(x, y) }
  void update(time) {
    if(this.location.x > this.move_to.x /* (or less than) */) {
      // adjust location.x
    }
    if(this.location.y > this.move_to.y /* (or greater than) */) {
      // adjust location.y
    }
  }
}
基本上:当用户单击游戏窗口中的某个位置时,将获取鼠标的x和y坐标,并且游戏对象将在一段时间内移向该位置。
嗯...代码可以工作,但是很丑陋,对象不会直接朝对象移动(相反,它是对角线移动,然后是单轴移动)。我猜想我可以使用一些数学函数,但是老实说,我不知道从哪里开始。有什么建议么?
                
                3
              
                  你好,客人。您正在寻找的是规范化。请阅读以下问题的答案:gamedev.stackexchange.com/questions/7540/…。一个提供您需要的代码,第二个解释它如何很好地工作。
                
                
                  
                    —
                    Notabene 2011年
                    
                  
                
              
                  您好,我已经查看了链接,而这正是我所需要的。谢谢!
                
                
                  
                    —
                    马特