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

loop though only once then stop

P: 59
i have a enemy and spriteanimation classes. in Enemy class i have bufferedimage array with 4 different images. than i am creating a animation and storing in 'animationEnemyHit' variable. than i am starting and updateing the animation in method. the problem is that it keeps on loop for ever. i want it stop after one full cirlce. so after 4th image stop.



Expand|Select|Wrap|Line Numbers
  1.    public class Enemy{
  2.        private BufferedImage[] bEnemyhit = {SpriteSheet.getSprite(6, 0),         SpriteSheet.getSprite(7, 0), 
  3.                                          SpriteSheet.getSprite(8, 0),     SpriteSheet.getSprite(9, 0),};
  4.  
  5.     private SpriteAnimation aEnemyHit = new SpriteAnimation(bEnemyhit, 10, false);     //set loop vairable in spriteAnimation.class to false
  6.     private SpriteAnimation animationEnemyHit = aEnemyHit;
  7.     private boolean flag = false;
  8.     ...
  9.     public void enemyMove()    //this method is in main game loop
  10.     {
  11.         x -= dx;
  12.         if(!flag){            //start only onces
  13.             this.animationEnemyHit.start();
  14.             flag = true;
  15.         }
  16.         this.animationEnemyHit.update();
  17.  
  18.     } 
  19.     ...
  20.      }
SpriteAnimation.class i am setting 'loop' vairable to true or false(value coming from enemy.class). if its false than it should not loop for ever so stop after one circle. in update method i did set up a if statment but it is not working. it just keep on looping.



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

right now what is happing is that it only prints 1st image than stops. i want to do one full ciyle than stop. any ideas?
May 6 '13 #1
Share this Question
Share on Google+
1 Reply


Nepomuk
Expert 2.5K+
P: 3,112
Hi!
Looking at your code, there's one mayor problem I can see: It's missing the actual loop method. Now, from what I understand the update() method in SpriteAnimation does stuff every 10th time it's called (because frameDelay is set to 10 when a SpriteAnimation object is created in the Enemy object) and it is called from the enemyMove() method in the Enemy class. So, I'm guessing the latter method is called in a loop of some kind?
May 16 '13 #2

Post your reply

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