BLARGiAMdEAD
YaBB Newbies
Offline
Posts: 12
Strange NullPointerException Error, Please Help!
Apr 21st , 2009, 4:10am
This is my first program in Processing, BTW.
Code: //Fish by Jonah C. //Initialize fish and rocks Fish fish1; Fish fish2; Fish fish3; Fish biggie; Rock rock1; Rock rock2; Rock small1; Rock small2; Rock small3; Rock big1; Chomp[] bites=new Chomp[1]; int chompCount; void setup() { //set size and background size(1000,400); background(255); //set chomp-array counter to 1 chompCount=1; //create local variables used in initialization float fishStart=50; float rockstartx; float rockstarty; //create random fish fish1=new Fish(fishStart,fishStart,int(random(70,100)),int(random(70,100)),1); fishStart=950; fish2=new Fish(fishStart,fishStart-900,int(random(70,100)),int(random(70,100)),2); fishStart=350; fish3=new Fish(fishStart-300,fishStart,int(random(70,100)),int(random(70,100)),3); fishStart=500; biggie=new Fish(fishStart,fishStart-300,int(random(95,120)),int(random(80,110)),4); //create random rocks rockstartx=int(random(800)); rockstarty=int(random(400)); rock1=new Rock(int(rockstartx),int(rockstarty),int(random(30,50)),int(random(30,50))); rockstartx=int(random(800)); rockstarty=int(random(400)); rock2=new Rock(int(rockstartx),int(rockstarty),int(random(30,50)),int(random(30,50))); rockstartx=int(random(800)); rockstarty=int(random(400)); big1=new Rock(int(rockstartx),int(rockstarty),int(random(95,120)),int(random(95,120))); rockstartx=int(random(800)); rockstarty=int(random(400)); small1=new Rock(int(rockstartx),int(rockstarty),int(random(10,30)),int(random(10,30))); rockstartx=int(random(800)); rockstarty=int(random(400)); small2=new Rock(int(rockstartx),int(rockstarty),int(random(10,30)),int(random(10,30))); rockstartx=int(random(800)); rockstarty=int(random(400)); small3=new Rock(int(rockstartx),int(rockstarty),int(random(10,30)),int(random(10,30))); } //animation loop void draw() { //clear screen background(255); //move and refresh fish fish1.move(); fish1.display(); fish2.move(); fish2.display(); fish3.move(); fish3.display(); biggie.move(); biggie.display(); //re-draw rocks rock1.display(); rock2.display(); big1.display(); small1.display(); small2.display(); small3.display(); } class Fish { //initialize fish parameters int ident; float x; float y; int count=0; float xmov=0; float ymov=0; PShape f; int rota; float rad; int sizex; int sizey; int origsizex; int origsizey; boolean v; //create fish based on input Fish(float tempX, float tempY,int tempsix,int tempsiy,int tempid) { ident=tempid; x = tempX; y = tempY; sizex=tempsix; sizey=tempsiy; origsizex=sizex; origsizey=sizey; v=false; //load starting sprite f=loadShape("fishthing.svg"); smooth(); } void move() { float chase; //change the directions once every 30 frames, 1st, 31st, etc. if (count==0) { xmov=random(-2.5,2.5); ymov=random(-2.5,2.5); v=false; } count=count+1; if (count==30) { count=0; } //grow the fish if it is a baby if(sizex<origsizex) { sizex=sizex+1; } if(sizey<origsizey) { sizey=sizey+1; } //move the fish's location x=x+xmov; y=y+ymov; //check for collision against rocks if(x+sizex<=rock1.sizex+rock1.x || x>=rock1.x && y+sizey<=rock1.sizey+rock1.y || y>=rock1.y) { xmov=-1*xmov; ymov=-1*ymov; } if(x+sizex<=rock2.sizex+rock2.x || x>=rock2.x && y+sizey<=rock2.sizey+rock2.y || y>=rock2.y) { xmov=-1*xmov; ymov=-1*ymov; } if(x+sizex<=small1.sizex+small1.x || x>=small1.x && y+sizey<=small1.sizey+small1.y || y>=small1.y) { xmov=-1*xmov; ymov=-1*ymov; } if(x+sizex<=small2.sizex+small2.x || x>=small2.x && y+sizey<=small2.sizey+small2.y || y>=small2.y) { xmov=-1*xmov; ymov=-1*ymov; } if(x+sizex<=small3.sizex+small3.x || x>=small3.x && y+sizey<=small3.sizey+small3.y || y>=small3.y) { xmov=-1*xmov; ymov=-1*ymov; } if(x+sizex<=big1.sizex+big1.x || x>=big1.x && y+sizey<=big1.sizey+big1.y || y>=big1.y) { xmov=-1*xmov; ymov=-1*ymov; } //check for fish within an area, and chase/run away if(ident!=1) { if(x+sizex<=fish1.sizex+fish1.x+25 || x>=fish1.x-25 && y+sizey<=fish1.sizey+fish1.y+25 || y>=fish1.y-25) { if(sizex*sizey>fish1.sizex*fish1.sizey+30) { chase=random(.5,1.5); xmov=chase*fish1.xmov; ymov=chase*fish1.xmov; } } if(sizex*sizey<fish1.sizex*fish1.sizey-30) { if(x>fish1.x && xmov<0 || x<fish1.x && xmov>0) { xmov=-1.2*xmov; ymov=-1.2*ymov; } else { xmov=xmov*1.2; ymov=ymov*1.2; } } } if(ident!=2) { if(x+sizex<=fish2.sizex+fish2.x+25 || x>=fish2.x-25 && y+sizey<=fish2.sizey+fish2.y+25 || y>=fish2.y-25) { if(sizex*sizey>fish2.sizex*fish2.sizey+30) { chase=random(.5,1.5); xmov=chase*fish2.xmov; ymov=chase*fish2.xmov; } if(sizex*sizey<fish2.sizex*fish2.sizey-30) { if(x>fish2.x && xmov<0 || x<fish2.x && xmov>0) { xmov=-1.2*xmov; ymov=-1.2*ymov; } else { xmov=xmov*1.2; ymov=ymov*1.2; } } } } if(ident!=3) { if(x+sizex<=fish3.sizex+fish3.x+25 || x>=fish3.x-25 && y+sizey<=fish3.sizey+fish3.y+25 || y>=fish3.y-25) { if(sizex*sizey>fish3.sizex*fish3.sizey+30) { chase=random(.5,1.5); xmov=chase*fish3.xmov; ymov=chase*fish3.xmov; } if(sizex*sizey<fish3.sizex*fish3.sizey-30) { if(x>fish3.x && xmov<0 || x<fish3.x && xmov>0) { xmov=-1.2*xmov; ymov=-1.2*ymov; } else { xmov=xmov*1.2; ymov=ymov*1.2; } } } } if(ident!=2) { if(x+sizex<=biggie.sizex+biggie.x+25 || x>=biggie.x-25 && y+sizey<=biggie.sizey+biggie.y+25 |