4 Replies - 3522 Views - Last Post: 03 March 2012 - 05:47 AM

#1 Ateedmia  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 17-February 11

Question: 2D sprite "walk" to mouse click

Posted 17 February 2011 - 05:03 PM

Hi all.

I'm rather new to programing and have been working through a tone of tutorials (I'm one of those where the hands on learning is what gets it into my head). I'm trying to make my little sprite guy "walk" to a point after a left mouse click. I'm using XNA 3.0, using C#. I cannot find any tutorials that cover this kind of update movement. I've got the keyboard down, and I have a good grasp on the logic needed for the mouse but the syntax (and some of the logic) is really messing me up.

I've read everything I can find about mouse movement, but a lot of the help questions seems to revolve around simulated mouse clicks. I can make my sprite just to where ever I just clicked, but I'm looking for soemthing more video game like...

Can someone point me to a good book, or an online tutorial that would cover this kind of thing?

Is This A Good Question/Topic? 0
  • +

Replies To: Question: 2D sprite "walk" to mouse click

#2 Kilorn  Icon User is offline

  • XNArchitect
  • member icon



Reputation: 1356
  • View blog
  • Posts: 3,528
  • Joined: 03-May 10

Re: Question: 2D sprite "walk" to mouse click

Posted 17 February 2011 - 06:26 PM

Well, the first thing you'll need is a way to check for mouse clicks, but since you said you've got a good grasp on the logic for the mouse, I won't go into any of that. Once you know the position that the mouse clicked, it's just a matter of figuring out the distance from that position to the position of the object being controlled and applying movement to the object in the proper direction in order to get it to walk to that point.
Was This Post Helpful? 0
  • +
  • -

#3 Hernaldo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 15-February 12

Re: Question: 2D sprite "walk" to mouse click

Posted 01 March 2012 - 10:51 AM

In the Post http://www.dreaminco...1&#entry1565111
y put the solution.

If the link is broken, read the Andrew Broadbent Post http://forums.create...3690/71746.aspx:


Quote

Re: Move a 2D sprite to a set coordinate
Reply Quote

just out of interest, did you want your sprite to move directly to the location? because the code above will not create a direct path to do that you will need a line drawing algorithm, or you could simply move the sprite with a vector:

get a vector from the sprite to the location; Vector2 velocity = Vector2.Subtract(location, spritePos)

Normalize this vector: velocity.Normalize();

multiply this vector by the speed you want the sprite to move at: velocity = Vector2.Multiply(velocity, desiredSpeed)

add this velocity to the sprites position every frame: spritePos = Vector2.Add(spritePos, velocity);



Does this help?


bye!!
Was This Post Helpful? 0
  • +
  • -

#4 superkb10  Icon User is offline

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 298
  • Joined: 27-November 11

Re: Question: 2D sprite "walk" to mouse click

Posted 02 March 2012 - 07:18 PM

Well, like Kilorn said, you need to get the location of the mouse click, then, there is a function called Normalize() which basically returns the direction of the Vector2 specified and then gives it a length of 1. You can use this to make your character walk towards the mouseclick. You may want to research some more on Normalize on your own though.
Was This Post Helpful? 0
  • +
  • -

#5 DanielLeone  Icon User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • Posts: 177
  • Joined: 04-February 12

Re: Question: 2D sprite "walk" to mouse click

Posted 03 March 2012 - 05:47 AM

This topic was started nearly a year ago?

I doubt Ateedmia is still stuck ;)

For anyone elses reference though, I used this tutorial when trying to implement a similar feature.
http://www.berecursi...n-object-in-xna

Very nicley explained. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1