Hi, I’m a newbie in java game programming, and I read some articles regarding Sprite Animation and has no luck understand it. Do you guys know a simple tutorial to understand the basic of it?
thanks.
I don’t know a simple tutorial right now, but what specifically don’t you understand? Is it something about the general process of sprite animations, or how to actually use?
It’s pretty simple. Do you know how to render a basic sprite? If no, then you shouldn’t be even thinking about sprite animation. First step is to know a way to render sprite onto the screen.
Once you know that, you can simple do something like this:
-make an array of sprites (say array which contains 3 different sprites);
-make an integer which should always be within 0-2 in this case (because we have an array of 3 members);
-take a sprite from that array passing your integer you just created as an index;
-adjust that integer to your liking (this will change the frame displayed);
This would like something likes this in code:
public class SpriteDemo {
private Sprite[] sprites = new Sprite[];
private int currentSprite = 0; // should be from 0 to sprites.length - 1;
public SpriteDemo() {
//Fill your array of sprites with sprites of some sort. All should be different
//if you want to get animation look.
for(int i=0;i<sprites.length;i++) {
sprites[i] = new Sprite();
}
}
public void update() {
currentSprite += 1; // increase frame counter by 1
//check and make sure animation counter is within your sprite
//array bounds.
if(currentSprite >= sprites.length) {
currentSprite = 0; // reset animation to start
}
}
public void render() {
sprites[i].draw();
}
Don’t try to enter this code and expect it to work. This is just a template of how animation class might look like.
Your code has a bug:
if(currentSprite >= sprites.length) {
currentSprite = 0; // reset animation to start
}
currentSprite += 1; // increase frame counter by 1
This will cause currentSprite to never equal zero, therefore the first frame will never be displayed.
What it should be:
if(currentSprite == sprites.length - 1) {
currentSprite = 0; // reset animation to start
} else {
currentSprite ++; // increase frame counter by 1
}
Of course you want to run update with a timer or something.
Thanks DXU, I fixed the issue Didn’t even read what I wrote.
thx for the tutorial. I’ll try to make some invaders move since i’m trying to make my own space invaders. THX