我試圖在對象保持動畫時點擊鼠標移動對象。有此網站上幾個類似的帖子,我已經根據我的代碼斷這樣的回答:使用鼠標在兩點間移動
An efficient algorithm to move ostrichs along a line at a constant speed
但我想使用一個線程保持動畫的對象。我應該怎麼做?這裏是我的代碼:
public void movePlayer(Graphics g, int finalX, int finalY)
{
int length = finalX - xpos;
int height = finalY - ypos;
int oldXpos = xpos;
int oldYpos = ypos;
double speed = 20;
double distanceX = (length)/speed;
double distanceY = (height)/speed;
double distance = (Math.hypot(length,height));
double distanceTraveled = 0;
//This currently doesn't work:
move = new Thread(this);
{
while (distanceTraveled<distance)
{
//move the object by increments
xpos += distanceX;
ypos += distanceY;
distanceTraveled = Math.hypot(xpos-oldXpos, ypos - oldYpos);
drawPlayer(img, g);
for(int x = 0; x < 100000; x ++);
}
}
}
你的問題在必要的細節上似乎很短,例如你的意思是「保持對象動畫」,這是Swing嗎? AWT?別的東西?請告訴我們所有我們需要知道的能夠幫助你。 – 2012-03-23 20:13:52
我正在使用JApplet。我希望看到物體在兩點之間無縫移動。我將使用動畫精靈,但現在,我只有一張我正在使用的圖片。 – helsont 2012-03-23 20:22:00