Hello again
I put this problem on hold while working on other parts of the game, but now it's become a showstopper.
Latest update: I'm going with the time-elapsed method since you both agree it's the right way. But won't the time elapsed always be 20, or whatever the number is in the Sleep function? In the game loop, I have two lines that store the last frame's time, and the time elapsed:
gTimeElapsedSinceLastFrame always equals 20. Assuming this is correct (I'm guessing it isn't), the next step is to multiply gTimeElapsedSinceLastFrame by dirX and dirY (the speed), right? The resulting movement is very jittery and not smooth at all. Also, it never reaches its destination because it never meets the while loop's condition (although this is probably an easy fix).
method LinearMovement(movingEntity, startX, startY, endX, endY)
{
var dX = Math.
Abs(endX - startX
);
var dY = Math.
Abs(endY - startY
);
var dirX = 1;
if (startX > endX)
dirX = -1;
var dirY = 1;
if (startY > endY)
dirY = -1;
dirX = dirX * gTimeElapsedSinceLastFrame;
dirY = dirY * gTimeElapsedSinceLastFrame;
var diff = dirX - dirY;
var posX = startX;
var posY = startY;
while ((posX != endX) && (posY != endY))
{
var dblDiff = 2 * diff;
if (dblDiff > (-1 * dY))
{
diff = diff - dY;
posX = posX + dirX;
}
if (dblDiff < dX)
{
diff = diff + dX;
posY = posY + dirY;
}
}
}