有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何使用精灵表快速设置精灵运动的动画?

我正在做一个小游戏,我可能会提交给即将到来的#towerjam。我想在一个类中用两个精灵表图像快速设置精灵的动画

以下是我目前的代码:

    if (handler.isUp()) {
        y -= speed;
        if (y < 0)
            y = 0;
        this.tile = Tile.PLAYER_UP1;
    } if (handler.isDown()) {
        y += speed;
        if (y > Game.HEIGHT - Tile.DRAW_SIZE)
            y = Game.HEIGHT - Tile.DRAW_SIZE;
        this.tile = Tile.PLAYER_DOWN1;
    } if (handler.isRight()) {
        x += speed;
        if (x >= Game.WIDTH - Tile.DRAW_SIZE)
            x = Game.WIDTH - Tile.DRAW_SIZE;
        this.tile = Tile.PLAYER_WALK1;
    } if (handler.isLeft()) {
        x -= speed;
        if (x < 0)
            x = 0;
        this.tile = Tile.PLAYER_WALK1FLIP;
    }

每个平铺静态变量都有一个数字2,我想循环并设置动画

更新

我尝试在外部添加这些变量:

int frame = 1;
long lastTime = System.currentTimeMillis();

然后在更新方法的内部:

    long newTime = System.currentTimeMillis();
    if(newTime-lastTime == 100) {
        if(frame == 1) frame=2;
        else frame = 2;
    }

然后像这样更改磁贴:

this.tile = frame == 1 ? Tile.PLAYER_UP1 : Tile.PLAYER_UP2;

不幸的是,这不起作用


共 (0) 个答案