I was wondering if someone could look at my animation for me to see if I am doing it correctly. I am currently programming a basic game in Java, nothing fancy though, and I just want to get the basics down before venturing into something more complex.
Right now, I want to animate a character's attack. When the player left clicks, the character should go from looking like this:
To looking like this:
Currently, I am doing this by starting this thread when a left-click happens:
public class thread implements Runnable
{
long swordTime = 250;
public void run()
{
try {
player.setAttacking(true);
Thread.sleep(swordTime);
player.setAttacking(false);
Thread.yield();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Which changes a boolean
variable attacking
. Afterwards, when the character is rendered again, attacking will be true, so the variable will be rendered as the image with this code:
public void render(Graphics g)
{
if (attacking)
{
ImageIcon i = new ImageIcon("/Users/myName/Desktop/FarthornPics/charAttacking.png");
Image attckImg = i.getImage();
g.drawImage(attckImg, (int)Game.WIDTH/3, (int)y, null);
}
else
g.drawImage(charImage, Game.WIDTH/3, (int)y, null);
Although this method works, I was wondering if there was a better way to accomplish this. People often tell me that multi-threading is kind of a crude way to go about things, so I was looking for a better alternative (if this is not already the best).