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 › Creating Variable Names
Page Index Toggle Pages: 1
Creating Variable Names (Read 305 times)
Creating Variable Names
Feb 19th, 2008, 1:05pm
 
Hello!

I have a simple problem in calling the right variables.
My code looks similar to this:

array a[];
array b[];

if keyPressed = a { draw(a[] };
if keyPressed = b { draw(b[] };

void draw ( String choice) {
if ( choice.length <= 10 ) { ...

I know the code is completely wrong, but i hope you understand the problem. I simply want to tell the draw method the right array to work with.

Anyone knows help?

Greets

Re: Creating Variable Names
Reply #1 - Feb 19th, 2008, 2:07pm
 
Here's a sketch off the top of my head, hope it helps.

Essentially, you have to check if and which key is being pressed. This way you can divert the program to either use one or the other array.


Quote:


// create the arrays
int[] big = new int[2];
int[] small = new int[2];

void setup() {
 size(200,200);
 background(0);
 noFill();
 stroke(255);
 rectMode(CENTER);

 // fill the arrays
 big[0] = 100;
 big[1] = 100;  
 small[0] = 10;
 small[1] = 10;

}

void draw() {
 background(0);

 if (keyPressed) {

   // if 'a' key is pressed
   if (key == 'a') {
     // draw using the first array
     rect(height/2,width/2,big[0],big[1]);
   }

   // if 'b' key is pressed
   if (key == 'b') {
     // draw using the second array
     rect(height/2,width/2,small[0],small[1]);
   }
 }
}

Re: Creating Variable Names
Reply #2 - Feb 19th, 2008, 2:23pm
 
Thaks for you reply!

I understand the code and will do it like this.
I thought there's a version with less code to pass the array name to a function which will use it to draw.

Greets
Re: Creating Variable Names
Reply #3 - Feb 19th, 2008, 2:54pm
 
Sorry, I totally neglected that in my last sketch.

Check this out. It's does exactly the same as the previous sketch, but this time using the function call. In the function you have to define what value is being passed. So, in our case it's an int array.

Quote:


// create the arrays
int[] big = new int[2];  
int[] small = new int[2];  

void setup() {  
 size(200,200);  
 background(0);  
 noFill();  
 stroke(255);  
 rectMode(CENTER);  

 // fill the arrays
 big[0] = 100;  
 big[1] = 100;    
 small[0] = 10;  
 small[1] = 10;  
}  

void draw() {  
 background(0);  

 if (keyPressed) {  

   // if 'a' key is pressed
   if (key == 'a') {  
      //call drawing function and pass the first array
      drawarray(big);
   }  

   // if 'b' key is pressed
   if (key == 'b') {  
     //call drawing function and pass the second array
      drawarray(small);
   }  
 }  
}  

// draw whatever you want, using the passed array
void drawarray(int choice[]) {
rect(height/2,width/2,choice[0],choice[1]);
}

Re: Creating Variable Names
Reply #4 - Feb 20th, 2008, 6:46pm
 
Thanks for your help, everything works fine Wink

Greets
Page Index Toggle Pages: 1