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?
Question: 2D sprite "walk" to mouse click
Page 1 of 14 Replies - 2360 Views - Last Post: 03 March 2012 - 05:47 AM
Replies To: Question: 2D sprite "walk" to mouse click
#2
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.
#3
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:
bye!!
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?
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!!
#4
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.
#5
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.
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.
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote








|