Java-Gaming.org    
Featured games (78)
games approved by the League of Dukes
Games in Showcase (429)
Games in Android Showcase (89)
games submitted by our members
Games in WIP (468)
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  
  Animated NPCs  (Read 3332 times)
0 Members and 1 Guest are viewing this topic.
Offline GeorgiaW

Senior Newbie




Java games rock!


« Posted 2002-11-29 14:46:43 »

I've been stuck on one thing, and this will most likely come off as an extremely stupid question, but I thought I would give it a shot Smiley

I want to have animated characters on my screen, but if I do this, I have to loop and call repaint() all of the time even when my own character isn't moving.

This means I can't have any messages go to the screen, because they'll be wiped off everytime.

What  I'm planning on doing is:

1. Painting the tiles (water, ground, path, grash, etc.).
2. Painting objects (trees, treasure chests, people).

The people will be special tile objects that will have their own animation sequences so they look like they're waddling, sleeping, or doing something in their area.

Should I just paint them all to the screen with everything else and loop their animations and call "repaint()" all of the time, or should I create a label or panel object the size of the tile and have that panel object run it's own paint() method?
Offline Captain-Goatse

Junior Member




I suck at teh 2D. XBOX IS BIG LOL!111


« Reply #1 - Posted 2002-11-29 14:52:20 »

Why not store npcs in an array. This would allow them to move as well. Then have some sort of cell system where the loop determines what picture is going to be painted next.

I'm afraid that with your current system its not possible to keep the design simple and use only one thread for doing this. I see changing the behaviour of the messages as easier task than trying to figure out workaround or use two threads.

If you want animation, there has to be a rendering loop. If you want rendering loop there must manager which decides what is drawn. Perhaps you should make the messages as objects which will be displayed for x number of frames. This shouldn't be too hard to implement and in my opinion the best sollution.
Offline GeorgiaW

Senior Newbie




Java games rock!


« Reply #2 - Posted 2002-11-29 14:56:17 »

That's a good idea, thank you Smiley

I'm a little new at doing this in Java... I jumped over from Visual Basic, so... ick Smiley hehe.

I need to figure out what this VolatileImage thing is that everyone is raving about. I've just been painting to the background of JPanels so far...
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline GeorgiaW

Senior Newbie




Java games rock!


« Reply #3 - Posted 2002-11-29 22:12:46 »

Actually, would anyone have a really simplistic example of how to animate something?

In VB, I used a timer... but I'm not sure if that's the way to go here or not.

Meaning, I can make my own timer object for each NPC and set the delay on it, but it probably isn't the best way of accomplishing this delay between frames.
Offline Captain-Goatse

Junior Member




I suck at teh 2D. XBOX IS BIG LOL!111


« Reply #4 - Posted 2002-11-30 05:39:25 »

int count=0;
public void run(){

while(running){

count++
repaint();
updateCords();
updateFrame(count);
if(count = 3)
count = 0;

try{
thread.sleep(refresh_rate);
}catch (Exception exc){...}
}
}

In update cords, you move the objects however you want like for example in asteroids or make the player walk in rpg. In updateFrame, the loop decides what frame is painted next.

public updateFrame(int i){

npc.setPicture(pic);

}

Now this is very simple example and not very good for doing big animations since handling every single animation by your self is kind of stupid. With loops and simple anim design from Black Art of Java Game Programming(the sprite structure) its easy to handle even quite big scale animations where more than 10 objects must be animated.

I think you can get the source on the internet. If you need it, please ask me.

I'm quite sure you have to make some modification to the above code, and its not very practical to use the sleep method. At shared code there is the Timer Hack or you could use JNI system timer.


}
Offline GeorgiaW

Senior Newbie




Java games rock!


« Reply #5 - Posted 2002-11-30 13:37:27 »

Well, considering I have other things going on in my main program, I can't call sleep in there, because it causes EVERYTHING to sleep.

I think that I am going to need to extend the thread object Sad
Offline Captain-Goatse

Junior Member




I suck at teh 2D. XBOX IS BIG LOL!111


« Reply #6 - Posted 2002-11-30 14:32:05 »

You can skip untill ******** if you don't feel like reading a lots.

What you need is sleep based timer-hack. It makes you life tons and metric tons easier.


I don't even think that you need to extend the thread in this situation. In your approach you start moving mountains if the sollution doesn't come close to you, such as figuring a work around by extendin thread. This could be accomplished with much less work and brain power, but of course if you are the man for moving mountains, then man's got to do what man's go to do.

In your approach, the mountain is very hard to change. Thats why you need to work different way to do this.

The best thing for you would be setting FPS. All programs with animation have FPS and I can't think of exceptations, except for animated gifs. (HAR HAR I MAKES TEH FUNNAY)

Now, once you have set fps, let's say 60 or 75 would be great since about then the fps is high enough to be unnoticable. You must understand that refresh rate is the same as FPS, but done by your monitor. Computer on the other hand has capability to achieve much greater speeds than your monitor.

Although it might sound useless, it is not, since more objects you add to screen, the more it takes from the computer to do whatever required to keep them in animation.
*****************
Here is the simple part. Just set the thread running with the FPS, which is frames per second divided by 1000, since we are using milliseconds.

Now make that as the general speed of the loop. Now you have window/applet which screen content updates every fps/1000 millisecond. Amazing, isn't it. Now you want to have something to animate. This even easier. Just make method which decides what to draw, and when.

Now comes the fun part: If you want things to animatie different speeds and want to see actually see the animation you have to set how many loops each picture is visible.

If you don't set the delay for each picture to stay on screen, you will see bunch of pictures just flashing. This is not what you want. It makes you dizzy, you want animation, right?

Now you just set the delay, not hard and then decide how many loops each picture stays. You can do this by time too if you want the are afraid of high end systems going too fast, but if you have set cap FPS it won't go too hard.

I have seen many different implementations, but time is harder, and generally better sollution.

Each sprite has animationtime attribute, which is the number of renderloops it stays on screen or switches to next frame. When its at next frame again it takes certain amount of loops untill it switches again to next one.

I'm sorry for my primitive grasp of english language, but if you have more questions, I will answer.
Offline GeorgiaW

Senior Newbie




Java games rock!


« Reply #7 - Posted 2002-11-30 16:08:35 »

Ah ok... sounds fairly simple I guess Tongue

What I did this morning was extend the Thread class and let that determine how often it renders the next image in a sequence of images to produce the effect of animation.

I pass the thread a timeslice (milliseconds), the main program, the main program's graphics object, and a string name.

I calculate what should be shown and when and if it should stop running after a specific time. It manipulates the graphics object and passes it back to the main program's paint method, which then paints everything.

This works so far... it isn't very robust, so I need to modify it a bit, but it sounds close to what you're talking about... unless I'm totally off Tongue
Offline GeorgiaW

Senior Newbie




Java games rock!


« Reply #8 - Posted 2002-11-30 17:35:06 »

Ok another question!!! Smiley

I still have flickering even though DoubleBuffering is set to True on the swing components that I'm using.

What happens is that every few seconds the character blinks (disappears then reappears very quickly).

The following is the run method from my Thread class (AnimateSprite extends Thread):

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  
/*****************************************************************
   method:  run()
   purpose: Handles the determination of the next image, sets up
            the new graphics object properties and passes it back
            to the paint() method in the main program.
  *****************************************************************/

  public void run()
  {
    // If the thread is in running mode, then calculate
   // the next image to be displayed by the program
   while(running)
    {
      // First image
     if (mode == MODE_ON)
      {
        background = Toolkit.getDefaultToolkit().getImage(firstimage);
        //System.out.println("Printing player_old.gif");
       mode = MODE_OFF;
      }
      // Second image
     else
      {
        background = Toolkit.getDefaultToolkit().getImage(secondimage);
        //System.out.println("Printing player2.gif");
       mode = MODE_ON;
      }

      // Repaint the scene to clear what was there last
     mainprog.repaint();

      // Draw the new image
     g.drawImage(background, xpos, ypos, mainprog);

      // Put the thread to sleep for the specified amount of time
     // in the sleep duration variable
     try{
        Thread.sleep(sleeptime);
      }
      catch (InterruptedException e)
      {}
    }
   
    // If we are no longer running, we want to repaint the scene
   if (!running)
    {
      mainprog.repaint();
    }
  }
Offline swpalmer

JGO Coder




Where's the Kaboom?


« Reply #9 - Posted 2002-12-01 00:22:12 »

Flickering is likely caused because you are not using swing rendering..

Quote
mainprog.repaint();
 
 // Draw the new image
 g.drawImage(background, xpos, ypos, mainprog);  


SO you are triggering drawing in the GUI thread AND painting in this other thread...  you are asking for trouble.  Keep all the painting in one thread.  If you can't then you will at least need to synchronize the painting between the threads somehow.. but that is too complex.  You shouldn't need to paint in more than one thread.

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

Senior Newbie




Java games rock!


« Reply #10 - Posted 2002-12-01 01:44:05 »

Yeah that's what I figured! This is my *first* time dealing with Threads, so I'm goofing up in all kinds of ways!

I think the best thing to do is just to have the thread calculate which image to display next, and change that image in the main program rather than dealing with the graphics object.

If I run two of these threads, the screen flickers all over the place!!!

I'm going to try modifying the thread to just change the image and have the paint method in the main program determine when it should refresh itself.
Offline GeorgiaW

Senior Newbie




Java games rock!


« Reply #11 - Posted 2002-12-01 03:46:49 »

Ok, now that I've fixed that little problem... now I have encountered another! Tongue

I removed the paint calls from the Thread. So now the flickering has stopped, but if I have another thread for another NPC running, the screen flickers really badly and alternates between the two characters.

I guess what I'm getting at is how would I animate other objects on the screen? Do I keep track of how many "frames" I have drawn and draw certain pieces at certain times, or do I need these separate threads for each one?

All I want is to be able to animate multiple things on the screen.
Offline markuskidd

Junior Member


Medals: 1



« Reply #12 - Posted 2002-12-01 04:13:46 »

A seperate Thread for each NPC is fundamentally not going to work. The fewer threads you have interacting with your rendering, the better. There are a lot of ways to approach this, some have been suggested earlier. If I may, here's one route that may be simpler for you right now.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
class AnimatedNPC {
     public AnimatedNPC(Image[] images) {
          this.images = images;
          currentFrame = 0;
     }

     public Image getFrame() {
          if(currentFrame == images.length) currentFrame = 0;
          return images[currentFrame++];
     }
     
     private Image[] images;
     private int currentFrame;
}


Then in your paint loop, something like this:
1  
2  
3  
4  
5  
6  
public void paint(Graphics g) {
    .....
    for(int i =0; i < NPC_COUNT; i++) {
         g.drawImage(npc[i].getFrame(), npcPosX[i], npcPosY[i], this);
     }
}


Once you have as many NPCs working as you need and you feel comfortable, improve the NPC class by syncing it with a global timer and provide methods to set the rate at which the current frame is iterated.

Just my suggestion. (coming from someone who makes extensive use of animated gifs Cheesy)
Offline GeorgiaW

Senior Newbie




Java games rock!


« Reply #13 - Posted 2002-12-01 14:04:48 »

Thank you Smiley That sounds a lot like what Captain was saying.

*** UPDATE ***

Just thought I'd come back to edit this and say THANK YOU to everyone that helped Smiley I finally got the idea through my thick skull and I have several little guys on the screen all moving randomly at their own speeds without any flickering Smiley
Offline zainuluk

Senior Newbie




Java games rock!


« Reply #14 - Posted 2002-12-13 17:47:31 »

Hi, well I don't think this well help you but more me. Well I want to design a platform game and was woundering where you got the bitmaps and images from for your game......I want to design a game but am not very good at images drawing so just woundering where I can download some.
ok thanks
Offline cfmdobbie

Senior Member




Who, me?


« Reply #15 - Posted 2002-12-14 01:02:53 »

The most famous free sprite library is SpriteLib GPL by Ari Feldman:

 http://www.arifeldman.com/free/1945.html

But if that doesn't work for you, try these:

 http://www.google.com/search?hl=en&q=free+game+graphics
 http://www.vbexplorer.com/VBExplorer/charpack1.asp
 http://www.alltheweb.com/search?cat=web&lang=english&query=+free+sprites+&phrase=on

Hellomynameis Charlie Dobbie.
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.

theagentd (6 views)
2014-04-24 23:00:44

xsi3rr4x (83 views)
2014-04-15 18:08:23

BurntPizza (75 views)
2014-04-15 03:46:01

UprightPath (86 views)
2014-04-14 17:39:50

UprightPath (69 views)
2014-04-14 17:35:47

Porlus (86 views)
2014-04-14 15:48:38

tom_mai78101 (109 views)
2014-04-10 04:04:31

BurntPizza (169 views)
2014-04-08 23:06:04

tom_mai78101 (265 views)
2014-04-05 13:34:39

trollwarrior1 (217 views)
2014-04-04 12:06:45
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
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!