0

タイトルには多くのことが書かれていますが、前にthread.sleepを試してみましたが、役に立ちません。キャラクターのジャンプが見えるように、ジャンプ コマンドを遅くしたいのですが、今のところ、瞬く間に終了します。これを行う方法を説明してください。私はJavaの初心者であるslick2d 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;
    }       
}
4

3 に答える 3

3

常に TIME に応じてゲームの更新を行う必要があるため、すべてのコンピューターで同じ速度で問題ありません。そして、それはあなたの問題も解決します。

public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException

ゲームコンテナには次のようなものが必要です:

gc.getTime()

これは、前回の実行で経過した時間を示しています。これがない場合は、使用できます

System.nanoTime()

次に、時間を保存する int 値だけが必要で、たとえば 50 ミリ秒ごとに 1 回だけ更新を行います。

于 2013-10-12T16:05:19.640 に答える
0

Slick2D は LWJGL を使用しているため、LWJGL wiki が特に役立ちます。

これはあなたの質問に最適です: LWJGL Basics 4: Timing

麻生、「デルタコードに早く慣れる方法」はこれを試してみてください

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);
于 2014-02-23T13:04:09.837 に答える