Animations and Threads

Hi,
i don’t know if I choose the right board, if not a moderator should move it to the right one.

back to topic:
I am doing a small applet-game.
The problem is, that I don’t know how I have to handle animations.
In my gameclass I draw the gamefield and all the other related stuff that is needed. But now I want to have a simple animation for a playchip that should jumping up and down on the gamefield while he is selected.

Have I to use an additional Thread, or how can i do this?

Evil

if you have your animation in an image array (ia) , this could be a solution:

float aniFrequency=1;

paintPlayChip(){
if(focused){
long time=System.getCurrentTimeMillis();
ia[((int)(ia.lengthaniFrequencytime/1000f))%as.lenght].draw();
}
else ia[0].draw();
}

(didn’t test it, but the idea works!):slight_smile:

no i havent it in an array : /
It is just an image. And want to apply new x/y values each animation frame

the game class


[...]
public void render(Graphics G) {
     // drawing stuff will be drawn here
}

// mouse stuff
public void mouseClicked(MouseEvent e) {
 // call players actions, which handles movement, animation etc
}
[...]

the spot aka playerchip


[...]
puplic void animation() {}
[...]

I could assign the x/y values to a seperate array. But then there is still one problem left.
I encountered that i have to call my draw functions inside my render function. Any time i draw something on the backbuffer, it will only be shown up once. Does i need a second back buffer that saves the new painting and then copy it to my first one, so the new drawing would be saved. Or is this thought wrong?

I recommend Kev’s space invader tutorial
Enough u need to know about timing and gameloops

http://www.cokeandcode.com/info/tut2d-2.html

What you want your game loop to be like is something like this:

  1. For each iteration of the game loop

  2. Advance your game state (this is where you process the animation of your playerchip thingy as well as other aspects of your game)

  3. Render your game at the current state (ie, clear and then draw everything onto a backbuffer, then blit the backbuffer onto the screen)

  4. Handle timing

  5. Go to step 1.

If your animation of the playerchip image is static (ie, no change to the pixels in your image) and consists of only the image moving up and down, then just simply draw the image at different x/y values of the screen each iteration of the loop.

Thanks for the tutorial link, but i have a question.


public class SpriteStore {
      /** The single instance of this class */
      private static SpriteStore single = new SpriteStore();
      
      /**
       * Get the single instance of this class 
       * 
       * @return The single instance of this class
       */
      public static SpriteStore get() {
            return single;
      }
      /** The cached sprite map, from reference to sprite instance */
      private HashMap sprites = new HashMap();
[...]
}

I don’t know how this class is handled in the background.
I know that I don’t have to create an object for calling static methods, but the static method returns an object of itself. Does this class remain the whole time in memory once created? I mention so, else the hashmap wouldn’t make any sense to me.
And how determine Java that there allready an instance of this class exists, cause it is called several times while loading the sprites into the application.

The class is initialized during construction (or what it’s called before everything else goes on) and stays in memory for the remainder of your applications life time.

If you call SpriteStore.get() you always get the same instance of the class because it’s static and the spritestore is static too.

So you just call away. You will get the same SpriteStore :slight_smile:

Actually the static member variable would get intialised the first time the class is loaded, note that this might not be at initialisation.

The SpriteStore is just following a singleton pattern, it might be worth looking at singletons generally.

Kev

ah ok. Anyway i´ve done it my own way. But good to know that there is only one object in memory

[quote]Actually the static member variable would get intialised the first time the class is loaded, note that this might not be at initialisation.
[/quote]
Ahh yeah. That was the word I was looking for :slight_smile: