Archive for June, 2009

If you have five radio buttons

If you have five radio buttons on a screen, how many of them can be turned on at one time?

A2: Only one radio button in a group can be on at a time. Radio buttons represent a situation where the choice is exclusive.

3: If you want the cursor to immediately appear in an input text, what do you need to do?

A3: First, set a name for the text field. Then use Selection.setFocus() to place the cursor there.

4: How do you prevent the user from entering any non-numbers into a text field?

A4: Set its restrict property to “0123456789″.

Taken From: Sams Teach Yourself Flash™ MX ActionScript in 24 Hours

No Comments

Is there a way to restrict

Is there a way to restrict user input to only upper- or lowercase letters?

A3: No, but you can use String.toUpperCase or String.toLowerCase to convert the results of their input when they are finished.

Workshop
The quiz questions are designed to test your knowledge of the material covered in this hour. The answers to the questions follow.

If you have five check boxes on a screen, how many of them can be turned on at one time?

A1: All of them. Check boxes are independent of each other, so any combination of check boxes can be on or off.

Taken From: Sams Teach Yourself Flash™ MX ActionScript in 24 Hours

No Comments

Q&A

Radio button and check box components come with Flash MX. Would it be easier to use them instead of writing my own code?

A1: In many cases, yes. But you can customize your own code in ways that the components cannot. Plus, you have more control over how your movie looks and how it performs.

Q2: How can I have more than one group of radio buttons on the screen at the same time?

A2: You could simply place all the radio buttons in a group together in their own movie clip. If you do that, you’ll need to refer to the movie clip to use the getValue function: group1.myRadioButton.getValue().

Taken From: Sams Teach Yourself Flash™ MX ActionScript in 24 Hours

No Comments

Summary

Check boxes and radio buttons are common interface elements that can be created with ActionScript. Check boxes have an on and off state and are independent of each other. Radio buttons also have on and off states, but they are related to each other so that only one radio button is on at any given time.

Text input fields can be modified to only accept certain characters. You can restrict text input to what is appropriate to that field. You can also restrict the input length of a field.

Using the tabIndex property of a field, you can change the order in which the fields receive keyboard focus when the user Tabs between them. You can also be notified when a Tab occurs by using the onSetFocus listener event.

Taken From: Sams Teach Yourself Flash™ MX ActionScript in 24 Hours

No Comments

The next function checks

The next function checks the userYearText variable. It makes sure that there is a value there, and then it compares the value to the current year. It gets the current year from the user’s system clock. It makes sure that the year is at least 100 years ago and no more than this year.

This last function checks the userEmailText to make sure that it is at least seven characters long, that it contains a “@” and a dot, that at least two characters are after the dot, and that the “@” comes before the dot.

Now all the fields are checked as the user Tabs through them. However, we’ll want to perform a complete check when the user clicks the Submit button. This calls the same three functions, one-by-one, until it hits one that is not true. If all are true, then it returns a true.

It is up to the button script to call the checkAll function. If it gets a true from the checkAll function, it can move the movie to the next frame, where there is a “Thank You” message.

In a complete movie, you will probably want to submit the information to your server. You will find out how to do that in Hour 18.

You could simplify this movie a great deal by removing the listener. Instead, just check for the correct input when the user clicks the Submit button.

Taken From: Sams Teach Yourself Flash™ MX ActionScript in 24 Hours

No Comments

Then the oldFocus variable

Then the oldFocus variable is compared to the three text fields. If it matches one of them, one of three functions is called. The result of this function is placed in ret.

The variable ret is set to false by one of these functions if the value of the field is not appropriate. In that case, ignoreSetFocus is set to true, and the focus is changed back to the previous field. The purpose of ignoreSetFocus is to prevent the function from being called a second time when the focus is changed by the code.

The checkUserName function examines the length of the userNameText to make sure that it is at least three characters. If it is not, a message is placed in the text field linked to the feedback variable. The function returns a false as well. Otherwise, the feedback variable is cleared, and a true is returned.

Taken From: Sams Teach Yourself Flash™ MX ActionScript in 24 Hours

No Comments

Next, we’ll force the movie to start

Next, we’ll force the movie to start with the keyboard focus on the first field.

Selection.setFocus(userName);

To recognize when a user is finished with a field, we’ll monitor the focus of the keyboard with a Selection object listener. This alerts us whenever the onSetFocus event occurs.

Selection.addListener(this);

Next, we’ll set a variable named ignoreSetFocus to false. We’ll use this variable later in the script.

ignoreSetFocus = false;

The function that deals with the onSetFocus event gets two parameters passed to it. The first is a reference to the field that was the previously focused text field, and the second is the currently focused text field.

We’ll use the oldFocus parameter to determine which field was just completed. But first, the variable ignoreSetFocus is checked. If it is true, it is set to false for next time. The function exits immediately by using the return command.

Taken From: Sams Teach Yourself Flash™ MX ActionScript in 24 Hours

No Comments

Task: Checking for a Valid Input

Task: Checking for a Valid Input
Next, let’s build a simple user input form that checks the user’s input as the user types. It asks for the user’s name, year of birth, and e-mail address. It checks to make sure that each entry fits certain criteria.

The name, for instance, should be at least three characters long. The date of birth should be four digits and be sometime in the last 100 years. E-mail address should be at least seven characters long and be of the format a@b.c, where a and b can be anything, but c must be at least three characters long. The “@” and period must be present.

Start with a new movie.

The main part of the code goes on the frame. It starts with the stop() command so that the movie doesn’t continue past the first frame. Then, the restrict and maxChars properties are set for each field.

Taken From: Sams Teach Yourself Flash™ MX ActionScript in 24 Hours

No Comments

The preceding line leaves one question

The preceding line leaves one question: Will the input field accept both upper- and lowercase letters? Yes, it will accept both, so the restrict property ignores case.

You can also limit the number of characters allowed in an input field. This can be done without ActionScript because there is a Maximum Characters option in the Properties panel. If you set this to 32, only 32 characters would be allowed in the field. You can also set this property with ActionScript using the maxChars property. For instance, if you wanted to limit a field to only accept years, you would want it to only accept digits, and only four of them:

text1.restrict = “01234567890″;
text1.maxChars = 4;

The example movie 15restrict.fla contains the preceding code and sample input field for you to try out.

Taken From: Sams Teach Yourself Flash™ MX ActionScript in 24 Hours

No Comments

You restrict which characters

You restrict which characters can be typed into an input field by using the restrict property of that field. If you don’t set this property at all, any character would be allowed in the field. However, if you set it to a string, only characters in that string are allowed.

Here is how you would set the input field text1 to only accept number characters:

text1.restrict = “01234567890″;

Suppose that you have a field where the user is supposed to type her e-mail address. E-mail addresses can only have the basic letters and numbers in them, plus the special characters “@” and “.”. In addition, dashes and underscores are allowed in e-mail addresses. This is how you would restrict the user to only these characters:

text2.restrict = “abcdefghijklmnopqrstuvwxyz0123456789@.-_”;

Taken From: Sams Teach Yourself Flash™ MX ActionScript in 24 Hours

No Comments

  • Partner links