Hit test not working counter not increasing
in
Programming Questions
•
11 months ago
- //images
- PImage obamaPic;
- PImage romneyPic;
- PImage boothPic;
- PImage backdrop;
- //ballots
- Ballot[] obama = new Ballot[12];
- Ballot[] romney= new Ballot [8];
- Booth booth;
- //counters
- int i;
- int obamacount;
- boolean obamaAlreadyHit;
- int dem;
- int gop;
- void setup(){
- size(650,700);
- obamaPic = loadImage("DEM.png");
- romneyPic = loadImage("GOP.png");
- backdrop = loadImage("wall2.png");
- boothPic = loadImage("booth.png");
- dem=0;
- gop=0;
- for (int i=0;i<obama.length;i++){
- obama[i] = new Ballot(obamaPic,random(650),-33);
- }
- for (int i=0;i<romney.length;i++){
- romney[i] = new Ballot(romneyPic,random(650),-33 );
- }
- booth = new Booth(boothPic,325,375);
- }
- void draw(){
- if(obamaAlreadyHit == true){ // CHECK IF OBAMA WAS HIT
- dem++;
- }
- image(backdrop,0,0,650,700);
- fill(255);
- noStroke();
- rect(370,260,170,50);
- fill(255,0,0);
- text("Obama"+dem+"Romney"+gop, 390, 290);
- for (int i=0;i<obama.length;i++){
- obama[i].fall();
- obama[i].display();
- }
- for (int i=0;i<romney.length;i++){
- romney[i].fall();
- romney[i].display();
- }
- booth.display();
- }
- void keyPressed(){
- if(keyCode==LEFT){
- booth.moveX(-1);
- } else if (keyCode==RIGHT){
- booth.moveX(1);
- }
- }
- class Booth {
- float xpos, ypos, speed, w, h;
- boolean obamaHit;
- boolean romneyHit;
- boolean romneyAlreadyHit;
- PImage p;
- Booth(PImage tempP,float tempX, float tempY){
- xpos = tempX;
- ypos = tempY;
- p =tempP;
- speed = 10;
- w = 134;
- h = 322;
- }
- void obamaHit(Ballot obama){
- if ((xpos+32 >= obama.xpos && xpos+32 <= obama.xpos + 32 || xpos >= obama.xpos && xpos <= obama.xpos + 32) && (ypos+h >=obama.ypos && ypos+h <= obama.ypos + 66 || ypos >= obama.ypos && ypos <= obama.ypos + 66)||(obamaAlreadyHit==false)){
- obamaAlreadyHit= true;
- } else {
- obamaAlreadyHit = false;
- }
- }
- void romneyHit(Ballot romney){
- if ((xpos+32 >= romney.xpos && xpos+32 <= romney.xpos + 32 || xpos >= romney.xpos && xpos <= romney.xpos + 32) && (ypos+h >=romney.ypos && ypos+h <= romney.ypos + 66 || ypos >= romney.ypos && ypos <= romney.ypos + 66)||(romneyAlreadyHit==false)){
- romneyAlreadyHit= true;
- gop++;
- } else {
- romneyAlreadyHit = false;
- }
- }
- void moveX(float xstep){
- xpos += xstep * speed;
- }
- void display(){
- image(p,xpos, ypos, w, h);
- }
- }
1