I am making a small game that I might submit to the upcoming #towerjam. I want to animate a sprite quickly in one class with 2 spritesheet images.
Here is my code so far:
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;
}
Each of these Tile static variables has a number 2, which I want to loop and animate to.
UPDATE
I tried adding these variables outside:
int frame = 1;
long lastTime = System.currentTimeMillis();
and then on the inside of the update method:
long newTime = System.currentTimeMillis();
if(newTime-lastTime == 100) {
if(frame == 1) frame=2;
else frame = 2;
}
and then to change the tile like so:
this.tile = frame == 1 ? Tile.PLAYER_UP1 : Tile.PLAYER_UP2;
This didn't work unfortunately.