We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I am trying to make a login screen in which there are 2 windows and you enter a username and password and when you click enter it will switch to a second screen. I have gotten this far:
//declare global variables
boolean slides[] = {true, false, false, false, false, false, false, false, false, false, false};
String topText = "";
String bottomText = "";
String wasText = "";
String bottomWasText = "";
boolean username = true;
void setup() {
size(600, 600);
}
//Function to draw (every procedure is called here)
void draw() {
loginMenu();
tableOfContents();
}
//Function(Procedure) controls Login Menu.
void loginMenu() {
if (slides[0] == true) {
background(255);
rectMode(CENTER);
fill(255);
rect(300, 200, 300, 100);
rect(300, 400, 300, 100);
fill(0);
textSize(20);
text(topText, 200, 215);
text(bottomText, 200, 375);
text(wasText, 200, 215);
}
}
//allows keyboard input for various things
void keyPressed() {
if (slides[0] == true) {
if ((keyCode == BACKSPACE && topText.length() > 0))
{
if (username == true) {
topText = topText.substring(0, topText.length()-1);
}
}
if ((keyCode == BACKSPACE && bottomText.length() > 0) ) {
if (username == false) {
bottomText = bottomText.substring(0, bottomText.length()-1);
}
}
else if (keyCode == ENTER)
{
username = false;
wasText = topText;
bottomWasText = bottomText;
if (wasText == "username" && bottomWasText == "password") {
slides[0] = false;
slides[1] = true;
}
} else if ((key >= 32 && key <= 126))//add char to string
{
if (username == true) {
topText = topText + key;
} else {
bottomText = bottomText +key;
}
}
}
}
void tableOfContents() {
if (slides[1] == true) {
background(255);
fill(0);
rect(250, 250, 30, 30);
}
}
Why won't it switch to the tableOfContents screen when you type in "username" and "password".
Answers
Anybody know how to fix this problem. I am really struggling to fix it. Any help would be appreciated. Thanks.
Your code is a bit hard to maintain. You will need to create functions that execute specific tasks, or you should consider using objects (oop) as this will be hard to maintain. On a side note, there are libraries out there that can provide you GUI functionality out of the box. If you are doing this as an exercise, consider the changes below (Notice: partial code). There were two bugs. One was related to selecting the password field right after the username field. As soon as it changes to collect the password, your code was evaluating for user+password correctness. The second bug is related to how you were testing this. There is a difference between
Instead, do this (https://processing.org/reference/String_equals_.html):
I hope this helps,
Kf
Thanks man your a genius