0
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

Your main game loop should usually loop through 3 main processes.

Each of these processes usually run in context to a specific scene/screen in your game .. menu screen, gameplay screen, exit screen , etc.

Process input: this is usually where people use an observer design pattern and create an event dispatcher which dispatches events to game objects who are interested in receiving certain events .. mouse move events, keyboard events the game objects interpret those lower level events to “game events” and call functions on themselves

Update: this is where your game logic is updated where players positions should be. What everything should be “doing”

Render: this is where everything will finally get drawn.. sounds play all the magic of your game

All of these processes should follow ideas of single responsibility your characters shouldn’t know how to animate or draw .. that’s the responsibility of your render system to know how to draw characters.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.