# Hitbox and death counter

**URL:** <https://discourse.processing.org/t/hitbox-and-death-counter/12763>\
**Category:** Coding Questions\
**Created:** [July 17, 2019, 5:39pm UTC](https://discourse.processing.org/t/hitbox-and-death-counter/12763 "2019-07-17T17:39:17Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Prantzz](https://avatars.discourse-cdn.com/v4/letter/p/6a8cbe/32.png) [@Prantzz](https://discourse.processing.org/u/Prantzz)\
**Post date:** [July 17, 2019, 5:39pm UTC](https://discourse.processing.org/t/hitbox-and-death-counter/12763/1 "2019-07-17T17:39:17Z")

</div>

Hey there, i’m new to processing and programming in general, so go easy on me 😃

Well, i was writing this simple game for processing 3, and found myself into some difficulties.  
I Made 2 tabs, one for the main coding, and other for classes (maybe you could give some tips for organization as well 🙂 )

so here’s my code

Main code tab:

```auto
 ball myBall;
 int score;
 int deaths;

void setup() {
  size ( 640, 480);
  noStroke();
  myBall = new ball();
}

void draw() {
  background(126);
  myBall.display();
  fill (255);
  ellipse(mouseX, mouseY, 33, 33);
  fill (0);
  text ("Score: " + score, 10, 460);
  text ("Deaths: " + deaths, 570, 460); 
  
  if (mouseX == myBall.xposb + 33 || mouseY == myBall.yposb + 33 || mouseX == myBall.xposb - 33 || mouseY == myBall.yposb - 33){
    die();
  }
}

void die(){
  deaths++;
  score = 0;
}

```

classes tab:

```auto
class ball {
  color c;
  float xposb;
  float yposb;
  float xspeed;

    ball(){
    c = color (0);
    xposb = width/2;
    yposb = height/2;
    xspeed = 0;
  }
  
  void display(){
   rectMode(CENTER);
   fill(c);
   rect(xposb, yposb, 33, 33);
  }
}

```

so, is there any easiest or clearer way to reference the rect hitbox instead of - ?

```auto
if (mouseX == myBall.xposb + 33 || mouseY == myBall.yposb + 33 || mouseX == myBall.xposb - 33 || mouseY == myBall.yposb - 33){
    die();
  }

```

and when i put the “if” outside the “draw()” function it says “expecting EOF, found if” but when i put it inside “draw()” function, the “die()” function runs every frame, which is very bad cause i don’t want it to happen, since it have to “die” once every time it hit the hitbox

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [July 17, 2019, 6:01pm UTC](https://discourse.processing.org/t/hitbox-and-death-counter/12763/2 "2019-07-17T18:01:29Z")

</div>

> [@Prantzz](#):
>
> any easiest or clearer way to reference the rect hitbox instead of - ?

Make this a boolean function inside the ball class

So you don’t need the ball. part

Also, I think you want && instead of ||

and \>= and \<= instead of ==

---

<div class="post-metadata">

**Author:** ![Prantzz](https://avatars.discourse-cdn.com/v4/letter/p/6a8cbe/32.png) [@Prantzz](https://discourse.processing.org/u/Prantzz)\
**Post date:** [July 17, 2019, 6:36pm UTC](https://discourse.processing.org/t/hitbox-and-death-counter/12763/3 "2019-07-17T18:36:48Z")

</div>

:o Thanks for the help on the hitbox! but still, i didn’t quite get the boolean function inside the class thing - it’s still making a death per frame xD

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [July 17, 2019, 6:54pm UTC](https://discourse.processing.org/t/hitbox-and-death-counter/12763/4 "2019-07-17T18:54:21Z")

</div>

> [@Prantzz](#):
>
> if (mouseX == myBall.xposb + 33 || mouseY == myBall.yposb + 33 || mouseX == myBall.xposb - 33 || mouseY == myBall.yposb - 33){ die(); }

Ah, it’s not a boolean function, when you call `die()` directly.

The function must be inside the ball class, so you don’t have to write `myBall.` everywhere:

from what you’re writing I make the assumption

- that the ball has a diameter of 66 and
- that you use ellipseMode(CENTER);

That’s why I say -33 and +33

Chrisir

```auto

void checkHit() {
  if (mouseX >= xposb-33 &&  
    mouseY >= yposb-33 &&
    mouseX <= xposb+33 &&
    mouseY <= yposb+33 ) {
    die();
  }
}

```

---

<div class="post-metadata">

**Author:** ![Prantzz](https://avatars.discourse-cdn.com/v4/letter/p/6a8cbe/32.png) [@Prantzz](https://discourse.processing.org/u/Prantzz)\
**Post date:** [July 17, 2019, 7:08pm UTC](https://discourse.processing.org/t/hitbox-and-death-counter/12763/5 "2019-07-17T19:08:27Z")

</div>

> [@Chrisir](#):
>
> from what you’re writing I make the assumption
> 
> - that the ball has a diameter of 66 and
> - that you use ellipseMode(CENTER);
> 
> That’s why I say -33 and +33

i couldn’t assume better 😃

right, so how do i not call the `"die()"` function directly, and call it as a boolean function instead (if it is even possible)… in the meantime of your answer i’ll be trying to crack my head here, it’s not fun to have someone writing your code for you hahah 🙂

thanks anyways!

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [July 17, 2019, 7:12pm UTC](https://discourse.processing.org/t/hitbox-and-death-counter/12763/6 "2019-07-17T19:12:09Z")

</div>

> [@Prantzz](#):
>
> how do i not call the `"die()"` function directly, and call it as a boolean function instead

**OKAY**

in draw() write:

```auto
if(myBall.hitCheck()) 
   die();

```

in the class write:

```auto
boolean checkHit() {
  // returns either true or false 
  return (mouseX >= xposb-33 &&  
    mouseY >= yposb-33 &&
    mouseX <= xposb+33 &&
    mouseY <= yposb+33 ) ;  
} // method 

```
