本文介绍了C#XNA 4避免碰撞的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码

This is my code

public Vector2 theclickedpoint;
      public Boolean clicked = false;
       protected override void Update(GameTime gameTime)
       {
           // Allows the game to exit
           if (Keyboard.GetState().IsKeyDown(Keys.Escape))
               Exit();







           //press left button down
           MouseState mouseState = Mouse.GetState();
           if (mouseState.LeftButton == ButtonState.Pressed)
           {
               //get the clicked point area
               theclickedpoint.X = mouseState.X;
               theclickedpoint.Y = mouseState.Y;
               clicked = true;
           }




           //move the redmouse sprite the the clicked mouse location
           if (clicked == true)
           {
               if (redmousePosition == theclickedpoint)
               {
                   clicked = false;
               }
               else
               {
                   // the X
                   //greater then
                   if (redmousePosition.X <= theclickedpoint.X)
                   {
                       redmousePosition.X += 10;
                   }
                   //less then
                   if (redmousePosition.X >= theclickedpoint.X)
                   {
                       redmousePosition.X -= 10;
                   }

                   // the Y
                   //greater then
                   if (redmousePosition.Y <= theclickedpoint.Y)
                   {
                       redmousePosition.Y += 10;
                   }
                   //less then
                   if (redmousePosition.Y >= theclickedpoint.Y)
                   {
                       redmousePosition.Y -= 10;
                   }
               }

           }




           // TODO: Add your update logic here

           base.Update(gameTime);
       }



现在它转到我单击的鼠标位置,(是的,我知道代码看起来很尴尬),但是我希望它能够避免对象的出现,比如说它只会绕过一个简单的texture2d精灵,任何人都可以帮助我实现



right now it goes to my clicked mouse location, (yeh i know the code looks awkward)but i want it to be able the avoid objects if its in its way, say it will just go around a simple texture2d sprite can anyone help me achieve this goal?

推荐答案


这篇关于C#XNA 4避免碰撞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 03:00