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!