Animating an object to move between two points

2019-05-21 10:50发布

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条回答
欢心
2楼-- · 2019-05-21 11:48

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.

查看更多
登录 后发表回答