# Is there a way to stop the register method?

**URL:** https://discourse.processing.org/t/is-there-a-way-to-stop-the-register-method/30107
**Category:** Coding Questions
**Created:** [May 15, 2021, 11:48am UTC](https://discourse.processing.org/t/is-there-a-way-to-stop-the-register-method/30107 "2021-05-15T11:48:31Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Flolo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/flolo/32/13874_2.png) [@Flolo](https://discourse.processing.org/u/Flolo)
#### Post date: [May 15, 2021, 11:48am UTC](https://discourse.processing.org/t/is-there-a-way-to-stop-the-register-method/30107/1 "2021-05-15T11:48:31Z")

</div>

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 🙂

> **Click to see the Example Code**
>
> ```auto
> //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

---

<div class="post-metadata">

### Author: ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)
#### Post date: [May 15, 2021, 12:50pm UTC](https://discourse.processing.org/t/is-there-a-way-to-stop-the-register-method/30107/2 "2021-05-15T12:50:23Z")

</div>

Try `unregisterMethod(..)`
