SHARE
TWEET

Untitled

Lunastras Dec 8th, 2019 76 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import javafx.scene.image.Image;
  2.  
  3. /**
  4.  * This class handles the sprite animation of the player.
  5.  * Code originally from https://github.com/foreignguymike/legacyYTtutorials/tree/master/dragontale , but modified for the purposes
  6.  * of CS-230 A2.
  7.  * @Author ForeignGuyMike
  8.  * @Author Radu Bucurescu
  9.  */
  10.  
  11. public class SpriteAnimation {
  12.  
  13.     private Image[] frames;
  14.     private int currentFrame;
  15.  
  16.     private long startTime;
  17.     private long delay;
  18.  
  19.     private boolean playedOnce;
  20.  
  21.     /**
  22.      * Initialise the animator for the player's sprite.
  23.      */
  24.     public SpriteAnimation() {
  25.         playedOnce = false;
  26.     }
  27.  
  28.     /**
  29.      * Set the frames of the current animation.
  30.      * @param frames The sprites.
  31.      */
  32.     public void setFrames(Image[] frames) {
  33.         this.frames = frames;
  34.         currentFrame = 0;
  35.         startTime = System.nanoTime();
  36.         playedOnce = false;
  37.     }
  38.  
  39.     /**
  40.      * Set the delay between frames.
  41.      * @param d The time in milliseconds.
  42.      */
  43.     public void setDelay(long d) {
  44.         delay = d;
  45.     }
  46.  
  47.     /**
  48.      * Set the current frame of the animation.
  49.      * @param i The index of the frame.
  50.      */
  51.     public void setFrame(int i) {
  52.         currentFrame = i;
  53.     }
  54.  
  55.     /**
  56.      * Update the animation.
  57.      */
  58.     public void update() {
  59.         if(delay == -1) return;
  60.  
  61.         long elapsed = (System.nanoTime() - startTime) / 1000000;
  62.  
  63.         if(elapsed > delay) {
  64.  
  65.             currentFrame++;
  66.             startTime = System.nanoTime();
  67.         }
  68.         if(currentFrame == frames.length) {
  69.  
  70.             currentFrame = 0;
  71.             playedOnce = true;
  72.         }
  73.     }
  74.  
  75.     /**
  76.      * Get the index of the current frame of the animation.
  77.      * @return The index.
  78.      */
  79.     public int getFrame() {
  80.         return currentFrame;
  81.     }
  82.  
  83.     /**
  84.      * Get the current frame in the animation.
  85.      * @return The frame.
  86.      */
  87.     public Image getImage() {
  88.         return frames[currentFrame];
  89.     }
  90.  
  91.     /**
  92.      * Checks if the animation has played at least once.
  93.      * @return playedOnce.
  94.      */
  95.     public boolean hasPlayedOnce() {
  96.         return playedOnce;
  97.     }
  98.  
  99. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top