473,394 Members | 1,567 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

loop through images once

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
1 1900
r035198x
13,262 8TB
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

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

Similar topics

7
by: Wayne | last post by:
I have a script that uses filesystemobject that reads files from a given path, in my case images. It is running on a server that is 2000 adv svr w/ all current patches. The script prior to some...
3
by: Simon | last post by:
This problem has been driving me mad for months.... Seen a few posts on forums about it but no answers... No mention on MSDN etc. XP Pro SP1, VS.NET (c#) .Net framework 1.1, IIS 5.1. In a...
10
by: Neo Geshel | last post by:
I am seeking to hand-roll my own blog in ASP.NET 2.0 and SQLExpress 2005. Why? Because I can. Because I will gain experience. The one thing that has me stumped at square one is inline images....
6
by: wattanabi | last post by:
Greetings, I'm attempting to layout a bunch of images in a grid using DIV's instead of a table. I currently have a 3x6 table that I need to convert to css. I've seen various example of a 3 to 4...
2
by: mouseit101 | last post by:
Hi, I'm writing a script that would (hopefully) search google images for whatever, and then return a list of URLs for the image. Right now I have: $dom = new DomDocument(); $url =...
0
by: Frenchie | last post by:
Hi, I have created a very neet menu from an example found on the MSDN library at: http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.menuitembinding.imageurlfield.aspx My...
4
toxicpaint
by: toxicpaint | last post by:
Hi, can anyone give me a hand. I'm currently displaying 4 random images at the top of a page. I did this using an array of 35 pictures and then writing them to page. The problem I have is that in...
5
by: remon87 | last post by:
I need some help. I have javasript that creates the submenu but it works if I have a text with css. I need it to do the same with a roll over images. so when I click on the image the submenu...
2
by: Prashanthpacchu | last post by:
Hi I am developing an website. So in that i m trying to make like an image slide show. just like in www.habtoor.com. But i am using 3 buttons on leftside. 6 images are used for this 3 buttons....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...

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.