Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm making a whack the mole like game where 4 enemies come from all 4 directions:

I've got the 4 enemy sprites and all I want them to do is walk accross the x or y axis towards the cow in the centre. When the direction key is clicked they get reset to their orginal position. E.g. If the farmer on the left went towards the cow in the centre you would click left to send him back.

share|improve this question
Please post some info regarding what you've done, how and why do you think it might work badly, i.e. which module? Check your click events for bogus forgotten about pieces of code (esp. positioning) – teodron May 17 '12 at 11:21
I have the assets loaded in. Thats about it. – Neowu May 17 '12 at 11:32
My cow spins around when I click the direction keys as well, but nothing relevent. I've spent a few hours looking into this and couldn't find much. Kinda need to do this for tomorrow so I figured I'd ask people who actually know what they're doing. – Neowu May 17 '12 at 11:44
Considering that you're already drawing the sprites at a position, it should be pretty simple to simply change that position. – William Mariager May 17 '12 at 13:20

You're pretty much asking us to program the entire gameplay logic for you. You should tackle one problem at a time, and post a question for one specific problem. I'm just going to answer the question on your title (How to move a sprite in a straight line) since that's the more concrete problem.

First define variables for the position, direction (which should be normalized) and speed of your sprite. You probably already have the position since you're drawing the sprite on screen.

Vector2 position;       // Position of the sprite, where it is drawn
Vector2 direction;      // Normalized vector pointing in the direction to move
float speed;            // Speed in units (pixels if not zooming) per second

Having these three, you can make the sprite move the in the specified direction by doing the following in the Update method:

float dt = (float)gameTime.ElapsedGameTime.TotalSeconds;
position += direction * speed * dt;

For example, to make the sprite move left to right you'd do something like:

direction = new Vector2(1,0);     // Points right
speed = 100f;                     // Moves 100 pixels per second
share|improve this answer
Big help thanks. – Neowu May 17 '12 at 12:04
Why is dt using TotalSeconds instead of seconds since last tick? – ashes999 May 17 '12 at 13:15
@ashes999 dt is the amount of seconds since the previous frame. TotalSeconds is just a property of the TimeSpan structure that returns the amount of seconds in that timespan as a floating point number. The important part is that I'm calling it on ElapsedGameTime, and you're probably confusing it with TotalGameTime. – David Gouveia May 17 '12 at 13:35
Ah yes, my bad. Thanks for pointing that out. – ashes999 May 17 '12 at 18:31

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.