Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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 </p> <p>suggest you some help : create a class the wrap you animation movement </p> <pre><code>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]; } } </code></pre> <p>and you soldier should act as a model </p> <pre><code>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 ; } </code></pre> <p>}</p> <p><strong>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</strong> </p> <p>example </p> <pre><code>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 &lt; 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 &lt;numbers_of_soliders ; ++i){ soliders[i].update(deltaTime); } then render them using the animation for(int i =0 ; i&lt; number_of_soliders ; ++i){ drawOnScreenThisImageWithThisPosition( soliders[i].xpos, soliders[i].ypos,soldierAnimation.getKeyFrame(soliders[i].walkingTime, Animation.ANIMATION_LOOPING); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload