Referencing colour value I defined in the main project again in class after redefining it in class?

Hi! Sorry, newcommer to processing, hoping I don’t mess up the guidelines.

The story so far:
I have a class called “blocks”. The colour, size and position of that box is defined through the main project in its signatur, because I want to be able to change the colour depending on what level.

Each individual block should turn black as I hover over it and stay black (as in invisible because the bg is also black).

But, I also want the game to be endless.
So when I win the game, it should reset with a keypress.

So. If I define the colour as red in main, and then in the blocks class, define that if mouseX and mouseY is over the block, it turns black, I usually just redefined the HEX value the variable “colour”.

But, since the game should be endless, that setting has to revert back to its original colour value once the game restarts.

I can’t just define the colour value again, cause it has to be able to be defined over the main project.

To my question:

So, how do I reference the colour value I defined in the main project again?

Thank you and I hope I explained everything understandably!

Keep the color value and store additionally when the mouse was over the block

boolean wasOver=false;

When mouse is over set it to true

When restart your game set it false for all

  • Use if(wasOver) fill (0); else fill(myColor);

Thank you for the quick answer!
I’ve tried that, but it still won’t reset.

Here is my class code:

class Blocks {
  int Blockwidth;
  int Blockheight;
  color col;
  float xPos;
  float yPos;
  boolean invisible = false;

  Blocks(float xPos, float yPos, int Blockwidth, int Blockheight, color col) {
    this.xPos = xPos;
    this.yPos = yPos;
    this.farbe = col;
    this.hoehe = Blockheight;
    this.Blockwidth = Blockwidth;
  }

  void vanish() {
    if (activated() == true && invisible == false) {
      invisible = true;
    }
    if (invisible==true) {
      fill(0);
    } else {
      fill(col);
    }
    if (activated() == false && invisible == false) {
      fill(col);
    }
    if (end == true) {
      fill(col);
      invisible = false;
    }
    rect(xPos, yPos, Blockwidth, Blockheight);
  }


  boolean activated() {
    if (
      mouseX >= xPos && mouseX <= (xPos+Blockwidth)  &&
      mouseY >= yPos && mouseY <= yPos+Blockheight
      ) {
      return true;
    } else {
      return false;
    }
  }
}

basically, once my mouse is over the block, the block turns black. if the end == true, then everything sets back. But it doesn’t. What am I missing?

Thanks in advance!

These are wrong

Must be col and Blockheight

Can’t see a big error otherwise

(although code can be shortened)

Maybe you don’t set variable end correctly?

When the mouse is on the block it won’t reset obviously

1 Like

Hello @MarkusWiesinger

This post by @jeremydouglass might be of use:

But because we can see only the class code and not the main program I had to kind of imagine the other variables at play.

For example, when you say:

Does endless here mean that there is an accumulation of a score that remains intact that you build upon when you reset?
Or does everything reset to the original default settings?

Also, regarding the boolean end in:

I’m assuming this is declared and initialized in the main program.
But cannot see how/or where in your main code you mark the event that the game is over. This may be key to understanding the issue. :slightly_smiling_face:

Also, I played with your code a bit to understand the behavior you want.
A super simple working version to confirm this is the basic behavior you want to reset the color to the initial red. (?) With a few comments.

//////////////////////////////////////////////////////////////////////////////
int x = 150;
int y = 150;
int w = 100;
int h = 100;

boolean activated = false; //set to false/off before the mouse position is hovering over the rect

boolean invisible = false; //?? no info on how this fits into your main program
boolean end = false; //?? no info on how this fits into your main program 

color col = color(255, 0, 0);
color black = color(0);


void setup() {
  size(400, 400);
  background(0);

  fill(col); // initialize with red
  rect (x, y, w, h);
}

void draw() {


  if (mouseX > x && mouseX < x+w && mouseY > y && mouseY < y+h) {
    activated = true;
  } else {
    activated = false;
  }

  if (activated) {
    fill(black); // change to black after mouse position over rect, stays black until reset(any key is pressed)

    rect (x, y, w, h);
  }
  
  if (keyPressed == true) {
    fill(col);
    rect (x, y, w, h);
  } else {
    fill(0); // need the else statement or reset to red does not work
  }
}

Hopefully this is helpful! :slightly_smiling_face:

:nerd_face:

1 Like