We are about to switch to a new forum software. Until then we have removed the registration on this forum.
float[] s = new float[width];
float sx = 20;
for (int i = 0; i < width; i+= sx) {
s[i] = i;
}
for (int i = 0; i < s.length; i++) {
ellipse(s[i],20,20,20);
}
this will work will show 5 circles
but then why if i put that inside draw() or setup() it give an ArrayIndexOutofBoundsException:0
float[] s = new float[width];
void setup(){
}
void draw(){
float sx = 20;
for (int i = 0; i < width; i+= sx) {
s[i] = i;
}
for (int i = 0; i < s.length; i++) {
ellipse(s[i],20,20,20);
}
}
ArrayIndexOutofBoundsException:0
Answers
http://forum.processing.org/two/discussion/8045/how-to-format-code-and-text
float[] s = new float[width];
, s[] is an array of length = width = 0! @-)width
is not known untilsetup()
is run, unless there is nosetup()
then the pre processor will run a basic defaultsetup()
behind the scene settingwidth
to 100. Thus your first code worksWhen you add
setup()
and don't specify asize()
it will setwidth
to 100 as well, but it can't be accessed beforesetup()
, soThat line before setup will fail to properly initialize the array, hence the "OutOfBounds"
You can split declaration and initialization to make this work, like:
ops... late :)
welp,thats a no brainer have not thought about it thanks