Is there a way to stop the register method?

Hey it’s me again
i wanted to ask how to remove the register method.

Because it now calls for every object created in the background and I don’t want that. Here is a small example that I made.
The function destroy () in the MouseFunctions class is supposed to destroy or switch off the register method or something like that.
I hope you understand what I mean
Thanks
Flolo :slight_smile:

Click to see the Example Code
//Main Sketch
ArrayList<Button> buttons;//List of buttons

void setup() {
  rectMode(CENTER);
  textAlign(CENTER, CENTER);

  size(600, 600);

  buttons = new ArrayList<Button>();

  //create Button and add him to the list
  Button b = new Button("test", new PVector(width/2, height/2), new PVector(200, 170));
  buttons.add(b);
}
void draw() {
  background(0);
  //draw all Buttons
  for (Button b : buttons) {
    b.show();
  }
}
void keyPressed() {
  if (buttons.size() <= 0) return;
  //If button size is > than 0

  //Remove the reister method from the Button so that the mousePRessed function is not called anymore
  buttons.get(0).destroy();

  buttons.remove(0);//Remove the button from the list
}
//Button class
//"Button class extends MouseFuntions" means that all 
//the functions in mouseFunction are in the button class as well
//(in short)

class Button extends MouseFunctions {
  PVector pos, size;
  String name;
  Button(String name, PVector pos, PVector size) {
    this.pos = pos.copy();
    this.size = size.copy();
    this.name = name;
  }
  void show() {
    noFill();
    stroke(255);
    strokeWeight(2);
    rect(pos.x, pos.y, size.x, size.y);
    textSize(30);
    fill(255);
    text(name, pos.x, pos.y, size.x, size.y);
  }
  //You can ignore @Override
  @Override 
    void mousePressed() {
    println("mousePressed " + name + " " + frameCount);
  }
}
//MouseFunctions class
public class MouseFunctions {
  MouseFunctions() {
    registerMethod("mouseEvent", this);
  }
  void destroy(){
    //destroys the registermethod or something like that 
  }
  void mouseEvent(final MouseEvent evt) {
    switch(evt.getAction()) {
    case MouseEvent.PRESS:
      mousePressed();
      break;
    case MouseEvent.RELEASE:
      mouseReleased();
      break;
    case MouseEvent.MOVE:
      mouseMoved();
      break;
    case MouseEvent.DRAG:
      mouseDragged();
      break;
    }
  }
  void mousePressed() {
  }
  void mouseReleased() {
  }
  void mouseMoved() {
  }
  void mouseDragged() {
  }
}

The Programm prints this out if I press the mouse then a key and then a button again:

mousePressed test 49
//here it has been removed from the list
mousePressed test 186
//and it still prints something

Try unregisterMethod(..)

3 Likes