Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Dec 2006
    Posts
    37
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Animted gifs for java

    Hello,
    I did an animated gif "magic move" that moves like 10steps ahead and when i did it, it calls the gif pefectly, but when it finishes it goes back to it's original spot 10 steps back. Also, how do i fix the problem where i stop in between the animated gif and it starts back at the first part of the animated gif because it seems like when i stop in between the animated gif, then call the gif again, it continues where i left off. Thank you for your assisstance.

  • #2
    Senior Coder
    Join Date
    Apr 2003
    Location
    England
    Posts
    1,192
    Thanks
    5
    Thanked 13 Times in 13 Posts
    There is a Java forum. It is hidden in the "Server Side" section (because JSP is server side.. but Java isn't so I think that forum should just be for JSP specifically)

    It sounds like you are just repeatedly calling the same code again and not starting again from where you left.

    However, it would help if you posted some code.

  • #3
    New Coder
    Join Date
    Dec 2006
    Posts
    37
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
     public void init()
    {
         FighterUltimate = new Image[12];
      FighterUltimate[Fighter1] = getImage(getCodeBase(),"Fighter.gif");
      FighterUltimate[Fighter2] = getImage(getCodeBase(),"FighterUltimate2.gif");
      FighterUltimate[Fighter3] = getImage(getCodeBase(),"Fighter3.gif");
    [...] // 12 pictures of Fighter
    }
     public void paint(Graphics g)
        {
         [...]
     if(selectstage)
     {
          play3(); //battlefield
                     drawFighter();
         keyTemp(); // seeing if keys are true
                 }
     }
    [...]
    public void drawFighter()
     {
      if(FighterSprite == 1)
      {
       gBuffer.drawImage(FighterStanceL,FighterX,FighterY,this);
      }
      if(FighterSprite == 0)
      {
       gBuffer.drawImage(FighterStanceR,FighterX,FighterY,this);
      }
      if(FighterSprite == 2)
      {
    ****************************************
    for(int k = 0; k<12;k++) //trying to animate fighter
       {
    gBuffer.drawImage(FighterUltimate[k],FighterX-50,FighterY-50,this);
        repaint();
        delay(100);
        FighterX++;   
    }
     ******************************************************************************  
      }
     }
     public void keyTemp() //movement of fighter
     {
      if(p2walkL)
      {
       FighterX-=10;
       FighterSprite = 1;
       p2walkL = false;
      }
      if(p2walkR)
      {
       FighterX+=10;
       FighterSprite = 0;
       p2walkR = false;
      }
      repaint();
     }
    public void keyPressed(KeyEvent e) //send true to temp if move is available
     {
      int key = e.getKeyCode();
      if(play && !selectstage &&key==KeyEvent.VK_ENTER)
      {
       selectstage=true;
       FighterSprite = 1;
      }
      if(selectstage)
      {
       if(key == KeyEvent.VK_D)
       {
        p2walkR = true;
       }
       if(key == KeyEvent.VK_A)
       {
        p2walkL = true;
       }
       if(key == KeyEvent.VK_T)
       {
        if(FighterSprite==0)
        {
         FighterSprite = 2;
        }
       }
       repaint();
      }
    I sort of gave you the main keyboarding parts, if something is unclear please ask. Thanks again.
    I mainly need to know how to fix the animation of the gif so i can call each and every picture and erase each and every picture.
    (//trying to animate part) because i got the keyboard thing to work already.
    If i do that trying to animate part, it just draws the every picture.
    Last edited by powerpuff; 05-15-2007 at 01:09 PM.

  • #4
    Senior Coder
    Join Date
    Apr 2003
    Location
    England
    Posts
    1,192
    Thanks
    5
    Thanked 13 Times in 13 Posts
    Sorry for this but it is pretty hard to see what is going on from your code. What does FighterX represent? In KeyTemp you are changing it by 10, but in your animation you are incrementing it.

    I would reccomend creating the classes Sprite, AnimatedSprite, and Fighter.

    Sprite may be an abstract class, in which case non animated sprites could be a further class StaticSprite, or you could do still sprites with Sprite and animated sprites with AnimatedSprite extending Sprite. Fighter will extend AnimatedSprite.

    Sprite will include a "draw" method and maybe an "update" method that can change its positions etc, AnimatedSprite will override this with some animation rather than a simple draw (you could implement Runnable if you want a new thread for this, or just make AnimatedSprite draw the current frame as usual (then a static sprite is simply an animated sprite with only 1 frame) and give it an "update" method that also changes the frame and wraps to the start if the sprite is supposed to when it runs out of frames. Fighter is just an animated sprite that you can control. It will have things like moveLeft() and moveRight() or if you use keydowns and keyups to toggle whether it should be moving, you could just have setLeft(boolean) and setRight(boolean) then your movement calculations go in the update method.

    PS you would probably benefit from keeping your code neater and giving better comments. Anyone reading your code would definately benefit from it

  • #5
    New Coder
    Join Date
    Dec 2006
    Posts
    37
    Thanks
    0
    Thanked 0 Times in 0 Posts
    if i implement runnable would i do it like this?
    Code:
     // there are 12 assigned values
    static final int FighterUltimate1 = 0;
    static final int FighterUltimate2 = 1;
     static final int FighterUltimate3 = 2;
    [...]
    // there are 12 assigned images
    		FighterUltimate[FighterUltimate1] = getImage(getCodeBase(),"FighterUltimate1.gif");
    		FighterUltimate[FighterUltimate2] = getImage(getCodeBase(),"FighterUltimate2.gif");
    		FighterUltimate[FighterUltimate3] = getImage(getCodeBase(),"FighterUltimate3.gif");
    
    public void drawCharacters()
    {
           if(temp && selectstage) // assuming temp and selectstage are true
    	{
    		run();
    	}
    }
    
       public void start () 
       {
        fThread = new Thread (this);
        // Start the thread
        fThread.start();
      } // start
       public void stop () 
       {
        if (fThread != null) 
        {
            fThread = null;
        }
      } // stop
       public void run() 
       {
        while (fThread != null) 
        {
          for (int k=0; k < 12; k++) 
          {
    		gBuffer.drawImage(FighterUltimate[k],FighterX,FighterY,this);
          }
          repaint();
          // Pause
          try {
               Thread.sleep(1000);
          }
          catch (InterruptedException e) {}
         }
      }

  • #6
    New Coder
    Join Date
    Dec 2006
    Posts
    37
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Could someone teach me how to implement the runnable with my 12 sprites in a java static array? Thanks.

  • #7
    Senior Coder
    Join Date
    Apr 2003
    Location
    England
    Posts
    1,192
    Thanks
    5
    Thanked 13 Times in 13 Posts
    Something like this would do it:
    Code:
    public class AnimatedSprite extends Sprite implements Runnable
    {
      private Image[] images;
      private int currentIndex;
    
      public AnimatedSprite(Image[] images)
      {
        this.images = images;
        currentIndex = 0;
      }
    
      public void run()
      {
        if(++currentIndex >= images.length)
          currentIndex = 0;
      }
    
      public void draw()
      {
        // paint images[currentIndex];
      }
    }
    
    ...
    AnimatedSprite as = new AnimatedSprite(images);
    new Thread(as).start();
    while(keepDrawing)
      as.draw();
    You could have put new Thread(this).start(); in the constructor. You never actually call run(). If you call it manually then it will just be executed in the calling thread. Using Thread.start() calls it in its own Thread so it can essentially run at the same time.

    Note that this starts a new thread for each animated sprite though a more efficient solution would just do what I told you to do in the reply to your private message and simply calculate the time delta between draws and work out which frame it should draw based on that. This is generally very efficient and can also be used for movement etc. It also keeps your game running at the same speed for any processor, or if your computer cpu spikes while it runs an antivirus or something. Framerate will drop but the game won't start acting funny. It is essentially going at the same speed but drawing less.

    Also note that threads cannot be relied upon. If you have 10 animated sprites on separate threads and leave them for 10 hours, chances are at least 1 will be out of sync because it has had less processor time or whatever.

    For example if the last draw happened 20 ms ago, you are animating between 9 images at a speed of 3ms per image and you are currently on frame 4 you can say that you should be 20/3 frames ahead since last time. This truncates down to 6. 4+6 = 10. 10 % 9 = 1 (your frames are numbered 0 to 8, therefore if you wrap back to the start you will be on the 2nd frame at index 1 in the frame array)
    Last edited by ghell; 05-23-2007 at 02:11 AM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •