By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,653 Members | 1,483 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,653 IT Pros & Developers. It's quick & easy.

loop through images once

P: 59
so i have two animation. one is when player is standing and one is when player get hit. 'standing' animation will keep looping for ever. and 'planehit' animation should loop though onces and stop.

in player class iam setting up animation by getting images. 'standing' animation has two images. 'planehit' has 4 images.

i need help modifing 'SpriteAnimation class' so that it will only loop though onces when player get hit. right now this code below keep looping both animation for ever.

**player class**

Expand|Select|Wrap|Line Numbers
  1.     ......
  2.     //get image for animation
  3.     private BufferedImage[] standing = {SpriteSheet.getSprite(0, 0), SpriteSheet.getSprite(1, 0)};
  4.     private BufferedImage[] planehit = {SpriteSheet.getSprite(0, 5), SpriteSheet.getSprite(1, 5), 
  5.                                         SpriteSheet.getSprite(2, 5), SpriteSheet.getSprite(3, 5),};
  6.  
  7.     // These are animation states
  8.     private SpriteAnimation aStand = new SpriteAnimation(standing, 10);
  9.     private SpriteAnimation aPlaneHit = new SpriteAnimation(planehit, 10);
  10.  
  11.     //This is the actual animation
  12.     private SpriteAnimation animationStand = aStand;
  13.     private SpriteAnimation animationPlaneHit = aPlaneHit;
  14.  
  15.  
  16. .........
  17.  
  18.     public void animation()
  19.     { 
  20.       animationStand.start();     //keep looping
  21.       if(i****)                      //if player get hit
  22.        animationPlaneHit.start();   //loop though onces but doesnt work
  23.  
  24.            animationStand.update();
  25.             animationPlaneHit.update();
  26.     }
  27.     ........
  28.     public void paint(...)
  29.     {
  30.     //make player standing a animation between two images.
  31.      //if player get hit set flag to true. i**** is only true when player get hit. soon as he is not hit it   
  32.      //turn to false. so if flag is true make animation.
  33.     g.drawImage(animationStand.getSprite(), (int)x,(int)y, width, height, null);
  34.        if(i****)
  35.            flag = true;
  36.        if(flag)
  37.            g.drawImage(animationPlaneHit.getSprite(), (int)x,(int)y, width, height, null);
  38.     }

**SpriteFrame**


Expand|Select|Wrap|Line Numbers
  1.     public class SpriteFrame {
  2.  
  3.         private BufferedImage frame;
  4.         private int duration;
  5.  
  6.         public SpriteFrame(BufferedImage frame, int duration) {
  7.             this.frame = frame;
  8.             this.duration = duration;
  9.         }
  10.  
  11.         public BufferedImage getFrame() {
  12.             return frame;
  13.         }
  14.  
  15.         public void setFrame(BufferedImage frame) {
  16.             this.frame = frame;
  17.         }
  18.  
  19.         public int getDuration() {
  20.             return duration;
  21.         }
  22.  
  23.         public void setDuration(int duration) {
  24.             this.duration = duration;
  25.         }
  26.     }
**SpriteAnimation**


Expand|Select|Wrap|Line Numbers
  1.     public class SpriteAnimation {
  2.         private int frameCount;                 // Counts ticks for change
  3.         private int frameDelay;                 // frame delay 1-12 (You will have to play around with this)
  4.         private int currentFrame;               // animations current frame
  5.         private int animationDirection;         // animation direction (i.e counting forward or backward)
  6.         private int totalFrames;                // total amount of frames for your animation
  7.  
  8.         private boolean stopped;                // has animations stopped
  9.  
  10.         private List<SpriteFrame> SpriteframeObject = new ArrayList<SpriteFrame>();    // Arraylist of frames 
  11.  
  12.         public SpriteAnimation(BufferedImage[] frames, int frameDelay) {
  13.             this.frameDelay = frameDelay;
  14.             this.stopped = true;
  15.  
  16.             for (int i = 0; i < frames.length; i++) {
  17.                 addFrame(frames[i], frameDelay);
  18.             }
  19.  
  20.             this.frameCount = 0;
  21.             this.frameDelay = frameDelay;
  22.             this.currentFrame = 0;
  23.             this.animationDirection = 1;
  24.             this.totalFrames = this.SpriteframeObject.size();
  25.  
  26.         }
  27.  
  28.         public void start() {
  29.             if (!stopped) {
  30.                 return;
  31.             }
  32.  
  33.             if (SpriteframeObject.size() == 0) {
  34.                 return;
  35.             }
  36.  
  37.             stopped = false;
  38.         }
  39.  
  40.         public void stop() {
  41.             if (SpriteframeObject.size() == 0) {
  42.                 return;
  43.             }
  44.  
  45.             stopped = true;
  46.         }
  47.  
  48.         public void restart() {
  49.             if (SpriteframeObject.size() == 0) {
  50.                 return;
  51.             }
  52.  
  53.             stopped = false;
  54.             currentFrame = 0;
  55.         }
  56.  
  57.         public void reset() {
  58.             this.stopped = true;
  59.             this.frameCount = 0;
  60.             this.currentFrame = 0;
  61.         }
  62.  
  63.         private void addFrame(BufferedImage frame, int duration) {
  64.             if (duration <= 0) {
  65.                 System.err.println("Invalid duration: " + duration);
  66.                 throw new RuntimeException("Invalid duration: " + duration);
  67.             }
  68.  
  69.             SpriteframeObject.add(new SpriteFrame(frame, duration));
  70.             currentFrame = 0;
  71.         }
  72.  
  73.         public BufferedImage getSprite() {
  74.             return SpriteframeObject.get(currentFrame).getFrame();
  75.         }
  76.  
  77.         public void update() {
  78.             if (!stopped) {
  79.                 frameCount++;
  80.  
  81.                 if (frameCount > frameDelay) {
  82.                     frameCount = 0;
  83.                     currentFrame += animationDirection;
  84.  
  85.                     if (currentFrame > totalFrames - 1) {
  86.                         currentFrame = 0;
  87.                     }
  88.                     else if (currentFrame < 0) {
  89.                         currentFrame = totalFrames - 1;
  90.                     }
  91.                 }
  92.             }
  93.         }
  94.     }
Apr 13 '13 #1
Share this Question
Share on Google+
1 Reply


10K+
P: 13,264
Your code says
Expand|Select|Wrap|Line Numbers
  1. public void update() {
  2.             if (!stopped) {
so stop it by setting the stopped variable to true.
Apr 15 '13 #2

Post your reply

Sign in to post your reply or Sign up for a free account.