ActionScript Explained
Let's take a closer look into what makes this work.
You might be surprised at the other uses you find for these
concepts in the future, even outside of the world of finding and
selecting.
-
finder.onRelease
= function()
{
Here we're making a function that's
called upon every time someone lets go of the left mouse
button after they click on it.
-
Selection.setFocus("_root.textInstance");
In this line we're setting the
selection, which is what is currently being interacted
with, to the text field we'll be selecting text in. We
need the "_root" before "textInstance" because the
ActionScript in this line is for the button, and we need
to tell it to go back to the main timeline to look for
our text field instance.
- var
inputterString:String
= _root.inputter;
- var inputLength:Number
= inputterString.length;
In these two lines, we're creating two
variables. The first variable is set to be the same
thing as what is in the input field at the time the
button is pressed. The second variable takes our newly
created variable and counts the number of characters in
it, which we'll use later on to do some calculations
with.
-
textStart =
textVar.indexOf(inputter,
0);
Here we're setting the textStart
variable to be a number that is equal to the position in
the text field where the text we're searching for
begins. The "0" represents where we start searching; in
this case at the beginning of the text.
- if
(inputLength>0)
{
- textEnd =
textStart+inputLength;
- } else
{
- textEnd =
0;
- }
With this if statement, we're
checking to see if someone has typed characters into the
input box. If so, it will set the textEnd variable to
the amount of character typed in plus the start position
of the charaters being searched for.
After this we have an else
statement, which will set the textEnd variable to 0 if
no text has been entered, so nothing will be selected.
- if
(textStart>=0)
{
- Selection.setSelection(textStart,
textEnd);
- } else
{
- Selection.setSelection(0,
0);
- }
Here we have another if statement
that controls what gets selected (highlighted) in the
text box. The reason we have an if
statement here is because the indexOf function we
used to find the text start point will return "-1" if it
doesn't find the text it was looking for.
If the textStart variable is "-1",
that means the text wasn't found, so it will go to
the else statement where no text will be
highlighted. This is to prevent users from entering
text that isn't found. Since the inputLength
variable is the same whether or not the text is
actually found, and the textEnd variable is based on
inputLength, before this statement was added the
user could fool the selector into highlighting
characters in the beginning even if they weren't the
ones being searched for.
- _root.textEnd
= textEnd;
In this little tidbit we're setting up a
variable for our Find Next button which will tell it
where to start searching. We want it to start searching
after where it left off the first time, so we don't find
the same instance again.
-
findNext.onRelease
= function()
{
- Selection.setFocus("_root.textInstance");
- var inputterString:String
= _root.inputter;
- var inputLength:Number
= inputterString.length;
- textStart =
textVar.indexOf(inputter,
_root.textEnd);
- if (inputLength>0)
{
- textEnd =
textStart+inputLength;
- } else
{
- textEnd =
0;
- }
- if (textStart>=0)
{
- Selection.setSelection(textStart,
textEnd);
- } else
{
- Selection.setSelection(0,
0);
- }
- _root.textEnd
= textEnd;
- }
This is the ActionScript for the Find
Next button. It is identical except for one line with a
difference that I will explain below.
-
textStart =
textVar.indexOf(inputter,
_root.textEnd);
As you can see the difference here is that it starts to
search the input string at the end of where it was at
the last cycle. Since this button also includes the line
that defines textEnd, every time the button is pressed
it will find the next instance until it runs out of
places to find what it's looking for. When it can't find
anymore, textStart is defined as "-1" so nothing will be
selected on the next press.
 |
Note |
If you want to use this
tutorial for Static Text boxes, I have provided
the .fla from a version of this tutorial that
has a Static Text box.
Static
Text .fla
| |
Closing Time
That's about it for this tutorial. If you
want to learn about loading Dynamic Text from
external sources, check out
This Tutorial
by Kirupa.
If you have any questions,
make sure you post about it in the
Forums. If I don't answer, one of the
other great forum members will.
The files I used in this
tutorial can be downloaded below:
Have fun scripting!
 |
page 2 of 2 |
|