标题说了很多,我之前尝试过thread.sleep,但没有帮助。我想放慢跳跃命令,这样你就可以看到角色跳跃,现在它在眨眼之间完成。请解释如何做到这一点。我使用 slick2d java,Java 新手
谢谢你的时间!
package JavaGame;
import org.newdawn.slick.*;
import org.newdawn.slick.state.*;
public class Play extends BasicGameState{
Animation bucky, movingUp, movingDown, movingLeft, movingRight;
boolean quit = false;
int[] duration = {400, 100}; //2 tenths of a second and 2 tenths of a second, both in under half a second. Series of image, how long each image lasts
//the array of numbers decides the length of the animation
float buckyPositionX = 0;
float buckyPositionY = 0;
float shiftX = buckyPositionX + 540; //Shifting something 320, always letting bucky be in the middle of the screen
float shiftY = buckyPositionY + 620;
boolean jumping = false;
double gravity = 10;
double jumpingTime = 200;
float v = shiftY;
double counter2 = 4;
public Play(int state){
}
public void init(GameContainer gc, StateBasedGame sbg) throws SlickException{
Image[] walkUp = {new Image("res/buckysRight.png"), new Image("res/buckysFront.png")}; //Change to jump, add more imagines (same amount of duration as images)
Image[] walkDown = {new Image("res/buckysFront.png"), new Image("res/buckysFront.png")}; //change last picture to the front will restore it to the front frame ending the animation
Image[] walkLeft = {new Image("res/buckysLeft.png"), new Image("res/buckysFront.png")};
Image[] walkRight = {new Image("res/buckysRight.png"), new Image("res/buckysFront.png")};
//Creating the actual animation, animations defined Animation, bucky bla bla
movingUp = new Animation(walkUp, duration, false);
movingLeft = new Animation(walkLeft, duration, false);
movingRight = new Animation(walkRight, duration, false);
movingDown = new Animation(walkDown, duration, false);
bucky = movingDown;
}
public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException{
g.fillRect(buckyPositionX, buckyPositionY, 10000, 50); //Øverste boks
g.fillRect(buckyPositionX, buckyPositionY + 660, 10000, 70); //Nederste boks
g.fillRect(buckyPositionX, buckyPositionY + 360, 50, 300); // Bakerste Kant
bucky.draw(shiftX, shiftY);
boolean debug = true;
if(debug == true){
g.drawString("Buckys X: " + buckyPositionX + "\nBuckys y: " +buckyPositionY, 900, 50);
g.drawString("Buckys 2 X: " + shiftX + "\nBuckys 2 y: " + shiftY, 900, 100);
}
if(quit == true){ //When esc is hit launch the menu.
g.drawString("Resume (R)", 250, 100);
g.drawString("Main Menu (M)", 250, 150);
g.drawString("Quit (Q)", 250, 200);
if(quit == false){
g.clear();
}
}
}
public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException{
Input input = gc.getInput();
if(input.isKeyDown(Input.KEY_RIGHT)){
bucky = movingRight;
buckyPositionX -= .2;
}
if(input.isKeyDown(Input.KEY_LEFT)){
bucky = movingLeft;
buckyPositionX += .2;
if(buckyPositionX > 492){
buckyPositionX -= .2;
}//delta * .1f
}
if(shiftY < 620 && jumping != true){
shiftY += gravity;
}
if(input.isKeyPressed(Input.KEY_SPACE)){
jumping = true;
while(jumping == true){
counter2 += 0.0005;
shiftY = shiftY - (int) ((Math.sin(counter2) + (Math.cos(counter2)))*20);
if(counter2 >= 7){
counter2 = 4;
jumping = false;
}
}
}
//escape
if(input.isKeyDown(Input.KEY_ESCAPE)){
quit = true;
}
//when the menu is up
if(quit == true){
if(input.isKeyDown(Input.KEY_R)){
quit = false;
}
if(input.isKeyDown(Input.KEY_M)){
sbg.enterState(0);
}
if(input.isKeyDown(Input.KEY_Q)){
System.exit(0);
}
}
}
public int getID(){
return 1;
}
}