[CS3] Global Variables [AS2]
A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: [CS3] Global Variables [AS2]

  1. #1
    Member
    Join Date
    Jun 2008
    Posts
    82

    [CS3] Global Variables [AS2]

    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);

  2. #2
    Banned deepakflash's Avatar
    Join Date
    Aug 2007
    Location
    [Object not found]
    Posts
    1,160
    Try this

    In line#4:
    _global.newbase = _global.userbase-1;

    In line#15:
    post_variable.string = _global.checktest;

    So, Whenever you use : _global.something=somevalue, try to refer to this "something" variable using "_global" when you try to read from it.
    Last edited by deepakflash; 12-13-2008 at 02:31 AM.

  3. #3
    Member
    Join Date
    Jun 2008
    Posts
    82
    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.
    Last edited by Teonnyn; 12-13-2008 at 03:43 AM.

  4. #4
    Member
    Join Date
    Jun 2008
    Posts
    82
    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.
    Last edited by Teonnyn; 12-13-2008 at 04:33 AM.

  5. #5
    Member
    Join Date
    Jun 2008
    Posts
    82
    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);

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width


HTML5 Development Center

Poll by Flashkit.com