Click here to Skip to main content
11,707,470 members (75,819 online)
Rate this: bad
good
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))
               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 19-Aug-12 22:09pm

1 solution

Rate this: bad
good
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)[^]
  Permalink  

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

  Print Answers RSS
0 Mika Wendelius 220
1 Maciej Los 195
2 OriginalGriff 148
3 Sergey Alexandrovich Kryukov 145
4 chainerlt 110
0 OriginalGriff 9,198
1 Sergey Alexandrovich Kryukov 8,562
2 CPallini 5,189
3 Maciej Los 4,921
4 Mika Wendelius 3,856


Advertise | Privacy | Mobile
Web02 | 2.8.150819.1 | Last Updated 21 Aug 2012
Copyright © CodeProject, 1999-2015
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