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 & HelpPrograms › ArrayIndexOutOfBounds with array of objects
Page Index Toggle Pages: 1
ArrayIndexOutOfBounds with array of objects (Read 1296 times)
ArrayIndexOutOfBounds with array of objects
Mar 15th, 2010, 3:44pm
 
I'm sorry if this is a silly question, but I'm very very new to Processing and I certainly need all the help I can get.

I've written the following program, which is supposed to be the basis for a simple game I will be making, but I keep getting the "ArrayIndexOutOfBounds" error. The code is pasted below:

Code:
int x = 0;
int y = 0;

Entity[] entities = new Entity[0];

int numEntities = 0;

void setup() {
 size(800, 640);
 // Init all controller keys to "not pressed"
 for (int i = 0; i< 4; i++) {
   keys[i] = false;
 }
}

void draw() {
 background(255);
 move();
 drawEntities();
 rect(x, y, 50, 50);
}



void move() {
 if(keys[W] == true) {  //Move the left player up.
   y = max(0, y-6);
   //y--;
 }
 if(keys[A] == true) {  //Move the left player down.
   x = max(0, x-6);
   //x--;
  }
 if(keys[S] == true) {  //Move the right player up.
   y = min(640, y+6+50)-50;
   //y++;
 }
 if(keys[D] == true) {  //Move the right player down.
   x = min(800, x+6+50)-50;
   //x++;
 }
}

void mousePressed() {
 append(entities, new Entity(mouseX, mouseY));
 numEntities++;
 println(numEntities);
}

void drawEntities() {
 for (int i = 0; i < numEntities; i++) {
   println(i);
   rect(entities[i].getX(), entities[i].getY(), 50, 50);
 }
}

class Entity {
int x;
int y;

Entity (int _x, int _y) {
int x = _x;
int y = _y;
}

int getX() {
return x;
}
int getY() {
return y;
}
}

boolean[] keys = new boolean[4];
final int W = 0;
final int A = 1;
final int S = 2;
final int D = 3;

void keyPressed() {
if(key == 'w') {
keys[W] = true;
} else if(key == 'a') {
keys[A] = true;
} else if(key == 's') {
keys[S] = true;
} else if (key == 'd') {
keys[D] = true;
}
}

void keyReleased() {
if(key == 'w') {
keys[W] = false;
} else if(key == 'a') {
keys[A] = false;
} else if(key == 's') {
keys[S] = false;
} else if (key == 'd') {
keys[D] = false;
}
}


I tried to piece together what I could from examples I found on the forum, but to no avail.

Thanks in advance for your help!
Re: ArrayIndexOutOfBounds with array of objects
Reply #1 - Mar 15th, 2010, 3:51pm
 
Entity[] entities = new Entity[0];

Means your array has a size of zero......so it can't hold any elements. I'm surprised this even runs, as you also start doing things with the keys array before you have even declared it!!!
Re: ArrayIndexOutOfBounds with array of objects
Reply #2 - Mar 15th, 2010, 3:57pm
 
Actually, there are three different sections, in three different "Tabs."

There is the Entity Class tab, the keyMonitor tab (which has all of the stuf fwith keys), and the main game tab.

I'm not sure how "tabs" work in processing, though.

EDIT: Besides, that "for" loop won't even be called until something is in the array...
Re: ArrayIndexOutOfBounds with array of objects
Reply #3 - Mar 15th, 2010, 5:43pm
 
Looks good, a couple things that will make your life easier, though:
when you want an expanding array, try an ArrayList or Vector.  I tend to use Vectors.  They're a class with a few new syntactical rules:

Vector myVec = new Vector();
myVec.add(new Entity());
for (int i=0; i<myVec.size(); i++){
 ((Entity)myVec.get(i)).update();
 ((Entity)myVec.get(i)).variable += 10;
}

and your keypress if/else chains could be handled slightly more quickly with a switch(){ statement:

switch(key){
 case('d'):
 keys[D] = true;
 break;
 case('w'):
 keys[W] = true;
 break;
}
Re: ArrayIndexOutOfBounds with array of objects
Reply #4 - Mar 15th, 2010, 6:40pm
 
Whoa, thank you BenHem and Giles!

I figured out how to get it to work thanks to you guys.

Thanks for showing me how to write better code in Processing!
Re: ArrayIndexOutOfBounds with array of objects
Reply #5 - Mar 16th, 2010, 1:49am
 
Jonathan C wrote on Mar 15th, 2010, 3:57pm:
Actually, there are three different sections, in three different "Tabs."

There is the Entity Class tab, the keyMonitor tab (which has all of the stuf fwith keys), and the main game tab.

I'm not sure how "tabs" work in processing, though.


Unless your tab has a .java extension tabs don't do anything and the code behaves as if it were all in a single tab.  They're just a convenient way of splitting up code; for example by putting a class in its own tab.

One thing to watch out for is that a typo or missing brace on one tab can sometimes be reported as an error on another tab; and people have experienced problems when removing (and possibly renaming) tabs (though this is usually resolved by closing and re-opening the sketch).


Re: ArrayIndexOutOfBounds with array of objects
Reply #6 - Mar 16th, 2010, 6:52am
 
I answered in the duplicate thread (which I deleted...).

Quote:
append(entities, new Entity(mouseX, mouseY));

is wrong: append doesn't alter entities but returns a new array: you must assign it back to entities.
Or just use an ArrayList. In preference to Vectors, which are obsolete.
Page Index Toggle Pages: 1