Class Problems

A bit new to classes, how do I make this work? I have a global variable x1

class Button {
  Button(float x1, float y1, float x2, float y2, int fromscene, int toScene) {
   x1 = this.x1
 }
 void createButton() {
   if (mouseX >= x1 && mouseY >= y1 && mouseX <= x2 && mouseY <= y2 && mousePressed) {
      if (fromscene == scene) {
            scene = toScene;
      }
    }
  }
}

So the error I get is: There is no global variable x1

Tagged:

Answers

  • edited June 2016

    You should post enough code that the sketch can be run and that we can see the global variable x1. However, just looking at this section of code I can see that your Button constructor has a x1 variable argument which is probably taking priority over the global one (guessing because code is missing)

  • edited June 2016

    Ok, update.

    The global variable isnt a problem anymore

    Button.createButton();
    

    I put that down, the class

    class Button {
      int x1, x2, y1, y2;
      int from, to;
      Button(int x1, int y1, int x2, int y2, int from, int to) {
        x1 = this.x1;
        x2 = this.x2;
        y1 = this.y1;
        y2 = this.y2;
        from = this.from;
        to = this.to;
      }
      void createButton() {
        if (mouseX >= x1 && mouseY >= y1 && mouseX <= x2 && mouseY <= y2 && mousePressed) {
          if(scene == from) {
            scene = to;
          }
        }
      }
    }
    

    And that doesnt work with the error Cannot make a static reference to a non-static method createButton() from the type Click.Button

  • Below I changed Button enough that it does not have errors:

    class Button {
      // x1 and y1 are variables that belong to Button
      float x1, y1;
    
      Button(float x1, float y1, float x2, float y2, int fromscene, int toScene) {
        // This is setting the Button x1 equal to the argument x1
        // this.x1 refers to Button's x1 because an argument is also called x1
        this.x1 = x1;
      }
      void createButton() {
        // Here x1 and y1 refer to Button's x1 and y1
        if (mouseX >= x1 && mouseY >= y1) {
        }
      }
    }
    

    Your Button class needs x1 and y1 variables because the createButton function is expecting them. Also you have x1 = this.x1 backwards, it is supposed to be this.x1 = x1

  • But i want the global variable to change

  • edited June 2016

    I changed your code to match what I want, and it didnt work

    class Button {
    
      float x1, y1;
      float x2, y2;
      int from, to;
      Button(float x1, float y1, float x2, float y2, int from, int to) {
        // This is setting the Button x1 equal to the argument x1
        // this.x1 refers to Button's x1 because an argument is also called x1
        x1 = this.x1;
        x2 = this.x2;
        y1 = this.y1;
        y2 = this.y2;
        from = this.from;
        to = this.to;
      }
      void createButton() {
    
        if (mouseX >= x1 && mouseY >= y1 && mouseX <= x2 && mouseY <= y2 && scene == from) {
          println("hi");
          scene = to;
        }
      }
    }
    
  • same error

  • edited June 2016

    You need to post your complete code with an explanation of expected behavior in that case, I don't know why you would want Button's constructor to change a global variable. I suspect that you don't realize what it means to do that, it would make sense for a constructor to change a class' variable

    Please format your code this time like you did in the first post

  • edited June 2016

    ok, not getting an error anymore but the button isnt working

    class Button {
    
      float x1, y1;
      float x2, y2;
      int from, to;
      Button(float x1, float y1, float x2, float y2, int from, int to) {
        // This is setting the Button x1 equal to the argument x1
        // this.x1 refers to Button's x1 because an argument is also called x1
        x1 = this.x1;
        x2 = this.x2;
        y1 = this.y1;
        y2 = this.y2;
        from = this.from;
        to = this.to;
      }
      void createButton() {
    
        if (mouseX >= x1 && mouseY >= y1 && mouseX <= x2 && mouseY <= y2 && scene == from && mousePressed) {
          println("hi");
          scene = to;
        } else if(!mousePressed) {
    
        }
      }
    }
    
  • It doesnt all show with

    <

    pre>

  • edited June 2016

    Rest:

    void createButton() {
    
      if (mouseX >= x1 && mouseY >= y1 && mouseX <= x2 && mouseY <= y2 && scene == from && mousePressed) {
        println("hi");
        scene = to;
        } else if(!mousePressed) {
    
        }
      }
    }
    
  • edited June 2016

    When I say complete code I mean including setup(), draw(), and anything else that might be relevant. What you keep posting is a part of a class that ends in the middle of an if statement (Edit: I had not realized some of SlimeEnergy's code was missing due to pre tags when I wrote this)

    Edit: Below is a complete sketch that can be run:

    void setup() {
      size(100, 100);
    
      // Create a Button instance
      Button b = new Button(0, 0, 0, 0, 0, 0);
    
      // Call that instance's createButton function
      b.createButton();
    }
    
    void draw() {
    }
    
    class Button {
      float x1, y1;
      float x2, y2;
      int from, to;
    
      Button(float x1, float y1, float x2, float y2, int from, int to) {
        // Note, this.x1 / this.x2 / etc. are on the left
        this.x1   = x1;
        this.x2   = x2;
        this.y1   = y1;
        this.y2   = y2;
        this.from = from;
        this.to   = to;
      }
    
      void createButton() {
        // Not a very meaningful function but your code ends here
        if (mouseX >= x1 && mouseY >= y1) {
          println("Hey, x1 = " + x1 + ", y1 = " + y1);
        }
      }
    }
    
  • Ok, so I want to define the variables for where the mouse has to be in the class. Then I want to use it in the .createButton thing to make the button

  • edited June 2016

    Perhaps you should read a bit more on how classes are used typically. It does make sense for the Button variables like x1, y1, etc. to be a part of the Button class, but putting the createButton function inside the Button class does not make a lot of sense. The easiest thing to do is to create the Buttons in setup by calling a Button constructor

    Below I make three Button instances of different dimensions, have a bit of code to display them, and they change color when the mouse is inside of them. The actual Button creation is done first thing when the sketch runs in setup

    Button a, b, c;
    
    void setup() {
      size(400, 400);
    
      // Create the Button instances here
      a = new Button(20, 20, 180, 180);
      b = new Button(220, 20, 380, 40);
      c = new Button(280, 320, 320, 360);
    }
    
    void draw() {
      background(255);
      a.display();
      b.display();
      c.display();
    }
    
    class Button {
      int x1, y1, x2, y2;
    
      Button(int x1, int y1, int x2, int y2) {
        this.x1 = x1;
        this.y1 = y1;
        this.x2 = x2;
        this.y2 = y2;
      }
    
      // This is so we can see where a Button instance is
      void display() {
        // Change the color if the mouse is inside the Button instance
        if (mouseX >= x1 && mouseY >= y1 && mouseX < x2 && mouseY < y2)
          fill(255, 0, 0);
        else
          fill(255);
        rect(x1, y1, x2 - x1, y2 - y1);
      }
    }
    

    If you have questions let me know. On a side note, I am a moderator so I was able to click to edit your posts and saw that you were trying to post more code but it did not show here. The easiest way to post code is to click auto format in Processing, copy all the code to a text box here, highlight all the code (select it), and then click on the button above the text box that looks like the letter 'C'. That will format your code for the forum without losing code

  • [Mod edits: changed all the pre formatting to ctrl-o formatting, some previously hidden code now visible - pre doesn't like less than signs so try not to use it]

  • edited June 2016

    Wait. I dont wanna see where it is. I want to detect if there is a click in it Without global variables now.

  • edited June 2016

    Oh, it works now. What I did was I removed EVERYTHING from `Button {

    }`

    And made it this ``class Button {

    Button() { }

    void work(float x1, float y1, float x2, float y2, int from, int to) { if (mouseX >= x1 && mouseY >= y1 && mouseX <= x2 && mouseY <= y2 && scene == from && mousePressed) { println("hi"); scene = to; } else if (!mousePressed) { //nothing } } } ``

  • edit post, highlight code, press ctrl-o

Sign In or Register to comment.