We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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
Answers
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)
Ok, update.
The global variable isnt a problem anymore
I put that down, the class
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:
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
I changed your code to match what I want, and it didnt work
same error
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
ok, not getting an error anymore but the button isnt working
It doesnt all show with
<
pre>
Rest:
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:
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
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
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]
Wait. I dont wanna see where it is. I want to detect if there is a click in it Without global variables now.
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