Boolean in parameters

On the void dead(boolean hit) I get the yellow message saying how the value of the parameter hit is not being used. I’ve realized that putting hit in a conditional statement such as in void show does not bring up this message. What I’m wondering is why I can’t set the parameter hit=true? Is this not possible and/or is there a way to do what I want a different way? I’m assuming that I can’t set hit to true because hit is already predefined as ded which is false when I call the method.

boolean ded=false;

void setup()
{
  size(800,600);
  background(0);
}

void draw()
{
  show(ded);
  dead(ded);
}

void show(boolean hit)
{
  if(hit==false)
    rect(width/2,height/2,50,50);
}

void dead(boolean hit)
{
  if(key=='a')
    hit=true;
}
1 Like
  • Function parameters are also local variables.
  • Therefore, they cease to exist once its function returns.
  • Using the assignment operator = directly on them is only visible inside its function.
  • If you need to retain the value assigned and/or need to access those values everywhere, declare a “global” (a.K.a. field) variable instead.
  • Or simply make a function which returns a value.
boolean ded;

void setup() {
  size(300, 200);
}

void draw() {
  ded = dead();
  getSurface().setTitle("Dead: " + ded);
}

boolean dead() {
  return keyPressed && key == 'a';
}
1 Like

So I understand that parameters are local variables and I understand that they can’t be accessed everywhere but I can’t figure out a way to use the boolean dead() method you wrote with drawing and undrawing multiple shapes. For example, if I want to have the show and dead functions that I wrote called twice with different parameters to have 2 different shapes, how would I implement the way you are making the ded boolean true or false. I used my old code to show kind of what I mean because I don’t know how to work yours into it. (Also, in the future I would add more parameters to the dead method to specify which rectangle should show because right now both would undraw if a is clicked)

boolean ded=false;
boolean ded2=false;

void setup()
{
  size(800,600);
  background(0);
}

void draw()
{
  show(ded,200,200);
  dead(ded);
  show(ded2,500,500);
  dead(ded2);
}

void show(boolean hit, int x, int y)
{
  if(hit==false)
    rect(x,y,50,50);
}

void dead(boolean hit)
{
  if(key=='a')
    hit=true;
}

You’re better off creating a class to represent each of your “shapes”:

  1. From several variables to arrays - Processing 2.x and 3.x Forum
  2. From several arrays to classes - Processing 2.x and 3.x Forum
  3. Objects / Processing.org