0

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.

Sheikh
  • 539
  • 1
  • 7
  • 29
  • 2
    What framework are you using? – MadProgrammer Jan 03 '16 at 07:30
  • @MadProgrammer No libraries, just plain Java – Sheikh Jan 03 '16 at 07:30
  • Swing? AWT? JavaFX? <- That's plain Java – MadProgrammer Jan 03 '16 at 07:35
  • AWT mostly. The Tiles have BufferedImages in them that are rendered by java.awt.Graphics – Sheikh Jan 03 '16 at 07:36
  • Well, conceptually, you basically need to know which "frame" to display. In order to know that, you need to know at what point you're up to in the animation cycle (so you know which frame to use, based on your desired FPS), maybe something like [this](http://stackoverflow.com/questions/27933159/how-to-draw-an-bufferedimage-to-a-jpanel/27933189#27933189) – MadProgrammer Jan 03 '16 at 07:39
  • Just updated question with what I tried. – Sheikh Jan 03 '16 at 07:59
  • Think about it this way. If you're animation is running at 25fps and you have 2 frames of animation, but you want to cycle the animation twice per second, then that means each frame needs to be shown (roughly) for 6 frames – MadProgrammer Jan 03 '16 at 08:06
  • Got it :) Thx man. I just forgot to reset lastTime. – Sheikh Jan 03 '16 at 08:16

0 Answers0