A
major programming concept and method you will use is known
as the If/Else statement. Collectively, an If / Else
statement enables you to ask the computer a Yes or No or
True or False question and trigger a reaction depending upon
the answer the computer provides. The Else statements use
the following structure:
If (condition)
{ statement
} else { statement2
}
The condition is what you are
telling the interpreter, Flash, to check to see if it is
true or false. If the condition is true, the code under
statement will be executed. If the condition is false, the
code under statement2 would be executed. You will understand
more about this as you complete this tutorial.
In the following example,
type kirupa in the input box and press the circle:
[ Type some
other name and press the circle again. ]
Create a
password check utility by using If/Else statements:
To expedite the learning process, I have provided the
partial source file with the interface and minor coding
automatically done for you. Don't worry, the minor coding
does not go beyond stop and button actions. You will have
to add the If/Then statements yourself.
Download the partial source
code by clicking here.
Once you have downloaded the partial code, make sure you
have it displayed in Flash. Right click on Frame 10 of the
actions layer and select Actions. The Frame Actions window
will appear. Press Ctrl + E to enable Expert Mode. Copy
and paste the following line of code:
After you have pasted the code, close the Frame Actions
window and preview the animation by pressing Ctrl + Enter.
Enter kirupa and press the circle to see the message
"Access granted!". Enter any other name, and you will see
the message "Access denied!".
Why the Animation Worked Because you copied and pasted the code, I
will explain why the code worked so you can replicate the
if/else technique in your own animations. I will explain
each line of the code:
password = inputName
When you click on the input
box in Frame 1, notice the Variable field under Text
Options. In the Variable field, you will see that I named
the input box "inputName". So,
in the first line of code, I'm making a variable that I call
password equal the contents of
the input box. Setting the contents of the input box into
the variable takes care of storing the name entered by the
visitor.
The second line of code is
the condition statement. You use the condition statement to
introduce the remaining lines of code. You are basically
telling Flash, if the password entered by the visitor is
kirupa, perform what is listed in the third line of code. If
the password entered is not kirupa, perform what is listed
in the fifth line of code.
What is inputStatus you say?
Well, inputStatus is the dynamic text field on Frame 10 that
displays the results from the code. Because dynamic text
fields can display data AND support naming by variables, it
would be the preferred choice for displaying the data. I
didn't use Static text because you cannot give static text a
variable name. I didn't use the Input Text (Input box)
selection because the user is not inputting any data. The
following image displays the text displayed in the Text
Options tab:
Instant Replay: Animation
Walkthrough When you enter the
word kirupa and press the circle button, you are taken to
Frame 10. Frame 10 contains the actions that invoke the
if/else script. Because you entered kirupa, the condition
statement (password == kirupa)
is true. Therefore, Flash knows to execute the code in the
third line (which is the statement:
inputStatus = "Access granted!").
Let's say there is someone
out there who misspelled my name (heaven forbid!). Because
the name entered does not match the value in the condition
statement (kirupa), Flash will go to the 5th line of code
which is: inputStatus = "Access
denied!"
The code you learned could be
used for any method in which you need to ensure that only
certain visitors have access to the site. While I made the
statements display data in a dynamic text field, you can use
Go To statements to take the visitor to a certain frame for
the correct or incorrect password, etc. The possibilities
are infinite!
Of course, in classic
kirupa.com tradition, the final source code has been
provided so you can see compare your FLA to that of mine. Click Here to Download the Source Code.
I hope the information helped.
If you have any questions or comments, please don't hesitate to post them on the
kirupa.com Forums. Just post your question and I, or our friendly
forum helpers, will help answer it.
The following is a list of related tutorial and help resources that you may find
useful: