Click here to Skip to main content
15,880,405 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is my code

C#
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);
       }


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?
Posted

1 solution

Unfortunately what you are asking for isn't the easiest thing in game development, so I can't provide an excellent example.

Assuming your sprite is at a starting location, as soon as a new target is established (your touch point), you need to run a calculation to determine the path.

You can store that path as a number of Vector2's (for 2D) and then have the sprite begin to move toward each point in succession until it reaches the end.

Unfortunately there are lots of variables, such as what if something moves into the path you've established? You would have to recalculate the path.

Here's a decent example (See first answer)[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900