Animating an object to move between two points

2019-05-21 11:31发布

问题:

I'm making a game where you have to order troops around, meaning click on the destination, click on the soldier, and the soldier moves to the destination.

I am having problems with the animation part. I have already figured out the MouseListener and graphics stuff. I know that you are supposed to use y = mx + b, m = y2 - y1/x2 - x1 and y - y1 = m(x - x1), but don't know how to implement this into my application.

I have a Game class that contains the listeners, and a UserUnit class that contains the actual moveTo() method. Assume that I've got everything else figured out, if I don't I'll ask. Thanks.

回答1:

In this related example, robots move(int, int) toward the player's grid position by stepping in one of eight (semi-) cardinal directions, as shown here. An instance of javax.swing.Timer drives the animation.