Had you ever heard about something called Frame independent movement
you should consider make the movement based on time so you will have a consistent movement on all cases
suggest you some help :
create a class the wrap you animation movement
public class Animation {
public static final int ANIMATION_LOOPING = 0;
public static final int ANIMATION_NONLOOPING = 1;
private Image [] sprites;
private float frameDuration ;
public Animation (Image ... sprites, float frameDuration){
this.sprites =sprites;
this.frameDuration = frameDuration;
}
//this function takes a the walking time of the soldier
public Image getKeyFrame(float stateTime , int mode){
int frameNumber = (int) stateTime/ frameDuration;
if(mode == ANIMATION_NONLOOPING){
frameNumber = Math.min( sprites.length , frameNumber);
}
else{
frameNumber = frameNumber % sprites.length;
}
return sprites[frameNumber];
}
}
and you soldier should act as a model
public class Solider {
float walkingTime = 0;
int xpos ;
int ypos ;
int xspeed;
public Solider(int xpos,int ypos,int xspeed){
this.xpos = xpos;
this.ypos = ypos;
this.xspeed = xspeed;//movementPerSecond in the x axis for example -3
}
//deltaTime is the time elapsed in milliseconds
public void update(float deltaTime){
xpos += xspeed * (deltaTime / 1000.0);
walkingTime += deltaTime ;
}
}
then in you Game main world class define an Animation object that represent the animation you want to made for the soldiers and make as much as you want instances of soldiers objects this way you will define the animation once i.e you dont have to create 8 images each time you want to create a soldier and another benifet that you will have a loosly coupled application
example
public class MyGameWorld {
Animation soldierAnimation ;
Soldier [] soliders ;
public MyGameWorld(int numbers_of_soliders){
soliders = new Soldier[numbers_of_soliders];
//loop over the array to instantiate them
for( int i =0; i < numbers_of_soliders;++i){
soliders[i] = new Soldier(randomx ,randomy ,-5); // randomx , randomy is random values you can supply
}
//then instantiate the animation object
//first get the images
Image [] sprites = new sprites [8];
sprites [0] = new Image ......
soldierAnimation = new Animation(0.2, sprites);// this will make it flip each 0.2 seconds you can put any value that fits your need
}
//in the game loop update you soldiers
for(int i =0 ; i <numbers_of_soliders ; ++i){
soliders[i].update(deltaTime);
}
then render them using the animation
for(int i =0 ; i< number_of_soliders ; ++i){
drawOnScreenThisImageWithThisPosition( soliders[i].xpos, soliders[i].ypos,soldierAnimation.getKeyFrame(soliders[i].walkingTime, Animation.ANIMATION_LOOPING);
}
}