View Full Version : Working With Variables
MOOGLEFIGHTER
12-11-2007, 05:52 PM
Alright, so here's what I want to do. There will be an Input Text Box on frame one, and it's variable username. Also on the first frame there is a button which goes to the next frame when pressed. When you press the button, I want the frame to go to a specific frame when I have a specific text in the input text box. Lets say I type in "cake" as my username, and press the button, I want it to go to frame 3.
How would I go about doing this?
I know it's very simple, but I'm a total newbie to AS.
_root.your_button.onRelease=function(){
//change your_button to the buttons instance name
//check if it's being pressed and released
if(username=="cake"||username=="Cake"){
//if so...check if the username variable is cake or Cake
_root.gotoAndStop(3)
//if so go to the 3rd frame
}
}
There you go.
MOOGLEFIGHTER
12-11-2007, 06:11 PM
1000 internets to you, Matt.
magcius
12-11-2007, 06:18 PM
This is what is called an if...then...else construct.
You also need an Enter Frame loop.
Let's say you want to check against username against "cake".
We define an if test to check username against "cake"
if ( username == "cake" ) {
gotoAndStop ( 3 );
}
Let's go over operators first:
OPERATOR : Action
== : Is equal
!= : Is not equal
< : Is less than
<= : Is less than or equal to
> : Is greater than
>= : Is greater than or equal to
These are not all the operators, but they are the most common/
If you insert this code as-is on the timeline, it won't work. Why? Because this code only checked against it at the start of the movie. It is not humanly possible to quickly insert "cake" into the text box before the start of the movie (unless you have super speed or are able to slow down time).
To check every single frame, we need a loop that will help us. What flash does when it starts up is looks for functions that have special names. One of these functions is named "onEnterFrame" and it allows us to create a function that happens every frame.
So, put this all together, and we have:
function onEnterFrame ( ) {
if ( username == "cake" ) {
gotoAndStop ( 3 );
}
}
That's it!
Lawl, just had to do the full out explanation. 1000 internets to you for giving more of a damn than me!
MOOGLEFIGHTER
12-11-2007, 06:41 PM
I'm having trouble getting the code to work. I've tried messing around with it, but I haven't made any progress.
Would anything work if I tried to directly apply a code on the button itself?
There shouldn't be any need to mess around with the code.... >_<
It's laid out exactly how you wanted it.
Ah well ,you don't learn from copy-paste. Probably a good thing you messed with it but not good that you couldn't get it to work until after messing.
MOOGLEFIGHTER
12-11-2007, 07:06 PM
Okay. So I pasted the script in the first frame, and did what you told me to do. I opened up the flash. Typed "Cake" in the text box. Pressed the button. Nothing happened.
I triple checked the text box's variable, which happens to be username, and the button's instance name, which happens to be your_button.
Nothing is apparently working.
magcius
12-11-2007, 07:49 PM
You need to type cake, not Cake.
If you want it to be case-insensitive, change the if to this:
if ( username.toLowerCase ( ) == "cake" ) {
Case matters.
Also, is there a third frame even?
MOOGLEFIGHTER
12-11-2007, 08:01 PM
-Yes, there is a third frame.
-I used "Cake" in the function and typed in Cake.
arkhan
12-12-2007, 03:14 AM
its because username means nothing at all unless you say it does so.
if username is the name of your textField, then you should be checking for username.text, coz calling username directly will only return its type, whereas username.text returns the input value of the field.
magcius
12-12-2007, 08:28 AM
I thought he filled in the variable field, not the instance name field.
MOOGLEFIGHTER
12-12-2007, 03:52 PM
Thanks to arkhan, it's now working.
I'll hand it to Matt, who gave me the original code. Magcius for explaining it detail by detail. And Arkhan for providing a fix up.
It's now working as I imagined it should. Thanks guys!
truimagz
12-12-2007, 08:48 PM
I read your post, and here is what I thought of as reading it.
I would create an array, something like var pageArray:Array = new Array ()
Then to make it easier to read and add users later I would manually input the usernames something like a list
pageArray[0] = "unavailable"
pageArray[1] = "cake"
pageArray[2] = "donut"
pageArray[3] = "pie"
now I have my passwords in an array and labeled
then I would create a look up function for the array, so that if a user types a password, it just calls this function each time.
function checkUser (Input:String) {
for (var i:Number = 1; i < pageArray.length; i++) {
if (pageArray[i] == Input {
_root.gotoAndStop(i)
i = pageArray.length
}
}
};
now you would need to put some basic error checking on this setup, but you could do something with a movie clip placed with a response if the username is invalid
but then on your button you just put _root.checkUser (usernameTextFieldVar)
Also if you went about it this way, then later you could set up a simple mysql to allow users to create a username and password, and then check the database against there input. You just push the mysql into pageArray
just my two cents
arkhan
12-13-2007, 03:09 AM
if you are going to check at an sql theres no need to check at all..just having the name and frame at the database would be enough..sql checks for equality for itself, and its hell faster than flash.
truimagz
12-13-2007, 05:32 AM
yes this is true
vBulletin® v3.7.3, Copyright ©2000-2010, Jelsoft Enterprises Ltd.