We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Variable animation speed with constant framerate
Page Index Toggle Pages: 1
Variable animation speed with constant framerate (Read 850 times)
Variable animation speed with constant framerate
Apr 20th, 2007, 5:28pm
 
Hello,
I'm about to start my first little processing project but got stuck now. I want to create a little application with a sprite-based animation and some user interface elements. The problem is, that I want the user to control the speed of the sprite animation, or even let him pause it, without changing the framerate. If I'd change the framerate directly, I'd also change the speed of button behaviour, which is not what I want. Are there other options for timing the sprite animation / appearance of the next frame? Do I need to use threads?
(I used the example code for sprites and image-based buttons from the Learning page.)

Thanks in advance for you help,

vupac
Re: Variable animation speed with constant framera
Reply #1 - Apr 20th, 2007, 7:08pm
 
You just need to create a class for your sprite that stores the number of milliseconds until it shoudl change to the next fream, and then only changes it if the number of milliseconds elapsed is equal or greater than that, and then change to the next frame.
Re: Variable animation speed with constant framera
Reply #2 - Apr 20th, 2007, 8:06pm
 
Sometimes the answer ist just too simple... thanks for your help and sorry for my stupid question. Smiley
Re: Variable animation speed with constant framera
Reply #3 - May 9th, 2007, 9:47pm
 
I am trying to do this, borrowing the Animated Sprite example. I tried to add a delay to slow it down, but it instead leaves blank spots in the animation.

Here is the code I'm using:

class AniSprite
{
 PImage[] ani;
 int frame;
 int numFrames;

 AniSprite(String imageName, int frameCount) {
   numFrames = frameCount;
   ani = new PImage[numFrames];
   loadImages(imageName);
 }

 void loadImages(String name) {
   for(int i=0; i<numFrames; i++) {
     String imageName = name + ((i < 10) ? "0" : "") + i + ".gif";
     ani[i] = loadImage(imageName);
   }
 }

 void display(float xpos, float ypos)
 {
   frame = (frame+1)%numFrames;
   int del = 100;
   if(lastMillis==0 || millis() > (lastMillis+del))
   {
     image(ani[frame], xpos, ypos);
     lastMillis= millis();
   }
 }


 int getWidth() {
   return ani[0].width;
 }

}
Re: Variable animation speed with constant framera
Reply #4 - May 9th, 2007, 10:01pm
 
I think I've seen the problem, though it's hard to tell without the rest of the code/images.

Code:
   void display(float xpos, float ypos)
{
// frame = (frame+1)%numFrames;
//you're increasing the frame even if it's not time to do so..
int del = 100;
if(lastMillis==0 || millis() > (lastMillis+del))
{
image(ani[frame], xpos, ypos);
lastMillis= millis();
//put it here
frame = (frame+1)%numFrames;
}
//you need to draw the previous one if it's not update time
else
{
image(ani[frame], xpos, ypos);
}
}

Re: Variable animation speed with constant framera
Reply #5 - May 9th, 2007, 10:11pm
 
beautiful! thanks.
Page Index Toggle Pages: 1