Click here to Skip to main content
12,076,216 members (50,706 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# XNA
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))



           //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;
                   // 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


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 19-Aug-12 23:09pm

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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)[^]

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.160208.1 | Last Updated 21 Aug 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100