0

I am working on trying to make a game as a side project with a few friends of mine. But I am kind of stuck at animating in java.

    private void render() {//Everything that renders
    BufferStrategy bs = this.getBufferStrategy();

    if (bs == null) {
        createBufferStrategy(3);//Triple buffering, increases speed overtime.
        return;
    }

    Graphics g = bs.getDrawGraphics();//Draws out buffers
    ////////////////////////////////////////////////////////////////////////////////////////
    //Between these comment lines is where images can be drawn out to the screen
    g.drawImage(image, 0, 0, getWidth(), getHeight(), this);//Draws image

    character.render(g);

    //End of drawing
    ////////////////////////////////////////////////////////////////////////////////////////
    g.dispose();//Destroys image
    bs.show();//Shows buffer


}

So this is the render method I am using to render my character onto the screen.

public void keyPressed(KeyEvent e) {
    int key = e.getKeyCode();

    if (key == KeyEvent.VK_W) {
        character.setVelY(-2);
    } else if (key == KeyEvent.VK_A) {
        character.setVelX(-2);
    }else if (key == KeyEvent.VK_S) {
        character.setVelY(2);
    }else if (key == KeyEvent.VK_D) {
        character.setVelX(2);
    }
}

And this is where I am checking to see if the button is being held down. I was trying to run a thread in the button checking to update the image every 200 milliseconds to a different image. Then to reset it to the first image in the line. But that wasn't working. Is there something else I should of done? Or is there another way to handle animating? Or should I attempt to make an animation class?

EDIT

The issue I am having does not deal with actually getting the image to move across the screen. But having the image switch back and forth as a key is being held down.

Community
  • 1
  • 1
Auti
  • 23
  • 3
  • So, basically, you need a "main loop", which, waits a prescribed amount of time, this is used to maintained the frame rate (and if done right, let the CPU do other things), it will update the game state and perform a render pass. Typically, this is all done within it's own thread (when using a `BufferStrategy`). Instead of `KeyListener`, I would recommend using the key bindings API. You would, when a given key stroke is pressed, set a flag indicating it's "pressed" state and when released, set a flag to represent it's "released" state. Then in your update loop, check the states – MadProgrammer Jan 26 '18 at 02:21
  • [This example](https://stackoverflow.com/questions/24856918/how-to-eliminate-delay-in-keypress/24857086#24857086) demonstrates the use of key bindings and basic "input store", which is the used by a "main loop" to make determinations about how the state should be updated – MadProgrammer Jan 26 '18 at 02:24
  • [A "very basic" example of a main loop using `BufferStrategy`](https://stackoverflow.com/questions/29000635/why-doesnt-my-screen-turn-red/29000677#29000677) – MadProgrammer Jan 26 '18 at 02:26
  • [Another example](https://stackoverflow.com/questions/34477332/why-is-my-eclipse-not-setting-my-background-color/34477410#34477410) demonstrating a mechanism for constraining the FPS to a constant rate - not a fan of it, as it causes the thread to run hot while waiting, which takes away cycles from the CPIU, but you get the idea – MadProgrammer Jan 26 '18 at 02:30
  • [Another example](https://stackoverflow.com/questions/25419808/frames-painting-at-different-times/25468229#25468229) for constraining FPS, but using `Thread.sleep` instead - not as accurate as the previous example, but it puts the thread to sleep allowing the CPU to focus on other things – MadProgrammer Jan 26 '18 at 02:31
  • I am using a main loop and a BufferStrategy. I do admit that the way that I am using the KeyListener to listen to specific keys is somewhat inefficient. Also I do have the frames per second limited down to sixty using this exact same method. The issue I am having is where I am trying to update the image I believe. I am able to get the image to move across the screen with ease. But I want to rapidly change the image so that it appears the character is walking around instead of just sliding. @MadProgrammer – Auti Jan 26 '18 at 21:24
  • The main loop is responsible for determining what should take place. This means, as I said, when you detect a key press, you set a flag indicating the state of the key, the main loop will then check the state of the key and update the state accordingly, when released, you reset the flag – MadProgrammer Jan 26 '18 at 21:33
  • Oh okay, thank you. I will try that out. – Auti Jan 26 '18 at 21:54

0 Answers0