Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (498)
Games in Android Showcase (116)
games submitted by our members
Games in WIP (563)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Need help with sprite cycles  (Read 1687 times)
0 Members and 1 Guest are viewing this topic.
Offline toomanystars

Junior Newbie





« Posted 2011-06-21 07:08:28 »

Hi guys,

I am a java noob, so please bear with me. For the past two weeks, I have found it difficult to cycle through images. I am Working on a simple 2D game, where I need the character to "walk". I have four Classes, Board, Character,Enemy, and Main(runs program).

In the Character Class, this is a part of the method I have:

public void keyPressed(KeyEvent e)

int key = e.getKeyCode():

   if(key == KeyEvent.VK_RIGHT){
   dx=1

   //need to cycle through 6 images here!


}

When I press the Right key, I need to have the character "walk" through 6 images, then, if I press the key again, the same needs to happen.

In the Board Class, I am using a Swing Timer with an update of 5ms.


This is a bit of an aside, but my Board Class is balooning into hundreds of lines of code to support all the "if" statments for enemies, and that's just the first screen! Is this normal? If I continue at this rate, at the end of the program, I will probably have a Board Class with thousands of lines of code.

Please help out a noob,

Mike
Offline pjt33
« Reply #1 - Posted 2011-06-21 07:18:09 »

You probably need more classes.

An Animation class could wrap a bunch of images and have a counter to indicate which one should currently be showing. Your Characters and Enemies could have a currentAnimation, or you could do a full MVC approach. (I doubt you'll want the latter yet, but bear it in mind for your second game).
Offline toomanystars

Junior Newbie





« Reply #2 - Posted 2011-06-21 15:01:24 »

Thanks for the reply,

So what you are saying is I should create a new "Animation Class" which will control the character and enemy walk cycles. Roughly what methods should I have, and how do run through 6 sprites using a Swing Timer (In my Board Class) with a 5ms update. I am worried that the sprites will updated so fast, they will not be noticable.

I have seen a lot of talk about something called a "Thread." Do I need to use it? Do I use BufferedImage[] to store the six sprites? I have no idea what a "full MVC approach." A little more detail in your anwser(s) would be greatly appriciated.

Sorry for all the questions, but all I am able to do right now is a simple 2D game with a character, bulltes, enemies, and collision.

Mike
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ra4king

JGO Kernel


Medals: 346
Projects: 3
Exp: 5 years


I'm the King!


« Reply #3 - Posted 2011-06-21 20:26:41 »

First of all, 5ms?!? Your game is going at 200FPS!! Try bringing it down to 16ms, 60FPS, that's all you'll ever need.

An Animation class holds an ArrayList of Images and usually has an update(long) method that updates the animation and a getImage() method that returns the current image in the Animation.

In your keyPressed method, you could set a boolean variable to true, so in your Timer, you would call the Animation's update method if that boolean is true, and in your paint method, you would draw the Image returned by the Animation's getImage() method.

Oh and MVC means Model-View-Controller, which basically means you separate the logic from the rendering from the data.

EDIT: Here is a basic Animation class:
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  
import java.awt.Image;
import java.util.ArrayList;

/**
 * A convenience Animation class used to animate 2D sprites.
 * @author Roi Atalla
 */

public class Animation {
   private ArrayList<Frame> frames;
   private long totalTime, currentTime;
   private int frameIndex;
   
   /**
    * Default constructor.
    */

   public Animation() {
      frames = new ArrayList<Frame>();
   }
   
   /**
    * Adds an image to the animation.
    * @param i The image to be added.
    * @param time The amount of time in milliseconds this image is displayed.
    */

   public void addFrame(Image i, long time) {
      totalTime += time;
      frames.add(new Frame(i,totalTime));
   }
   
   /**
    * Returns the current image displayed.
    * @return The current image displayed.
    */

   public Image getFrame() {
      return frames.size() == 0 ? null : frames.get(frameIndex).i;
   }
   
   /**
    * Must be called to update the animation
    * @param deltaTime The time passed since the last call to it.
    */

   public void update(long deltaTime) {
      if(frames.size() > 1) {
         currentTime += deltaTime;
         
         if(currentTime > totalTime) {
            frameIndex = 0;
            currentTime %= totalTime;
         }
         
         while(currentTime > frames.get(frameIndex).time)
            frameIndex++;
      }
   }
   
   private static class Frame {
      private Image i;
      private long time;
     
      public Frame(Image i, long time) {
         this.i = i;
         this.time = time;
      }
   }
}

Offline toomanystars

Junior Newbie





« Reply #4 - Posted 2011-06-21 21:21:53 »

Damn, thanks a lot, there's no way I would of been able to come up with that by myself. I don't have much time today, but I will ahve a good look at your Class tomorrow, and see what I can come up with.

Just a quick question about organizing 2D games. Should each level have it's own Class, or is it best just to have one humongous Board Class? When it comes to soundeffects, I assume these would be simple return methods inside their respective Classes. So for example a MachineGun Class would hold all the sounds for this gun, instead of one one Sound Class that holds everything.

Thanks again for your help, I thought I was doomed with static images for all my 2D games!

Mike
Offline ra4king

JGO Kernel


Medals: 346
Projects: 3
Exp: 5 years


I'm the King!


« Reply #5 - Posted 2011-06-21 21:34:45 »

Is this a tile-based game or how is your level constructed?

And for sounds, it could be organized in any way you think is appropriate.
I usually have a Sound class that contains a HashMap that maps Strings to Clips and I have a simple method called play(String).

You've just got to think things through one by one in an organized and OO way.

Good luck Wink

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

radar3301 (12 views)
2014-09-21 23:33:17

BurntPizza (30 views)
2014-09-21 02:42:18

BurntPizza (22 views)
2014-09-21 01:30:30

moogie (20 views)
2014-09-21 00:26:15

UprightPath (28 views)
2014-09-20 20:14:06

BurntPizza (32 views)
2014-09-19 03:14:18

Dwinin (48 views)
2014-09-12 09:08:26

Norakomi (74 views)
2014-09-10 13:57:51

TehJavaDev (102 views)
2014-09-10 06:39:09

Tekkerue (50 views)
2014-09-09 02:24:56
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!