Kultid_Games
Kultid_Games

Reputation: 311

Java 2D Game Programming -SpriteSheet Animations

I am currently working on a small 2D Java Game and I have just finished fixing any bugs/errors etc. that I've gotten from following CodeNMore's YouTube tutorials.

With everything in my game up-to-date with his, I've been working on expanding my game. So, I've created a sprite for a character that faces in different directions. Unfortunately, I have not even the slightest clue as to how I should go about displaying the images.

To further explain, CodeNMore uses this line of code: [From SpriteSheet.java]

public BufferedImage crop(int col, int row, int w, int h){
        return sheet.getSubimage(col * 16, row * 16, w, h);
}

The issue I'm having is that I have no clue as to how to animate/change the characters sprite on the spritesheet whenever they are moving in a certain direction.

I.E. If the player is facing right, I need the image (which is ,by default, in column 0, row 0) to change to column 1, row 3.

And once that is completed, I have to figure out how to animate the player. (which hopefully I should figure out all by myself).

Everything that you need to see can be downloaded from this mediafire:

http://www.mediafire.com/download/dfdkudun91wdup5/tile_rpg_part_20.zip

Its the exact files that CodeNMore has been working on, just with my spritesheet in the /res.

Upvotes: 1

Views: 7493

Answers (1)

Moddl
Moddl

Reputation: 360

Here is the animation class i use

import java.awt.Graphics;
import java.awt.image.BufferedImage;

public class Animation {
private int speed;
private int frames;

private int index = 0;
private int count = 0;

private BufferedImage[] images;
private BufferedImage currentImg;

public Animation(int speed, BufferedImage... args){
    this.speed = speed;
    images = new BufferedImage[args.length];
    for(int i = 0; i < args.length; i ++){
        images[i] = args[i];
    }
    frames = args.length;
}

public Animation(int speed, boolean flip, BufferedImage... args) {
    if (flip) {
        this.speed = speed;
        images = new BufferedImage[(args.length * 2) - 2];
        for (int i = 0; i < args.length; i++) {
            images[i] = args[i];
        }
        BufferedImage[] temp = new BufferedImage[args.length - 2];
        for (int i = args.length - 2; i > 0; i--) {
            temp[args.length - i - 2] = images[i];
        }
        for (int i = args.length; i < images.length; i++) {
            images[i] = temp[i - args.length];
        }
        frames = images.length;
    }else{
        this.speed = speed;
        images = new BufferedImage[args.length];
        for (int i = 0; i < args.length; i++) {
            images[i] = args[i];
        }
        frames = args.length;
    }
}

public void runAnimation(){
    index ++;
    if(index > speed){
        index = 0;
        nextFrame();
    }
}

private void nextFrame(){
    for(int i = 0; i < frames; i ++){
        if(count == i)
            currentImg = images[i];
    }

    count ++;

    if(count >= frames)
        count = 0;
}

public void drawAnimation(Graphics g, int x, int y, int scaleX, int scaleY){
    g.drawImage(currentImg, x, y, scaleX, scaleY, null);
}
}

To use it just create an instance of this class for each loop of images and in the speed parameter give it how many ticks between each frame and the images in the args. Then in your tick method (or what ever you tick you object with) call the run animation and in your render method call the drawAnimation method when you want to render the animation. If you add the flip parameter as true, it will automatically reverse your sequence of images Eg. You give it Img1, Img2, Img3, Img4, Img5. it will process it as Img1, Img2, Img3, Img4, Img5, Img4, Img3, Img2.

Upvotes: 2

Related Questions