So I have been trying to figure out for so long how I can loop the frames on my animation automatically without entering user input to prompt movement of my object. I have four different frames for a sprite which I would like to loop, but at the moment it only shows the first frame. Here is the relevant code for my Play class (as the entire thing is too long):
package simpleslickgame;
import org.newdawn.slick.*;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
import java.util.Random;
public class PlayScreen extends BasicGameState {
Image enemy;
int enemyX = 800;
int enemyY = 500;
int RandomX;
int RandomY;
Animation blobfish, one, two, three, four;
public PlayScreen(int state) {
}
public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
enemy = new Image("C:\\Users\\User\\IdeaProjects\\Slick2D\\src\\main\\resources\\enemy.png");
Image[] blobfishOne = {new Image("C:\\Users\\User\\IdeaProjects\\Slick2D\\src\\main\\resources\\blob-one.gif"), new Image("C:\\Users\\User\\IdeaProjects\\Slick2D\\src\\main\\resources\\blob-one.gif")};
Image[] blobfishTwo = {new Image("C:\\Users\\User\\IdeaProjects\\Slick2D\\src\\main\\resources\\blob-two.gif"), new Image("C:\\Users\\User\\IdeaProjects\\Slick2D\\src\\main\\resources\\blob-two.gif")};
Image[] blobfishThree = {new Image("C:\\Users\\User\\IdeaProjects\\Slick2D\\src\\main\\resources\\blob-three.gif"), new Image("C:\\Users\\User\\IdeaProjects\\Slick2D\\src\\main\\resources\\blob-three.gif")};
Image[] blobfishFour = {new Image("C:\\Users\\User\\IdeaProjects\\Slick2D\\src\\main\\resources\\blob-four.gif"), new Image("C:\\Users\\User\\IdeaProjects\\Slick2D\\src\\main\\resources\\blob-four.gif")};
one = new Animation(blobfishOne, 200,true);
two = new Animation(blobfishTwo, 200, true);
three = new Animation(blobfishThree, 200, true);
four = new Animation(blobfishFour, 200, true);
blobfish = one;
}
public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
blobfish.draw(RandomX, RandomY);
Random enemyX = new Random();
Random enemyY = new Random();
RandomX = enemyX.nextInt(1500);
RandomY = enemyY.nextInt(1000);
}
public void update(GameContainer gc, StateBasedGame sbg, int delta) {
Input input = gc.getInput();
blobfish.start(); //I thought this would have ran the animation.
isMoving = true;
keyispressed = true;
if (keyispressed) {
isMoving = true;
}
}
public boolean mouseClicked() {
return true;
}*/
}
public int getID () {
return 1;
}
}
I did think that the 'blobfish.start();' line would have started the animation. The variable 'blobfish' is the animation and 'enemy' is the image.
I'm a serious noob when it comes to Java game dev so any help as to why the animation is not running would be greatly appreciated.