Show Posts
|
|
Pages: [1]
|
|
3
|
Game Development / Newbie & Debugging Questions / Re: runtime error accessing ArrayList
|
on: 2007-12-12 21:01:04
|
I thought that was never supposed to happen because of the 1 2
| animTime = animTime % totalDuration; |
I left the Animation class untouched so that would mean the code in the book is wrong. Or I made a mistake copying. I think the fault is somewhere in my test class. oh by the way your hotfix gave a compile error. I added something similar, It got rid of the error message, but paint is still only called once, before the images are loaded.
|
|
|
|
|
4
|
Game Development / Newbie & Debugging Questions / runtime error accessing ArrayList
|
on: 2007-12-12 05:29:59
|
These 2 classes are basically the classes from the book developing games in java, without the fullscreen stuff. They are supposed to create an animation. They both compile fine but when I run I get an error message, an no animation, paint() is only called once. Here's the error message: Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 4, Size: 4 at java.util.ArrayList.RangeCheck(ArrayList.java:546) at java.util.ArrayList.get(ArrayList.java:321) at Animation.getFrame(Animation.java:60) at Animation.update(Animation.java:43) at AnimationTest1.animationLoop(AnimationTest1.java:58) at AnimationTest1.run(AnimationTest1.java:44) at AnimationTest1.main(AnimationTest1.java:10) I think this means the program is trying to access frame 4 when there is none. Right? I just can't get my brain around this. Any help would be great! 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
| import java.awt.Image; import java.util.ArrayList;
public class Animation { private ArrayList frames; private int currFrameIndex; private long animTime; private long totalDuration; public Animation() { frames = new ArrayList(); totalDuration = 0; start(); } public synchronized void addFrame(Image image, long duration) { totalDuration += duration; frames.add(new AnimFrame(image, totalDuration)); } public synchronized void start() { animTime = 0; currFrameIndex = 0; } public synchronized void update(long elapsedTime) { if (frames.size() >1) { animTime += elapsedTime; if (animTime >= totalDuration) { animTime = animTime % totalDuration; currFrameIndex = 0; } while (animTime > getFrame(currFrameIndex).endTime) { currFrameIndex ++; } } } public synchronized Image getImage() { if (frames.size() == 0) { return null; } else { return getFrame(currFrameIndex).image; } } private AnimFrame getFrame(int i) { return (AnimFrame)frames.get(i); } private class AnimFrame { Image image; long endTime; public AnimFrame(Image image, long endtime) { this.image = image; this.endTime = endTime; } } } |
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
| import java.awt.*; import javax.swing.ImageIcon; import javax.swing.JFrame; public class AnimationTest1 { public static void main (String args[]) { AnimationTest1 test = new AnimationTest1(); test.run(); } private static final long DEMO_TIME = 5000; private JFrame frame; private Animation anim; public void loadImages() { Image player1 = loadImage("gif1.png"); Image player2 = loadImage("gif2.png"); Image player3 = loadImage("gif3.png"); anim = new Animation(); anim.addFrame(player1, 200); anim.addFrame(player2, 200); anim.addFrame(player3, 200); anim.addFrame(player2, 200); } private Image loadImage(String filename) { return new ImageIcon(filename).getImage(); } public void run() { try { frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(300, 300); frame.setVisible(true); loadImages(); animationLoop(); } finally { } } public void animationLoop() { long startTime = System.currentTimeMillis(); long currTime = startTime; while (currTime - startTime < DEMO_TIME) { long elapsedTime = System.currentTimeMillis() - currTime; currTime += elapsedTime; anim.update(elapsedTime); Graphics g = frame.getGraphics(); draw(g); g.dispose(); try { Thread.sleep(20); } catch (InterruptedException ex) {} } } public void draw(Graphics g) { g.setColor(Color.white); g.fillRect(0, 0, 300, 300); g.drawImage(anim.getImage(), 0, 0, null); } } |
|
|
|
|
|
10
|
Game Development / Newbie & Debugging Questions / Re: Cannot get this to work properly.
|
on: 2007-04-14 00:18:17
|
|
My java book shows how to do this but it uses deprecated methods. Fletchergames, when you say take out the thread alltogether, how would you control the speed of the animation? Or use doublebuffering? When I followed SimonH,s advice the ball wouldn't move at all. Is there a way of slowing down a loop, timers or something?
|
|
|
|
|
11
|
Game Development / Newbie & Debugging Questions / Re: Cannot get this to work properly.
|
on: 2007-04-13 02:34:20
|
Thanks for the replies! I think this is the reason for not moving along x 1. That should be if clickX < posX
the first if clickX > posX moves the ball to the right, and the second if clickX >posX moves it right back!! Silly me!  I changed the code and added in sleep() so the ball moves slowly. 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
| import java.applet.*; import java.awt.*; import java.awt.event.*;
public class App extends Applet implements MouseListener, Runnable { Image offscreen; Graphics buffer; int posX, posY, clickX, clickY; Thread th;
public void init() { offscreen = this.createImage(100,100); buffer = offscreen.getGraphics();
addMouseListener(this); }
public void paint(Graphics g) { buffer.setColor(Color.white); buffer.fillRect(0,0,100,100); buffer.setColor(Color.red); buffer.fillOval(posX - 5,posY - 5,10,10); g.drawImage(offscreen,1,1,this); }
public void start() { if(th == null) { th = new Thread(this); } }
public void stop() { }
public void mouseClicked (MouseEvent me) { clickX = me.getX(); clickY = me.getY(); th.start(); }
public void run() { while(isActive()) { try { if(clickX > posX) { posX ++; } else if(clickX < posX) { posX --; } if(clickY > posY) { posY ++; } else if(clickY < posY) { posY --; } if(posX != clickX || posY !=clickY) { repaint(); th.sleep(100); } } catch(Exception e) { } } } public void mouseEntered (MouseEvent me) {} public void mousePressed (MouseEvent me) {} public void mouseReleased (MouseEvent me) {} public void mouseExited (MouseEvent me) {}
} |
It all works but the second time (and following times) MouseClicked is called I get like 8 error messages in command. I think its because I call start when the thread is already running but I cant see any other way of doing it. Looks like I need some help once again!
|
|
|
|
|
13
|
Game Development / Newbie & Debugging Questions / Cannot get this to work properly.
|
on: 2007-04-12 06:28:25
|
This is the code for my first game. So far Its supposed to be a circle that moves to wherever you click the mouse. The code compiles fine but when I run the applet the circle only moves along the y-axis, and not across. Could someone please tell me what's wrong with it? 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 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
| import java.applet.*; import java.awt.*; import java.awt.event.*;
public class App extends Applet implements MouseListener, Runnable { Image offscreen; Graphics buffer; int posX, posY, clickX, clickY; Thread th;
public void init() { offscreen = this.createImage(100,100); buffer = offscreen.getGraphics();
addMouseListener(this); }
public void paint(Graphics g) { buffer.setColor(Color.white); buffer.fillRect(0,0,100,100); buffer.setColor(Color.red); buffer.fillOval(posX - 5,posY - 5,10,10); g.drawImage(offscreen,1,1,this); }
public void start() { if(th == null) { th = new Thread(this); } }
public void stop() { }
public void mouseClicked (MouseEvent me) { clickX = me.getX(); clickY = me.getY(); th.start(); }
public void run() { while(isActive()) { try { if(clickX > posX) { if(clickY > posY) { posX ++; posY ++; repaint(); } if(clickY < posY) { posX ++; posY --; repaint(); } if(clickY == posY) { posX++; repaint(); } } if(clickX > posX) { if(clickY > posY) { posX --; posY ++; repaint(); } if(clickY < posY) { posX --; posY --; repaint(); } if(clickY == posY) { posX--; repaint(); } } if(clickX == posX) { if(clickY > posY) { posY ++; repaint(); } if(clickY < posY) { posY --; repaint(); } if(clickY == posY) { repaint(); th.interrupt(); } } } catch(Exception e) { } } } public void mouseEntered (MouseEvent me) {} public void mousePressed (MouseEvent me) {} public void mouseReleased (MouseEvent me) {} public void mouseExited (MouseEvent me) {}
} |
|
|
|
|
|
14
|
Game Development / Game Play & Game Design / Re: Whata classes to use?
|
on: 2007-04-12 06:20:45
|
|
I have made a textbased game and a gui game and this was supposed to be my first applet game. I think I'll try to make a graphical application first as I have some experience in that and then make it into an applet later. Thanks for the replies, Riven.
|
|
|
|
|
15
|
Game Development / Game Play & Game Design / Re: Whata classes to use?
|
on: 2007-04-11 23:58:43
|
|
(after doing some internet searches on visitor pattern)
I think I understand what you mean but I don't see how I would get that to work in an applet. What if I got update() to call my visitor pattern which then calls paint? Would that work?
|
|
|
|
|
16
|
Game Development / Game Play & Game Design / Whata classes to use?
|
on: 2007-04-11 04:09:44
|
|
I'm trying to make a fairly basic rpg but I keep getting stuck with what classes to make. I'm trying to have it all rendered (vector graphics?) but I'm stuck with what classes used for drawing. For example: Would you put the code for drawing and animating your player in a seperate class to the main player class. And would you have another class for what the player looks like when going in different directions? I think my main issue is that its not using images so you have to have different painting routines for different directions of movement. Any help to sort of unjumble my mind would be very welcome!
|
|
|
|
|
17
|
Game Development / Newbie & Debugging Questions / Re: Is this a good tutorial?
|
on: 2007-04-10 09:55:30
|
Netscape 4? WTF? Isn't that thing also like 10 years old?
Try Firefox or Opera (a current version... don't get em from some browser archive again o_O).
I meant the screenshots lol. I use opera 9! Thanks for the replies.I think I will use the cokeandcode tutorial first!
|
|
|
|
|
22
|
Game Development / Newbie & Debugging Questions / just starting
|
on: 2006-06-21 03:41:14
|
|
I know some basic java and was wandering if anyone could tell me what would be the best thing to do next.
Eventually I want to make a 3d game but that'll take some time.
Especially tutorials would be good.
|
|
|
|
|
|
Add your game by posting it in the WIP section,
or publish it in Showcase.
The first screenshot will be displayed as a thumbnail.
|
|