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 › variable variables
Page Index Toggle Pages: 1
variable variables (Read 789 times)
variable variables
Nov 9th, 2005, 6:02pm
 
Hi

if I have a String which concatenates a name and a number in every draw() like so...

String v_currentimage = "currentgrab" + v_currentimagenumber;

How do I convert the String to be the name of another variable type?  How does...

String v_currentimage;
become
PImage currentgrab1;
if v_currentimagenumber was 1?

thanks in advance
gar
Re: variable variables
Reply #1 - Nov 9th, 2005, 10:56pm
 
It would make more sense to use an array.
Code:

PImage [] buffer;
int myImage = 0;
void setup(){
size(200,200);
buffer = new PImage[10];
for(int i = 0; i < buffer.length; i++){
buffer[i] = new PImage((i + 1) * 20,(i + 1) * 20);
rect(int(random(width)),int(random(height)),10,10);
loadPixels();
buffer[i].copy(g,0,0,width,height,0,0,buffer[i].width,buffer[i].height);
}
framerate(12);
}
void draw(){
background(255);
image(buffer[myImage],0,0);
myImage = (myImage+1)%buffer.length;
}

What you're suggesting is common place in Flash as elements in a Flash movie tend not to have the capacity to be broken into arrays.
Re: variable variables
Reply #2 - Nov 11th, 2005, 11:16pm
 
st33d wrote on Nov 9th, 2005, 10:56pm:
What you're suggesting is common place in Flash as elements in a Flash movie tend not to have the capacity to be broken into arrays.


right, had a feeling an array might be a solution but had no idea how the images would be placed in an array as i need details about the images such as their size.  the image array you create PImage [] buffer solves that for me because they are the actual images in the array - again completely foreign concept for me everthing I've done this in the past, php, director, cgi i would have converted the string with number to a single variable referencing the image.  thanks am going to try and see if i can get this to work in my app.

a+
gar
Re: variable variables
Reply #3 - Nov 14th, 2005, 6:53pm
 
Got your array solution working, this was actually a parrallel image rotation attempt to the other posting you were responding to here http://processing.org/discourse/yabb_beta/YaBB.cgi?board=Syntax;action=display;num=1131471481 so that solves both of those for me

thanks for your help, below is the code for anyone else who wants to use it.


public class rotateimage extends PApplet
{
 //initialise variables
 int v_width, v_height, v_screenwidth, v_screenheight;
 float v_increment = 0.00;
 PImage [] buffer;
 
 //setup function
 void setup()
 {
   //set stage size
   v_screenwidth = screen.width;
   v_screenheight = screen.height;
   size(v_screenwidth, v_screenheight, P3D);
   
   //set stage background
   background(0, 0, 0);

   //width and height stored as a variable
   v_width = 318; v_height = 256;
   
   //create an array with images
   buffer = new PImage[360];
   
   //load image
   PImage loadimage = loadImage("confused-black-dog.jpg");
   
   //centre all image placements
   translate(width / 2, height / 2);
   
   //loop through the buffer array
   for(int i = 0; i < buffer.length; i++)
   {
     //put an image in the buffer array
     buffer[i] = new PImage(v_width, v_height);
     
     //copy image in memory into image in buffer array
     loadPixels();
     buffer[i].copy(loadimage, 0, 0, loadimage.width, loadimage.height, 0, 0, v_width, v_height);
     buffer[i].updatePixels();  
 
     //display the image
     image(buffer[i], 0, 0);
     
     //rotate the image
     rotate(v_increment);

     //rotate this amount on next loop
     v_increment = 0.01754;
   }
 }  

}
Page Index Toggle Pages: 1