Hello!
First of all this is my fist time using this forum so please forgive me if I do something wrong.
I started developing a game, and managed to create a world, collision detection, etc… I’m now stuck on a very important part: animation. I got a sprite sheet, and creating a class that would let me take subimages. I then created an array list of these subimages for when the player moves right for example. I then tried to create a loop, that would take each subimage, draw them to the screen, wait 30 milliseconds, then draw the next one, etc…
It all worked out, though i encountered one problem: the previous image that was drawn to the screen does not disappear. I then tried to call the repaint() method after the 30 milliseconds, though that doesn’t work. I eventually told myself that the images were cycling maybe too quickly, so I created a new class, and made it 100 milliseconds, but even that didn’t work.
What im asking is for someone to help me fix this. I don’t know if I have to change the hole code, or just a bit. I did try to use a Timer, but that didn’t really work out, as I dont exactly know how to really use it.
Thank you in advanced, and here is the code:
NOTE: There will probably be errors in this code, as i have only taken the essential parts of it. The method i need help in is the draw() method. The init() method uses methods from another class that I haven’t included. Sorry to not have included everything, but it would have resulted in nearly 1000 lines of code, and complicate everything.
002 import java.awt.;
003 import java.awt.event.;
004 import java.awt.image.BufferedImage;
005 import java.io.IOException;
006 import java.net.URL;
007 import java.util.logging.Level;
008 import java.util.logging.Logger;
009 import javax.imageio.ImageIO;
010 import javax.swing.;
011 import javax.swing.plaf.basic.BasicTabbedPaneUI.MouseHandler;
012
013 import java.awt.;
014 import javax.swing.;
015 import java.awt.event.;
016 import java.awt.image.BufferedImage;
017 import java.io.IOException;
018 import java.util.ArrayList;
019 import java.util.logging.Level;
020 import java.util.logging.Logger;
021
022
023
024 public class Animation extends JFrame{
025
026
027 BufferedImage stillRight;
028
029 static BufferedImage right[];
030
031 //Double buffering
032 Image dbImage;
033 Graphics dbg;
034
035
036
037 //Initializing Menu.java
038 static Menu menu = new Menu();
039
040 //Variables for screen size
041 int
042 GWIDTH = 600,
043 GHEIGHT = 525;
044 //Dimension of GWIDTHGHEIGHT
045 Dimension screenSize = new Dimension(GWIDTH, GHEIGHT);
046
047
048
049 //Create constructor to spawn window
050 public Animation(){
051
052 this.setTitle(“LendaryCastle”);
053 this.setSize(screenSize);
054 this.setResizable(false);
055 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
056 this.setVisible(true);
057 this.setBackground(Color.DARK_GRAY);
058
059 }
060
061
062
063 public static void main(String[] args){
064 Animation main = new Animation();
065
066
067 }
068
069 private void init(){
070
071
072 BufferedImage spriteSheet = null;
073 try {
074 spriteSheet = loadImage(“Monk2.png”);
075 } catch (IOException ex) {
076 Logger.getLogger(Animator.class.getName()).log(Level.SEVERE, null, ex);
077 }
078 SpriteSheet ss = new SpriteSheet(spriteSheet);
079
080
081 right = new BufferedImage[5];
082 right[0] = ss.grabSprite(19256, 512, 256, 256);
083 right[1] = ss.grabSprite(2256, 512, 256, 256);
084 right[2] = ss.grabSprite(12256, 512, 256, 256);
085 right[3] = ss.grabSprite(5256, 512, 256, 256);
086 right[4] = ss.grabSprite(25256, 512, 256, 256);
087
088
089
090
091
092 }
093
094
095 public BufferedImage loadImage(String pathRelativeToThis) throws IOException{
096
097 URL url = this.getClass().getResource(pathRelativeToThis);
098 BufferedImage img = ImageIO.read(url);
099 return img;
100 }
101
102
103 @Override
104 public void paint(Graphics g){
105 dbImage = createImage(getWidth(), getHeight());
106 dbg = dbImage.getGraphics();
107 draw(dbg);
108 g.drawImage(dbImage, 0, 25, this);
109 }
110
111
112 public void draw(Graphics g){
113
114
115
116 for(int i = 0; i<right.length; i++){
117
118
119 stillRight = right[i];
120
121
122 g.drawImage(stillRight, i*30, 0, 60, 55, null);
123
124 try {
125
126 Thread.sleep(100);
127
128 } catch (InterruptedException ex) {
129 Logger.getLogger(Animation.class.getName()).log(Level.SEVERE, null, ex);
130 }
131 }
132
133
134
135
136
137 }
138
139
140 repaint();
141 }
142
143 }