Show Posts
|
|
Pages: [1]
|
|
1
|
Game Development / Newbie & Debugging Questions / Re: Error with animation using spriteSheets
|
on: 2012-08-24 18:47:15
|
NOTE: Sorry about the previous post, I didn't think to put the code tags, and I do tend to overy exagerate spaces  . Here is the correct format. 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. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
| import java.awt.*; import java.awt.event.*; import java.awt.image.BufferedImage; import java.io.IOException; import java.net.URL; import java.util.logging.Level; import java.util.logging.Logger; import javax.imageio.ImageIO; import javax.swing.*; import javax.swing.plaf.basic.BasicTabbedPaneUI.MouseHandler; import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.awt.image.BufferedImage; import java.io.IOException; import java.util.ArrayList; import java.util.logging.Level; import java.util.logging.Logger;
public class Animation extends JFrame{
BufferedImage stillRight; static BufferedImage right[]; Image dbImage; Graphics dbg;
static Menu menu = new Menu();
int
GWIDTH = 600,
GHEIGHT = 525;
Dimension screenSize = new Dimension(GWIDTH, GHEIGHT); public Animation(){ this.setTitle("LendaryCastle"); this.setSize(screenSize); this.setResizable(false); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); this.setBackground(Color.DARK_GRAY); } public static void main(String[] args){ Animation main = new Animation(); } private void init(){ BufferedImage spriteSheet = null; try { spriteSheet = loadImage("Monk2.png"); } catch (IOException ex) { Logger.getLogger(Animator.class.getName()).log(Level.SEVERE, null, ex); } SpriteSheet ss = new SpriteSheet(spriteSheet); right = new BufferedImage[5]; right[0] = ss.grabSprite(19*256, 512, 256, 256); right[1] = ss.grabSprite(2*256, 512, 256, 256); right[2] = ss.grabSprite(12*256, 512, 256, 256); right[3] = ss.grabSprite(5*256, 512, 256, 256); right[4] = ss.grabSprite(25*256, 512, 256, 256); } public BufferedImage loadImage(String pathRelativeToThis) throws IOException{ URL url = this.getClass().getResource(pathRelativeToThis); BufferedImage img = ImageIO.read(url); return img; } @Override public void paint(Graphics g){ dbImage = createImage(getWidth(), getHeight()); dbg = dbImage.getGraphics(); draw(dbg); g.drawImage(dbImage, 0, 25, this); } public void draw(Graphics g){ for(int i = 0; i<right.length; i++){ stillRight = right; g.drawImage(stillRight, i*30, 0, 60, 55, null); try { Thread.sleep(100); } catch (InterruptedException ex) { Logger.getLogger(Animation.class.getName()).log(Level.SEVERE, null, ex); } } } repaint(); } } |
|
|
|
|
|
2
|
Game Development / Newbie & Debugging Questions / Error with animation using spriteSheets
|
on: 2012-08-24 17:49:11
|
|
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 GWIDTH*GHEIGHT 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(19*256, 512, 256, 256); 083 right[1] = ss.grabSprite(2*256, 512, 256, 256); 084 right[2] = ss.grabSprite(12*256, 512, 256, 256); 085 right[3] = ss.grabSprite(5*256, 512, 256, 256); 086 right[4] = ss.grabSprite(25*256, 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; 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 }
|
|
|
|
|