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 › noob - problem with array
Page Index Toggle Pages: 1
noob - problem with array (Read 359 times)
noob - problem with array
Aug 2nd, 2008, 3:28am
 
I'm having a problem figuring out how to make an array:

---------------
int h = 400;
int w = 400;

void setup() {
 size ( h,w);
 background ( 250, 250, 205);
 stroke( 100, 203, 12 );
}

/*
float[] points = new float[10];  
for ( int i = 0; i < 10; i++ ) {
  points[i] = i * 3;
}
*/
int i;
 
void draw() {
 for ( i = 0; i < 10; i++ ) {
//  ellipse(points[i], 104, 4, 4);      
   ellipse( i * 9, 55, 23, 43); // testing
 }
}
----

I added the single ellipse line to make sure that I was actually getting something in the draw loop.

Take out the commented sections, which are supposed to create an array, and then draw some ellipses based on that array.

I'm using 135, and I don't get any errors, but I don't get any ellipses either! What am I doing wrong?
Re: noob - problem with array
Reply #1 - Aug 2nd, 2008, 3:35am
 
Try putting the array section in either the setup function or the draw loop.

As far as I know, if you use a setup and draw loop you can't use code that is outside of the loops (except variable declarations etc.)
Re: noob - problem with array
Reply #2 - Aug 2nd, 2008, 4:44am
 
CongoZombie wrote on Aug 2nd, 2008, 3:35am:
Try putting the array section in either the setup function or the draw loop.

As far as I know, if you use a setup and draw loop you can't use code that is outside of the loops (except variable declarations etc.)


Okay, I try this:
---------------------------------
int h = 400;
int w = 400;

void setup() {
 size ( h,w);
 background ( 250, 250, 205);
 stroke( 100, 203, 12 );
 
 float[] points = new float[10];  
 for ( int i = 0; i < 10; i++ ) {
   points[i] = i * 3;
 }

}
 
void draw() {
 
 for ( i = 0; i < 10; i++ ) {
//  ellipse(points[i], 104, 4, 4);  
   ellipse( i * 9, 55, 23, 43); // testing
 }
}
------------------------------

And I get the error
Semantic Error: No accessible field named "i" was found in type "Temporary_856_5281".

I heard somewhere along the line that anything created in setup is not available later on. That seems to be true, since I set i in the setup, and the error gets thrown in the draw() loop.

So, I apparently can't create an array in setup() that I would want to use in draw(), because it gets destroyed. However, I don't want to create it in draw(), because I'm looping, and I only want to create the array once, and I *do* eventually want to use the looping feature of draw().

So the only logical place to put the creation of the array would be outside of both setup() and loop(), AFAICT.



Re: noob - problem with array
Reply #3 - Aug 2nd, 2008, 10:38am
 
Code:

int h = 400;
int w = 400;
float[] points;

void setup() {
 size ( h,w);
 background ( 250, 250, 205);
 stroke( 100, 203, 12 );
 
 points = new float[10];  
 for ( int i = 0; i < 10; i++ ) {
   points[i] = i * 3;
 }

}
 
void draw() {
 
 for ( int i = 0; i < 10; i++ ) {
  ellipse(points[i], 104, 4, 4);  
//    ellipse( i * 9, 55, 23, 43); // testing
 }
}
Re: noob - problem with array
Reply #4 - Aug 2nd, 2008, 4:42pm
 
Thanks, John G!

Now, I'm having problems with the 2D array.

------
 int iterations = 100;
 
 points = new float[iterations + 1][2];
-------

Gives the error
emantic Error: The type of the right sub-expression, "float[][]", is not assignable to the variable, of type "float[]".
Re: noob - problem with array
Reply #5 - Aug 2nd, 2008, 7:22pm
 
points is defined as only a single dimensional array.. so you need to change the definition to float[][] points;
Page Index Toggle Pages: 1