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 › array of buttons in setup
Page Index Toggle Pages: 1
array of buttons in setup (Read 371 times)
array of buttons in setup
Mar 19th, 2010, 3:48am
 
hi guys

i have this array in my setup:
Code:

volumeBar = new button[] {
new button(80,hoogte,"volumeUit.png", "volumeAan.png", 50),
new button(120,hoogte,"volumeUit.png", "volumeAan.png", 50),
new button(160,hoogte,"volumeUit.png", "volumeAan.png", 50),
new button(200,hoogte,"volumeUit.png", "volumeAan.png", 50),
new button(240,hoogte,"volumeUit.png", "volumeAan.png", 50),
new button(280,hoogte,"volumeUit.png", "volumeAan.png", 50),
new button(320,hoogte,"volumeUit.png", "volumeAan.png", 50),
new button(360,hoogte,"volumeUit.png", "volumeAan.png", 50),
new button(400,hoogte,"volumeUit.png", "volumeAan.png", 50),
new button(440,hoogte,"volumeUit.png", "volumeAan.png", 50),
new button(480,hoogte,"volumeUit.png", "volumeAan.png", 50),
new button(520,hoogte,"volumeUit.png", "volumeAan.png", 50),
new button(560,hoogte,"volumeUit.png", "volumeAan.png", 50),
new button(600,hoogte,"volumeUit.png", "volumeAan.png", 50),
new button(640,hoogte,"volumeUit.png", "volumeAan.png", 50),
new button(680,hoogte,"volumeUit.png", "volumeAan.png", 50),
new button(720,hoogte,"volumeUit.png", "volumeAan.png", 50),
new button(760,hoogte,"volumeUit.png", "volumeAan.png", 50),
new button(800,hoogte,"volumeUit.png", "volumeAan.png", 50),
new button(840,hoogte,"volumeUit.png", "volumeAan.png", 50),
new button(880,hoogte,"volumeUit.png", "volumeAan.png", 50),
new button(920,hoogte,"volumeUit.png", "volumeAan.png", 50),
new button(960,hoogte,"volumeUit.png", "volumeAan.png", 50),
};

obviously this is very redundant.

although i can expand arrays with ints, strings,... i'm kind of stuck with this one.

i want it to be something like:
Code:

volumeBar = new button[] {
for(int i=0;i<maxButtons;i++){
new button(80+i,hoogte,"volumeUit.png", "volumeAan.png", 50),
}
}
Re: array of buttons in setup
Reply #1 - Mar 19th, 2010, 4:42am
 
have you tried the following?
Code:

// define an array of buttons, in your case this array has a
// length of 23 buttons

volumeBar = new button[23];

// create a loop which loops from 0 to 22 and fill the
// array with your buttons.
// the value of the first parameter of the button
//constructor will be set according to the value of i.
// (here: 80, 120, etc.)

for(int i=0;i<23;i++) {
 button[i] = new button(80 + (i*40),hoogte,"volumeUit.png", "volumeAan.png", 50);
}


Re: array of buttons in setup
Reply #2 - Mar 19th, 2010, 4:59am
 
thx this worked! (with a slight modification).

somtimes simple solutions lay very far away if you've been staring at the screen all day.

for the people interested in the solution:
Code:
    volumeBar = new button[23];

for(int i=0;i<23;i++) {
volumeBar[i] = new button(80 + (i*40),hoogte,"volumeUit.png", "volumeAan.png", 50);
}

Page Index Toggle Pages: 1