Click here to Skip to main content
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 23:09pm
(unknown member)

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 OriginalGriff 250
1 Jochen Arndt 155
2 PIEBALDconsult 150
3 DamithSL 125
4 Afzaal Ahmad Zeeshan 120
0 OriginalGriff 5,695
1 DamithSL 4,591
2 Maciej Los 4,012
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,190


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