标题说很多,我以前尝试过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;
}
}
答案 0 :(得分:3)
你应该总是根据时间进行游戏更新,所以它在所有计算机上都没有问题。它也解决了你的问题。
public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException
游戏容器应该有这样的东西:
gc.getTime()
它显示了上次运行中经过了多少时间。如果没有,可以使用
System.nanoTime()
然后你只需要一些int值,你可以在那里存储时间,例如每50个小时只进行一次更新。
答案 1 :(得分:0)
由于Slick2D使用LWJGL,你会发现LWJGL wiki特别有帮助。
这个很适合您的问题:LWJGL Basics 4: Timing
麻生太郎,试试这个“如何快速习惯使用delta代码”
private float yRect;
@Override
public void update(GameContainer gc, int delta) throws SlickException {
yRect += 0.03f * delta;
}
@Override
public void render(GameContainer gc, Graphics g) throws SlickException {
g.pushTransform();
g.translate(0, yRect);
g.fillRect(20, 20, 200, 100);
g.popTransform();
}
确保以合理的帧速率运行此示例...
appgamecontainer.setTargetFrameRate(60);
答案 2 :(得分:0)
哦,你在没有渲染的情况下在while循环中进行所有跳跃计算。
while(jumping == true){
counter2 += 0.0005;
shiftY = shiftY - (int) ((Math.sin(counter2) + (Math.cos(counter2)))*20);
if(counter2 >= 7){
counter2 = 4;
jumping = false;
}
}
这不正确。你必须在每一轮跳跃循环中渲染图像。否则跳跃计算将会发生,但不会显示。
我正在给出这个答案,希望你的主要方法是这样的。如果你要覆盖其中一个类,那么光滑的主要方法。
init()
while(true){
update();
render();
doTiming(); // Whatever the method you use for timing in the game loop.
}
所以你需要做的就是在跳跃时加上一个标志并阻挡其他动作。
public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException{
if(jumping){ // Or jumping == true
counter2 += 0.0005;
shiftY = shiftY - (int) ((Math.sin(counter2) + (Math.cos(counter2)))*20);
if(counter2 >= 7){
counter2 = 4;
jumping = false;
}
}else{
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;
}
//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);
}
}
}
}
因此,在每轮跳跃循环中,都会调用render方法。因此,将显示跳转中的所有部分。
建议:如果你擅长物理运动,则使用运动方程,而不是使用sin和cos来模拟跳跃。我不确定你是如何改变shiftY的。但如果你在equation使用了v = u +,你可以将它作为真实的世界跳跃。