Hi, this is my first post to Java-Gaming (Newbie alert),
and I’m having a little trouble with Animation,
I used a custom 2D Animation class for a game I am working on
and the animation of the sprite runs well, it’s just
When I first move up, the sprite disappears for a split second, then reappears,
same results when I move down. It’s only the initial press of the keys, after that, the animation is smooth,
oddly enough, the right/left movement doesn’t have the hiccup on the first press of the keys.
Any idea what could be the cause for this?
Hope I explained it well-enough, if not, let me know!
Note: I am only using the standard Java libraries
Here’s the Animation class (runAnimation() is called in ‘update’, drawAnimation() is called in ‘render’):
import java.awt.Graphics;
import java.awt.image.BufferedImage;
public class Animation {
private int animSpeed; //Animation speed.
private int count = 0; //Works with the animSpeed, to control the animation speed.
private int frames; //Number of frames in the animation.
private int index = 0; //Current frame.
private BufferedImage[] animation;
private BufferedImage currentImage; //The current image in the animation.
public Animation(BufferedImage[] images, int speed) {
animation = images;
animSpeed = speed;
frames = animation.length;
}
public void runAnimation() {
count++;
if(count > animSpeed)
{
count = 0;
currentImage = animation[index % frames]; //Change the image for drawAnimation.
index++; //For next image.
}
}
public void drawAnimation(Graphics g, int x, int y, int scaleX, int scaleY) {
g.drawImage(currentImage, x, y, scaleX, scaleY, null);
}
}