I'm trying to figure out problems with global variables and functions. My script has a set of variables it calls on to check if a user has typed in a username, or if they have anything typed in at all. It then compares the username to the database (not done yet). However, I've noticed that for some reason, the variables do not work outside the functions they are declared in, and global variables refuse to work as well. The only way the script works is if the variables are non-global, and inside the "checker" function, but I need to reference some of them outside the function.
Code:
stop();
_global.checktest = username_test.text;
_global.userbase = checktest.length;
_global.newbase = userbase-1;
numberofcards = 0;
cardnumber.text = numberofcards;
/// "Check" Button, linked to Username_test.text ///
checker.onRelease = function() {
if (_global.newbase == 0) {
errortxt.text = "Please enter your username.";
} else {
/// Check if the username entered is in the database ///
var php_process:LoadVars = new LoadVars();
var post_variable:LoadVars = new LoadVars();
post_variable.string = checktest;
post_variable.sendAndLoad("control.php",php_process,"POST");
trace(_global.newbase);
}
php_process.onLoad = function(success:Boolean) {
if (success) {
errortxt.text = php_process.result;
} else {
errortxt.text = "Error connecting to server.";
}
/// End Check Code ///
trace(newbase);
};
};
/// The Next Button ///
pageonenext.onRelease = function() {
if (_global.newbase == 0) {
errortxt.text = "I'm sorry, please enter your Username.";
} else {
gotoAndStop("pagetwo");
trace(_global.checktest);
}
};
function theTimer() {
errortxt.text = "";
}
var testing = setInterval(theTimer, 9000);
Bit confused.. how would altering line fifteen help? That's unrelated to the actual areas I need to get working. I'll try the other variables though.. forgot those.
Fixed line #15... realized what was wrong there after a second glance, and added a lot of _global. to other variables that needed it. Still not getting anything though.. this is odd.
Fixed the code in several areas so that most of the variables are now declared as _global, but I'm not getting anything. In fact it seems that unless I declare the variables as normal, it refuses to admit they even exist.
Code:
stop();
numberofcards = 0;
_global.checktest = username_test.text;
_global.userbase = _global.checktest.length;
_global.newbase = _global.userbase-1;
cardnumber.text = numberofcards;
/// "Check" Button, linked to Username_test.text ///
checker.onRelease = function() {
if (_global.newbase == 0) {
errortxt.text = "Please enter your username.";
} else {
/// Check if the username entered is in the database ///
var php_process:LoadVars = new LoadVars();
var post_variable:LoadVars = new LoadVars();
post_variable.string = _global.checktest;
post_variable.sendAndLoad("control.php",php_process,"POST");
trace(_global.newbase);
}
php_process.onLoad = function(success:Boolean) {
if (success) {
errortxt.text = php_process.result;
} else {
errortxt.text = "Error connecting to server.";
}
/// End Check Code ///
trace(_global.newbase);
};
};
/// The Next Button ///
pageonenext.onRelease = function() {
if (_global.newbase == 0) {
errortxt.text = "I'm sorry, please enter your Username.";
} else {
gotoAndStop("pagetwo");
trace(_global.newbase);
}
};
function theTimer() {
errortxt.text = "";
}
var testing = setInterval(theTimer, 9000);
Small note: Newbase is used to determine if they enter anything at all, while checktest reads the text directly and uses it as the original string. In theory, checktest ought to be working properly. The "userbase" variable is just to convert it to a number using .length.
One more big change to the code. The only way I seem to be able to get the important set of variables to work is to redeclare them inside the second function. It refuses to listen to them otherwise, even IF the first set of variables is made _global. Is there any way to smooth this code out?
Code:
stop();
numberofcards = 0;
cardnumber.text = numberofcards;
/// "Check" Button, linked to Username_test.text ///
checker.onRelease = function() {
checktest = username_test.text;
userbase = checktest.length;
newbase = userbase-1;
if (newbase == 0) {
errortxt.text = "Please enter your username.";
} else {
/// Check if the username entered is in the database ///
var php_process:LoadVars = new LoadVars();
var post_variable:LoadVars = new LoadVars();
post_variable.string = checktest;
post_variable.sendAndLoad("control.php",php_process,"POST");
trace(newbase);
}
php_process.onLoad = function(success:Boolean) {
if (success) {
errortxt.text = php_process.result;
} else {
errortxt.text = "Error connecting to server.";
}
/// End Check Code ///
trace(newbase);
};
};
/// The Next Button ///
pageonenext.onRelease = function() {
checktest = username_test.text;
userbase = checktest.length;
newbase = userbase-1;
if (newbase == 0) {
errortxt.text = "I'm sorry, please enter your Username.";
} else {
gotoAndStop("pagetwo");
trace(newbase);
}
};
function theTimer() {
errortxt.text = "";
}
var testing = setInterval(theTimer, 9000);