1

我们无法在 java slick2d 游戏中制作角色动画。我看到的教程显示了我是如何做到的。现在它只在向左或向右时显示第一张图像。它永远不会在 walkLeft 和 walkRight 中设置的 2 个图像之间循环。

import org.newdawn.slick.*;
import org.newdawn.slick.state.*;

public class Game extends BasicGameState {

    Animation cowboy;
    Animation movingLeft; 
    Animation movingRight;
    Image background;
    boolean quit = false;
    int[] animationDuration = {200,200};
    float cowboyPositionX = 0;
    float cowboyPositionY = -370;
    float shiftX = cowboyPositionX + 200;
    float shiftY = cowboyPositionY + 830;

    public Game(int state) {

    }

    public void init(GameContainer gc, StateBasedGame sbg)
            throws SlickException {
        background = new Image("images/background.png");
        Image[] walkLeft = { new Image("images/cowboyleft1.png"), new Image("images/cowboyleft2.png") };
        Image[] walkRight = { new Image("images/cowboyright1.png"), new Image("images/cowboyright2.png") };
        movingLeft = new Animation(walkLeft, animationDuration, false);
        movingRight = new Animation(walkRight, animationDuration, false);
        cowboy = movingRight;
    }

    public void render(GameContainer gc, StateBasedGame sbg, Graphics g)
            throws SlickException {
        background.draw(cowboyPositionX, cowboyPositionY);
        cowboy.draw(shiftX, shiftY);
        g.drawString("Cowboy's X:" + cowboyPositionX + "\nCowboy's Y: "
                + cowboyPositionY, 400, 20);    
    }

    public void update(GameContainer gc, StateBasedGame sbg, int delta)
            throws SlickException {
        Input input = gc.getInput();
        if (input.isKeyDown(Input.KEY_LEFT)) {
            cowboy = movingLeft;
            cowboyPositionX += horizontalSpeed;
            if (cowboyPositionX > 0) {
                cowboyPositionX -= delta * horizontalSpeed;
            }
        }
        if (input.isKeyDown(Input.KEY_RIGHT)) {
            cowboy = movingRight;
            cowboyPositionX -= horizontalSpeed;
            if (cowboyPositionX < -2975.0) {
                cowboyPositionX += delta * horizontalSpeed;
            }
        }
    public int getID() {
        return 1;
    }
}
4

1 回答 1

0

在您的更新方法中尝试插入cowboy.update(delta);应该可以工作。它适用于我一直在研究的程序。

于 2013-11-20T00:02:28.010 回答