473,473 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

loop though only once then stop

59 New Member
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
1 1414
Nepomuk
3,112 Recognized Expert Specialist
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

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

Similar topics

11
by: Aaron | last post by:
Is there a way I can loop through everything that is in session?
4
by: Ryan Liu | last post by:
How to loop though controls on the form? I set each control's TabStop and TabIndex, but at run time, I press Tab key, it jump though first control to last control and does not go back to the...
8
by: Matt Theule | last post by:
While stepping through an ASP.NET project, I found that data was being inserted into my database even though I was not stepping through the code that inserted the data. I have a single page with...
2
by: Peter | last post by:
Hi I have an application that opens a new form displaying an alert for every new alert the system receives. The code is running on a timer in a separate thread and all the alert forms are...
6
by: Stuart | last post by:
In VB6 I could cycle though loaded forms like this. Dim MyForm as Form For each MyForm in Forms debug.print MyForm.Name Next How can I do it in VB.NET?
2
by: Mike Walters | last post by:
I am trying to loop thought all the services on a system and stat/stop then as needed, I know How I can start or stop then, but how do I loop thought all installed services. Thanks Mike
7
by: randomtalk | last post by:
hello, recently i am reading "The C Programming Language, Second Edition", and for some reason, the sample program in there doesn't work as expected under GCC.. here is the code: #include...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.