When you click a location, your unit walks in that direction, right? So how about when you click a location, you determine the distance that the unit should move; and when that distance is covered, it stops.
To stop a sprite from moving:
In your code above, you should (at a minimum) change:
// from...
temp+2;
// to...
temp+=2;
without the assignment, the
temp variable never changes. Without a mouse-click or escape-key, there is no other exit from the while...loop unless you plan on quiting the entire game...
However, looking at your code, you are (or will be) having other issues. For example, given your existing code above, when the sprite is moving, *nothing* else will be happening. No AI (artificial intelligence) can think, no burning buildings can animate (not entirely true but bare with me here) or no bullets can fly around.
Why? The short answer is that you're not giving them a chance to.
Again, you may ask: Why? Because you are placing the entirety of the sprite's movement distance within a single while...loop. That means that while the sprite is moving, nothing else *can* happen.
Animated sprites/objects are (potentially) different because somewhere within LoopGDK() or dbSync() [I'm not sure which exactly] they are handled by GDK; not neccessarily correctly, but they are handled somewhere by one of those two functions...
So, instead of having the sprite move in "one fail-swoop" so to speak, you should have it move in increments. At what increment, you ask? Well, a frame-rate increment! What do I mean by that...
//**************************************
// somewhere in DarkGDK()...
//**************************************
bool mouse_clicked=false;
int selected_id=0; // use a GDK Sprite/Object ID as appropriate,
// this sample assumes a Sprite...
// targeted parameters...
int target_pos_x=-1, target_pos_y=-1; // negative values indicate *nothing* targeted...
int fast_targeted_distance=0.0f; // type int since sqrt() is not used...
bool location_targeted=false; // we'll set true when dbMouseClick() is true...
// more code here... Setup buildings to burn/bullets to fly, etc...
...
while (LoopGDK())
{
// Here's where you determine which sprite/object will be moving to the targeted location...
selected_id=SelectSprite();
// Handle the targeting here...
if (dbMouseClick())
{
// Only recognize *distinct* click actions and not just holding
// the mouse button from one frame to the next...
if (!mouse_clicked)
{
target_pos_x=dbMouseX();
target_pos_y=dbMouseY();
// How far must the sprite travel to reach the targeted location?
// Note: no sqrt() call for speed. In this case, you don't need an
// *actual* distance value...
fast_targeted_distance=((dbSpriteX(selected_id) - target_pos_x)*
(dbSpriteX(selected_id) - target_pos_x)+
(dbSpriteY(selected_id) - target_pos_y)*
(dbSpriteY(selected_id) - target_pos_y));
// Now, face the selected_id towards the targeted location...
float diff_x=dbSpriteX(selected_id)-target_pos_x;
float diff_y=dbSpriteY(selected_id)-target_pos_y;
float angle=((atan2((float)diff_y,(float)diff_x))*180.0f/3.1415f)+90.0f;
dbRotateSprite(selected_id, angle);
// Show that we've targeted a location and clicked the mouse to do it...
location_targeted=true;
mouse_clicked=true;
}
}
else
{
// Signal that next detection of mouse click is distinct...
mouse_clicked=false;
}
// more code here... This code can handle *other* things, burning buildings, bullets flying, etc...
...
// Now, see if the location is targeted. If so, move until we reach it... one unit per game-frame...
if (location_targeted)
{
// Have we travelled far enough to reach it...?
if (fast_targeted_distance>((dbSpriteX(selected_id) - target_pos_x)*
(dbSpriteX(selected_id) - target_pos_x)+
(dbSpriteY(selected_id) - target_pos_y)*
(dbSpriteY(selected_id) - target_pos_y)))
{
// No. Keep moving towards the target...
dbMoveSprite(selected_id, 1.0f);
}
else
{
// Yes. Stop moving and clear targeted location...
dbMoveSprite(selected_id, 0.0f);
location_targeted=false;
}
}
// move on to the next game-frame...
dbSync();
}
Notice in this snippet, that the selected_id only advances at an incremented rate; in this case, one unit per frame. Each frame of the game will allow the selected_id to move 1-unit towards the targeted position while allowing the user to interact, the buildings to burn and bullets to fly by...
With all that said, Good-Luck and Happy Coding.
JTK