I'm currently looking into XNA game development with the C# language.
I have two classes: the main game handler and a "sprite" class. Following is some basic pseudo-code which I hope adequately describes the issue.
Game.cs
class game {
sprite the_sprite;
void update(time) {
var mouse = mouse.state
if(mouse.clicked) { this.the_sprite.moveTo(mouse.x, mouse.y) }
this.the_sprite.update(time)
base.update(time)
}
}
Sprite.cs
class sprite {
vector2 location;
vector2 move_to;
void moveTo(x, y) { this.move_to = new vector2(x, y) }
void update(time) {
if(this.location.x > this.move_to.x /* (or less than) */) {
// adjust location.x
}
if(this.location.y > this.move_to.y /* (or greater than) */) {
// adjust location.y
}
}
}
Basically: when the user clicks somewhere in the game window, the x and y coordinates of the mouse are taken, and the game object will move towards that location over a period of time.
Well... the code works, but it's ugly, and objects don't move directly towards the object (instead, it's diagonal movement, followed by single-axis movement). I'm guessing there are some mathematical functions I can use, but I honestly haven't a clue where to get started. Any suggestions?