2014-05-11 18 views
0

我一直在用Slick2D(java)開發一個基於狀態的遊戲,並且到達了「死衚衕」。Slick2D如何開始動畫,暫停它,直到發生什麼事情,並恢復它?

我有一個由2幀組成的動畫。我想要在用戶運行程序時停止動畫。當他點擊時,第二幀應該出現0.5秒,然後它應該返回到第一幀,並再次暫停,直到用戶再次點擊。

類似的東西:

ani.draw(150,150); //draw the animation 
ani.stopAt(0);  //the animation stops at it's first frame 
~~~~user clicks~~~~ 
ani.start(); 
ani.stopAt(1); //here it stops at the second frame. Pretty sure there's another way to do it but that's not the problem. 
~~~~wait for 0.5 secs~~~~ (here's the part I need) 
ani.start(); 
ani.stopAt(0); // return to the first frame again 

我試着用了Thread.sleep這樣做,但不能使它工作 - (我認爲),它跳轉到第一幀如此之快,你不能看到第二個爲所有。也許我只是不明白Thread.sleep是如何工作的。

任何幫助,將不勝感激。我一直在做這個遊戲很多時間(主要是做藝術),並且不想因此而放棄。

感謝

回答

1

你需要的東西是這樣的:

//Declare variables: 

float coolDown = 0f; 
boolean cooling = false; 

... 

//In the update() method: 

if (gc.getInput().isMouseButtonPressed(0)) { 
    cooling = true; 
    ani.setCurrentFrame(1); 
} 
if (cooling) { 
    coolDown += 0.1f * delta; //Increases the coolDown by a constant amount each frame 
} 
if (coolDown >= 500) { 
    //Change '500' to a larger number for a longer wait. NOTE: 500 is NOT 500 milliseconds. 

    cooling = false; 
    coolDown = 0f; 
    ani.setCurrentFrame(0); 
} 

希望這有助於:)