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 & HelpSyntax Questions › Is this a mistake in the book
Page Index Toggle Pages: 1
Is this a mistake in the book? (Read 492 times)
Is this a mistake in the book?
Mar 26th, 2009, 3:25pm
 
The script is from page 316.
numFrames is 12, in the end it checks
if (frame == numFrames) {
If that is true then it's set to 0.
But the movie contain 12 images, and from 0 to 12 is 13 which would mean that the last frame (011.gif) is showed twice.
Is that correct?

int numFrames = 12;
int frame = 0;
PImage[] images = new PImage[numFrames];

void setup(){
 size(100, 100);
 frameRate(30);
 images[0] = loadImage("ani-000.gif");
 images[1] = loadImage("ani-001.gif");
 images[2] = loadImage("ani-002.gif");
 images[3] = loadImage("ani-003.gif");
 images[4] = loadImage("ani-004.gif");
 images[5] = loadImage("ani-005.gif");
 images[6] = loadImage("ani-006.gif");
 images[7] = loadImage("ani-007.gif");
 images[8] = loadImage("ani-008.gif");
 images[9] = loadImage("ani-009.gif");
 images[10] = loadImage("ani-010.gif");
 images[11] = loadImage("ani-011.gif");
}

void draw() {
 frame++;
 if (frame == numFrames) {
   frame = 0;
 }
 image(images[frame], 0, 0);
}

Re: Is this a mistake in the book?
Reply #1 - Mar 26th, 2009, 3:34pm
 
It's ok for me, in a full cycle you have images from 1 to 11 then 0, which display 12 images.
Everthing look ok except that in the first frame this is image 1 and not 0 that is displayed... which you can fix by initializing frame to -1.
Re: Is this a mistake in the book?
Reply #2 - Mar 26th, 2009, 4:12pm
 
Aah, this zero-based arithmetic is tripping lot of people! To think some people complaint against one-based indexes (like they are in Lua, for example)... Smiley I have no problem with either, having used Basic and C for a long time...

The script is correct, the only "issue" is that it starts by displaying frame 1 instead of frame 0, although it is probably unnoticeable (and might be fixed by putting the image() call on top of draw() instead of on bottom.

Let see how it acts:
- increment frame
- if frame is 12, set it to 0
- display image number 'frame'

So the last line will only see frame numbers 0 to 11.
Page Index Toggle Pages: 1