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 › Name of variable
Page Index Toggle Pages: 1
Name of variable (Read 488 times)
Name of variable
Oct 11th, 2007, 8:33pm
 
I'm new to processing, but have a lot of experience with actionscript.
Now I want to load some images into my processing file.
Normaly you would do something like this:

Code:
PImage img1;
img1 = loadImage("file1.jpg");
image(img1, 0, 0);


But I need to load in a bunch of files and I would like to use a for loop to do this.
In actionscript, I would do something like this (the 'for' part):
Code:
for (var i=1; i<=amount; i++){
PImage this["img"+i];
this["img"+i] = loadImage("file"+i+".jpg");
image(this["img"+i, 0, 0);}


Can I make variables with 'combined' names in processing?
( like
Code:
int a = 1;
String ["img"+a] = "foo";
print (img1);//outputs "foo";
Re: Name of variable
Reply #1 - Oct 11th, 2007, 10:39pm
 
Short answer: no.

Long answer: No, but you have arrays, which fulfil the same role.

e.g.
Code:
PImage[] images=new PImage[5];
for(int i=0;i<5;i++)
{
images[i]=loadImage("file"+i+".jpg");
}
Re: Name of variable
Reply #2 - Oct 11th, 2007, 10:58pm
 
Thank you for the answer

Can you maybe tell me what
Code:
PImage[] images=new PImage[5]; 

means? Does the [] behind PImage means its an array of PImage objects? (so; images is an array of 6 PImage objects?)
Re: Name of variable
Reply #3 - Oct 12th, 2007, 12:24am
 
Heedless wrote on Oct 11th, 2007, 10:58pm:
Does the [] behind PImage means its an array of PImage objects (so; images is an array of 6 PImage objects)


yes, but it's an array of 5 PImage objects. where images[0] is the first, and images[4] is the last object in the array.
http://processing.org/reference/arrayaccess.html
Re: Name of variable
Reply #4 - Oct 12th, 2007, 12:27am
 
Okay, I thought 0 through 5

Thank you, this was most helpful.
It takes some time to get used to the processing syntax
Page Index Toggle Pages: 1