#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    65
    Rep Power
    2

    Loop though only once than stop


    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.



    Code:
       public class Enemy{
           private BufferedImage[] bEnemyhit = {SpriteSheet.getSprite(6, 0),         SpriteSheet.getSprite(7, 0), 
                                             SpriteSheet.getSprite(8, 0),     SpriteSheet.getSprite(9, 0),};
        
        private SpriteAnimation aEnemyHit = new SpriteAnimation(bEnemyhit, 10, false);     //set loop vairable in spriteAnimation.class to false
        private SpriteAnimation animationEnemyHit = aEnemyHit;
        private boolean flag = false;
        ...
        public void enemyMove()    //this method is in main game loop
        {
            x -= dx;
            if(!flag){            //start only onces
                this.animationEnemyHit.start();
                flag = true;
            }
            this.animationEnemyHit.update();
        
        } 
        ...
         }
    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.



    Code:
        public class SpriteAnimation {
        
        private int frameCount;                 // Counts ticks for change
        private int frameDelay;                 // frame delay 1-12 (You will have to play around with this)
        private int currentFrame;               // animations current frame
        private int animationDirection;         // animation direction (i.e counting forward or backward)
        private int totalFrames;                // total amount of frames for your animation
        
        private boolean stopped;                // has animations stopped
        
        private boolean loop;                  //loop for ever or loop onces
        
        private List<SpriteFrame> SpriteframeObject = new ArrayList<SpriteFrame>();    // Arraylist of frames 
        
        public SpriteAnimation(BufferedImage[] frames, int frameDelay, boolean shouldLoop) {
            this.frameDelay = frameDelay;
            this.stopped = true;
            this.loop = shouldLoop;   //see if it has to loop or no
        
            for (int i = 0; i < frames.length; i++) {
                addFrame(frames[i], frameDelay);
            }
        
            this.frameCount = 0;
            this.frameDelay = frameDelay;
            this.currentFrame = 0;
            this.animationDirection = 1;
            this.totalFrames = this.SpriteframeObject.size();
        }
        
        
        
        
        public void start() {
            if (!stopped) {
                return;
            }
        
            if (SpriteframeObject.size() == 0) {
                return;
            }
            stopped = false;
        }
        
        public void stop() {
            if (SpriteframeObject.size() == 0) {
                return;
            }
            stopped = true;
        }
        
        public void restart() {
            if (SpriteframeObject.size() == 0) {
                return;
            }
            stopped = false;
            currentFrame = 0;
        }
        
        public void reset() {
            this.stopped = true;
            this.frameCount = 0;
            this.currentFrame = 0;
        }
        
        private void addFrame(BufferedImage frame, int duration) {
            if (duration <= 0) {
                System.err.println("Invalid duration: " + duration);
                throw new RuntimeException("Invalid duration: " + duration);
            }
        
            SpriteframeObject.add(new SpriteFrame(frame, duration));
            currentFrame = 0;
        }
        
        public BufferedImage getSprite() {
            return SpriteframeObject.get(currentFrame).getFrame();
        }
        
        public void update() {
            if (!stopped) {
                frameCount++;
        
                if (frameCount > frameDelay) {
                    frameCount = 0;
                    currentFrame += animationDirection;
        
                    if (currentFrame > totalFrames - 1) {
                        currentFrame = 0;
        
                       //if not looping for ever than stop
        
                        if(!loop) 
                        {
                            stopped = true;
                        }
                    }
        
                    else if (currentFrame < 0) {
                        currentFrame = totalFrames - 1;
                         if(!loop)
                           { 
                               stopped = true;
                            }
                    }
                }
        
            }
        
        }
        }

    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?
  2. #2
  3. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Feb 2004
    Location
    Mobile, Alabama
    Posts
    4,023
    Rep Power
    1285
    Do you have a subclass of JApplet or JFrame?
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    65
    Rep Power
    2
    Originally Posted by bullet
    Do you have a subclass of JApplet or JFrame?
    do you mean in paint method? this is my set up for paint method. i didnt want to paint dir on japplet so i am paint on jpanel.
    Code:
    public class Main extends JApplet ...
    ...
    //paint method
    	public class Display extends JPanel
    	{
    		private static final long serialVersionUID = 1L;
    
    		public void paintComponent(Graphics g)
    		{	
    ...
  6. #4
  7. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Feb 2004
    Location
    Mobile, Alabama
    Posts
    4,023
    Rep Power
    1285
    We don't see all of the code, so I would just speculate that possibly what you're seeing is the effect of only having one thread (the main thread) running. You might try creating a separate thread to draw your images.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    65
    Rep Power
    2
    Originally Posted by bullet
    We don't see all of the code, so I would just speculate that possibly what you're seeing is the effect of only having one thread (the main thread) running. You might try creating a separate thread to draw your images.
    i am pretty sure that there is no problem in thread, subclass or paint method. the problem is in two classes i post up.
  10. #6
  11. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Feb 2004
    Location
    Mobile, Alabama
    Posts
    4,023
    Rep Power
    1285
    Originally Posted by game2d
    i am pretty sure that there is no problem in thread, subclass or paint method. the problem is in two classes i post up.
    What I mean is when an applet begins executing, it is executing a particular thread. If you have loops or other things that need to run in update or paint, it's a good idea to run them in a separate thread.

IMN logo majestic logo threadwatch logo seochat tools logo