1
\$\begingroup\$

My game can display tiles such as Grass tile, Dirt tile and Rock tile with no problems. Now I am trying to display water tile and animate it but for some reason it is not animating and only displaying single frame of water tile. What am I doing wrong?

Below are the appropriate classes that I am using to animate my water tiles. Please let me know if you require any more info to help me solve the problem. Thanks!

Tile class:

public abstract class Tile {

public static Tile[] tiles = new Tile[256];
public static Tile grassTile = new GrassTile(0);
public static Tile dirtTile = new DirtTile(1);
public static Tile rockTile = new RockTile(2);
public static Tile waterTile = new WaterTile(3);
public static Tile waterfallTile = new WaterfallTile(4);
public static Tile wateSplashTile = new WaterSplashTile(5);
public static final int TILE_WIDTH = 64, TILE_HEIGHT = 64;

protected BufferedImage texture;
protected final int id;

public Tile(BufferedImage texture, int id){
    this.texture = texture;
    this.id = id;
    tiles[id] = this;
}

public abstract void update();
public abstract void render(Graphics g, int x, int y);

public Boolean isSolid(){
    return false;
}

WaterTile class:

public class WaterTile extends Tile {
private Animation waterAnim;
private int speedAnim = 150;

public WaterTile(int id) {
    super(Assets.water[0], id);
    waterAnim = new Animation(speedAnim, Assets.water);
}

public void update(){
    waterAnim.update();
}

@Override
public void render(Graphics g, int x, int y) {
    g.drawImage(getAnimationFrame(), x, y, Tile.TILE_WIDTH, Tile.TILE_HEIGHT, null);
}

private BufferedImage getAnimationFrame() {

    return waterAnim.getFrames();

}

public Boolean isSolid(){
    return true;
}

Animation class:

public class Animation {

private int speed, index;
private long lastTime, timer;
private BufferedImage[] frames;

public Animation(int speed, BufferedImage[] frames){
    this.speed = speed;
    this.frames = frames;
    index = 0;
    timer = 0;
    lastTime = System.currentTimeMillis();
}

public void update(){
    timer+=System.currentTimeMillis() - lastTime;
    lastTime = System.currentTimeMillis();

    if (timer > speed){
        index++;
        timer = 0;
        if (index >= frames.length){
            index = 0;
        }
    }
}

public BufferedImage getFrames() {
    return frames[index];
}
\$\endgroup\$
  • \$\begingroup\$ Have you reverted to the simplest possible test case? I would do this: write a very basic piece of code that allows you to press spacebar and see a Tile / BufferedImage change frame, once. Then reintroduce the rest of the logic, slowly, till you have a working sample. \$\endgroup\$ – Engineer Mar 18 '18 at 9:28
  • 1
    \$\begingroup\$ Do you actually call the update on the watertile? \$\endgroup\$ – Bálint Mar 18 '18 at 10:22
  • 1
    \$\begingroup\$ NullPointerExceptions usually tell you why they happen, so please refer to a general guide about that \$\endgroup\$ – Bálint Mar 18 '18 at 15:30
  • 1
    \$\begingroup\$ I figured it out. I didn't initialize my tile before calling its update method. Silly mistake. Thanks for the help. \$\endgroup\$ – AaySquare Mar 18 '18 at 16:25
  • 2
    \$\begingroup\$ @AaySquare you should post (and accept) your solution as an answer to help other that might encounter this problem \$\endgroup\$ – Pikalek Mar 20 '18 at 14:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.