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.
Page Index Toggle Pages: 1
Array problem (Read 586 times)
Array problem
Apr 19th, 2009, 6:31am
 
Hi there, I have a problem here which i am sure is quite simple to some of you.The basic premise of the problem is this : I have 5 colors in an array, when mouse is pressed it will draw a rect with color[0], the next click will draw a rect with color[1] etc.
I'm not so familiar to arrays and am unsure on the exact syntax to push through the array and move onto the next value when needed.
I apologize if this is posted somewhere else on the forum but I have unable to find it. I'd really appreciate any help.
Re: Array problem
Reply #1 - Apr 19th, 2009, 9:18am
 
Code:

for (int i = 0; i < array.length; i++) {
val = array[i]
}
Re: Array problem
Reply #2 - Apr 19th, 2009, 3:18pm
 
actually to modify the index of the array which is called every time the mouse is pressed you should declare an int variable which increases every time the mouse is pressed.

this way [i will only write the portions of code which are relevant for you to get the big picture; this example will not work if you don't declare the variables x, y, sizex and sizey and the color array along with its values for each index]:

int currentIndex;

void setup()
{
 currentIndex = 0;
}

void draw()
{
// you can use a color[] to fill a rect calling and employing the
// currentIndex variable:
fill(color[currentIndex]);
//and draw the rect
rect(x, y, sizex, sizey);
}

void mousePressed() // this is what increments the currentIndex
// everytime the mouse is pressed
{
 currentIndex ++;
if(currentIndex > 4) // you could use if(currentIndex > color.lenght)
{
currentIndex = 0;
}
}


Re: Array problem
Reply #3 - Apr 20th, 2009, 1:13pm
 
That works perfectly Pedro. Thank you!
Page Index Toggle Pages: 1