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.