2dsquid
YaBB Newbies
Offline
Posts: 3
No idea what could be the problem!!
Sep 15th , 2008, 4:40am
I am fairly new at processing and I have run into a error that I cannot figure out. I have done some java before, and I don't think that this error would have happened in pure java. By the way the code is a zombie/human simulation mod that is basically copied off: http://zombies.insertdisc.com/zombies.java by Kevan Davis. I am just recoding it as an exercise and since I have run into this error my code has been getting closer to Davis's because I am trying to ween out the bug. Here is my code: //Containers Being [] beings; //Controlling Vars int numZombies = 1; int numHumans = 6000; int totalBeings; color zombie = color(0,255,0); color human = color(255,0,0); color panicHuman = color(255,0,255); color wall = color(0,0,0); color nothing = color(1,1,1); void setup() { size(200,200); background(0); frameRate(30); totalBeings = numZombies + numHumans; beings = new Being[totalBeings]; //Create Zombies for(int i = 0; i<numZombies; i++) { beings[i] = new Being(true); //True = isZombie } //Create Humans for(int i = numZombies; i<totalBeings; i++) { beings[i] = new Being(false); } } void draw() { background(0); //println(frameRate); for(int i = 0;i<totalBeings;i++) { beings[i].exist(); } } color look(int x, int y, int dir, int d) { int xpos = x; int ypos = y; for(int i = 0; i < d; i++) { if(dir==1) ypos--; if(dir==2) xpos++; if(dir==3) ypos++; if(dir==4) xpos--; if(xpos>width-1 || xpos<1 || ypos>height-1 || ypos<1) return wall; else if(get(xpos,ypos) == human) return human; else if(get(xpos,ypos) == panicHuman) return panicHuman; else if(get(xpos,ypos) == zombie) return zombie; } return nothing; } class Being { //State boolean isZombie; int x,y; int dir; //1 = up, 2 = right, 3 = down, 4 = left int panic = 0; //Controlling vars color zombie = color(0,255,0); color human = color(255,0,0); color panicHuman = color(255,0,255); color wall = color(0,0,0); color nothing = color(1,1,1); int panicTurns = 5; int mass = 5; int distance = 10; Being(boolean isZombie) { this.isZombie = isZombie; x = (int)random(width)+1; y = (int)random(height)+1; dir = (int)random(4) + 1; } //The main loop for the zombie/human. void exist() { checkState(); updatePosition(); apply(); } //This function updates the being's state vars depending on if the being is a zombie or a human void checkState() { if(isZombie) { fill(zombie); stroke(zombie); } else if(panic > 0) { fill(panicHuman); stroke(panicHuman); } else { fill(human); stroke(human); } } void updatePosition() { int r = (int)random(10); if((!isZombie && panic>0) || r < 3) { //update position if(look(x,y,dir,1)==nothing) { if(dir==1) y--; else if(dir==2) x++; else if(dir==3) y++; else if(dir==4) x--; } else { dir = (int)random(4)+1; } if(panic>0) panic--; } int target = look(x,y,dir,distance); if(isZombie) { //Look around! if(target == zombie || target == wall || (target == nothing && (int)random(5) == 1)) { dir = (int)random(4)+1; } int ix = x, iy = y; if(dir==1) iy--; if(dir==2) ix++; if(dir==3) iy++; if(dir==4) ix--; if(target == panicHuman || target == human) println("i see human!"); //Infect something?! if(look(x,y,dir,1) == human) { //This line triggers the error for(int i=0; i<totalBeings; i++) { beings[i].infect(ix,iy); } } } else { //Is I human?! //Oh Noes! Look for zombies! if(target == zombie) panic = panicTurns; else if(target == wall || target == human) dir = (int)random(4)+1; else if(target == nothing && (int)random(8) == 1) dir = (int)random(4)+1; //Run away! if(target == zombie) { dir += 2; if(dir>4) dir -= 4; } } } void infect(int ix, int iy) { if(x == ix && y == iy) isZombie = true; } void apply() { //ellipse(x,y,5,5); point(x,y); } } The error isn't a syntax error or a error processing tells me... Its just that whenever the if statement that has the error comment (in the updatePosition() function) is uncommented (as it is now), the zombies dont even see humans... However when it is commented they see humans and infect them. I am completely lost as to why, because it seems to me that this is just a simple if statement. Thanks in advance for help!!