In this java game, I have an animation class that renders frames from a spritesheet. For the attack specifically, I made a different render method (renderAttack) because I want it to render all 16 frames of the animation, with 30 milliseconds in between each. I was looking into how to do delay the drawImage call, and decided a Swing Timer may be the best option. However, I am having the hardest time putting this in. Here is what I am trying to do, WITHOUT the timer:
public class Animation {
public void renderAttack(Graphics g, int x, int y, int width, int height) {
for(int index = 0; index < 16; index++)
{
g.drawImage(images[index], x, y, width, height, null);
//wait 30ms
}
}
}
To wait those 30 ms, I tried using the timer here. And as of now I have this:
public class Animation {
public void renderAttack(Graphics g, int x, int y, int width, int height) {
ActionListener taskPerformer = new ActionListener();
Timer timer = new Timer(30, taskPerformer);
timer.start();
}
}
However, where would this go, and what is the ActionEvent that it takes in? And how can I pass in the index variable?
public void actionPerformed(ActionEvent e) {
g.drawImage(images[index], x, y, width, height, null);
}
Hopefully this made any sense...I'll fix it step by step for now.