Because all string indexes are zero-based, the index of the last character for any string x is tracecheckAtSymbol"dog@house.net"; // output: true tracecheckAtSymbol"Chris"; // output: fa
Trang 1Property; an integer specifying the number of characters in the specified String object
Because all string indexes are zero-based, the index of the last character for any string x is
trace(checkAtSymbol("dog@house.net")); // output: true
trace(checkAtSymbol("Chris")); // output: false
An example is also in the Strings.fla file in the HelpExamples folder The following list gives typical paths to this folder:
• Windows: \Program Files\Macromedia\Flash MX 2004\Samples\HelpExamples\
• Macintosh: HD/Applications/Macromedia Flash MX 2004/Samples/HelpExamples/
•
Trang 2The following example creates a variable, my_str, assigns it a String value, and then calls the
slice() method using a variety of values for both the start and end parameters Each call to
slice() is wrapped in a trace() statement that displays the output in the Output panel
// Index values for the string literal
// positive index: 0 1 2 3 4
// string: L o r e m
// negative index: -5-4-3-2-1
var my_str:String = "Lorem";
// slice the first character
trace("slice(0,1): "+my_str.slice(0, 1)); // output: slice(0,1): L
trace("slice(-5,1): "+my_str.slice(-5, 1)); // output: slice(-5,1): L
// slice the middle three characters
trace("slice(1,4): "+my_str.slice(1, 4)); // slice(1,4): ore
trace("slice(1,-1): "+my_str.slice(1, -1)); // slice(1,-1): ore
// slices that return empty strings because start is not to the left of end trace("slice(1,1): "+my_str.slice(1, 1)); // slice(1,1):
trace("slice(3,2): "+my_str.slice(3, 2)); // slice(3,2):
trace("slice(-2,2): "+my_str.slice(-2, 2)); // slice(-2,2):
Trang 3String.slice() 803
// slices that omit the end parameter use String.length, which equals 5 trace("slice(0): "+my_str.slice(0)); // slice(0): Lorem
trace("slice(3): "+my_str.slice(3)); // slice(3): em
An example is also in the Strings.fla file in the HelpExamples folder The following list gives typical paths to this folder:
• Windows: \Program Files\Macromedia\Flash MX 2004\Samples\HelpExamples\
• Macintosh: HD/Applications/Macromedia Flash MX 2004/Samples/HelpExamples/
•
See also
String.substr() , String.substring()
Trang 4delimiter A string; the character or string at which my_str splits
limit A number; the number of items to place into the array This parameter is optional
Returns
An array; an array containing the substrings of my_str
Description
Method; splits a String object into substrings by breaking it wherever the specified delimiter
parameter occurs and returns the substrings in an array If you use an empty string ("") as a delimiter, each character in the string is placed as an element in the array
If the delimiter parameter is undefined, the entire string is placed into the first element of the returned array
Example
The following example returns an array with five elements:
var my_str:String = "P,A,T,S,Y";
var my_array:Array = my_str.split(",");
for (var i = 0; i<my_array.length; i++) {
The following example returns an array with two elements, "P" and "A":
var my_str:String = "P,A,T,S,Y";
var my_array:Array = my_str.split(",", 2);
trace(my_array); // output: P,A
The following example shows that if you use an empty string ("") for the delimiter parameter, each character in the string is placed as an element in the array:
var my_str:String = new String("Joe");
var my_array:Array = my_str.split("");
for (var i = 0; i<my_array.length; i++) {
trace(my_array[i]);
}
Trang 5• Windows: \Program Files\Macromedia\Flash MX 2004\Samples\HelpExamples\
• Macintosh: HD/Applications/Macromedia Flash MX 2004/Samples/HelpExamples/
•
See Also
Array.join()
Trang 6length A number; the number of characters in the substring being created If length is not specified, the substring includes all the characters from the start to the end of the string.
Example
The following example creates a new string, my_str and uses substr() to return the second word
in the string; first, using a positive start parameter, and then using a negative start parameter:
var my_str:String = new String("Hello world");
var mySubstring:String = new String();
mySubstring = my_str.substr(6,5);
trace(mySubstring); // output: world
mySubstring = my_str.substr(-5,5);
trace(mySubstring); // output: world
An example is also in the Strings.fla file in the HelpExamples folder The following list gives typical paths to this folder:
• Windows: \Program Files\Macromedia\Flash MX 2004\Samples\HelpExamples\
• Macintosh: HD/Applications/Macromedia Flash MX 2004/Samples/HelpExamples/
•
Trang 7end A number; an integer that is 1+ the index of the last character in my_str to be extracted Valid values for end are 1 through String.length The character indexed by the end parameter
is not included in the extracted string If this parameter is omitted, String.length is used If this parameter is a negative value, 0 is used
Returns
String: a substring of the specified string
Description
Method; returns a string comprising the characters between the points specified by the start and
end parameters If the end parameter is not specified, the end of the substring is the end of the string If the value of start equals the value of end, the method returns an empty string If the value of start is greater than the value of end, the parameters are automatically swapped before the function executes and the original value is unchanged
Example
The following example shows how to use substring():
var my_str:String = "Hello world";
var mySubstring:String = my_str.substring(6,11);
trace(mySubstring); // output: world
The following example shows what happens if a negative start parameter is used:
var my_str:String = "Hello world";
var mySubstring:String = my_str.substring(-5,5);
trace(mySubstring); // output: Hello
An example is also in the Strings.fla file in the Examples folder The following list gives typical paths to this folder:
• Windows: \Program Files\Macromedia\Flash MX 2004\Samples\HelpExamples\
• Macintosh: HD/Applications/Macromedia Flash MX 2004/Samples/HelpExamples/
•
Trang 8var upperCase:String = "LOREM IPSUM DOLOR";
var lowerCase:String = upperCase.toLowerCase();
trace("upperCase: " + upperCase); // output: upperCase: LOREM IPSUM DOLOR trace("lowerCase: " + lowerCase); // output: lowerCase: lorem ipsum dolor
An example is also in the Strings.fla file in the HelpExamples folder The following list gives typical paths to this folder:
• Windows: \Program Files\Macromedia\Flash MX 2004\Samples\HelpExamples\
• Macintosh: HD/Applications/Macromedia Flash MX 2004/Samples/HelpExamples/
•
See Also
String.toUpperCase()
Trang 9var lowerCase:String = "lorem ipsum dolor";
var upperCase:String = lowerCase.toUpperCase();
trace("lowerCase: " + lowerCase); // output: lowerCase: lorem ipsum dolor trace("upperCase: " + upperCase); // output: upperCase: LOREM IPSUM DOLOR
An example is also found in the Strings.fla file in the HelpExamples folder The following list gives typical paths to this folder:
• Windows: \Program Files\Macromedia\Flash MX 2004\Samples\HelpExamples\
• Macintosh: HD/Applications/Macromedia Flash MX 2004/Samples/HelpExamples/
•
See also
String.toLowerCase()
Trang 10String delimiter; when used before and after characters, quotation marks ("") indicate that the
characters have a literal value and are considered a string, not a variable, numerical value, or other
ActionScript element
Example
The following example uses quotation marks ("") to indicate that the value of the variable
yourGuess is the literal string "Prince Edward Island" and not the name of a variable The
value of province is a variable, not a literal; to determine the value of province, the value of
yourGuess must be located
var yourGuess:String = "Prince Edward Island";
Trang 11method The method to invoke in the superclass.
arg1 Optional parameters that are passed to the superclass version of the method (syntax 1) or
to the constructor function of the superclass (syntax 2)
Returns
Both forms invoke a function The function can return any value
Description
Operator: the first syntax style can be used within the body of an object method to invoke the
superclass version of a method and can optionally pass parameters (arg1 argN) to the
superclass method This is useful for creating subclass methods that add behavior to superclass
methods but also invoke the superclass methods to perform their original behavior
The second syntax style can be used within the body of a constructor function to invoke the
superclass version of the constructor function and can optionally pass parameters to it This is
useful for creating a subclass that performs additional initialization but also invokes the superclass
constructor to perform superclass initialization
Example
In the following example, you create two classes You use the super keyword in the Sock class to
call functions in the parent class (Clothes) Although both the Socks and Clothes classes have a
method called getColor(), using super lets you specifically reference the base class’s methods
and properties Create a new AS file called Clothes.as, and enter the following code:
Trang 12Then create a new class called Socks that extends the Clothes class, as shown in the following example:
class Socks extends Clothes {
private var color:String;
The following result is displayed in the Output panel:
[Clothes] I am the constructor
[Socks] I am the constructor
If you forgot to put the super keyword in the Sock class’s getColor() method, then the
getColor() method could call itself repeatedly, which would cause the script to fail because of infinite recursion problems The Output panel would display the following error if you didn’t use the super keyword:
[Socks] I am getColor
[Socks] I am getColor
[Socks] I am getColor
256 levels of recursion were exceeded in one action list.
This is probably an infinite loop.
Further execution of actions has been disabled in this movie.
Trang 13expression Any expression.
caseClause A case keyword followed by an expression, a colon, and a group of statements to
execute if the expression matches the switch expression parameter using strict equality (===)
defaultClause A default keyword followed by statements to execute if none of the case
expressions match the switch expression parameter strict equality (===)
Returns
Nothing
Description
Statement; creates a branching structure for ActionScript statements As with the if statement,
the switch statement tests a condition and executes statements if the condition returns a value of
true All switch statements should include a default case The default case should include a break
statement that prevents a fall-through error if another case is added later When a case falls
through, it doesn’t have a break statement
Example
In the following example, if the String.fromCharCode(Key.getAscii()) parameter evaluates
to A, the trace() statement that follows case "A" executes; if the parameter evaluates to a, the
trace() statement that follows case "a" executes; and so on If no case expression matches the
String.fromCharCode(Key.getAscii()) parameter, the trace() statement that follows the
default keyword executes
var listenerObj:Object = new Object();
Trang 15You can use the System.capabilities object to determine the abilities of the system and player
hosting a SWF file, which lets you tailor content for different formats For example, the screen of
a cell phone (black and white, 100 square pixels) is different than the 1000-square-pixel color PC
screen To provide appropriate content to as many users as possible, you can use the
System.capabilities object to determine the type of device a user has You can then either specify
to the server to send different SWF files based on the device capabilities or tell the SWF file to
alter its presentation based on the capabilities of the device
You can send capabilities information using a GET or POST HTTP method The following
example shows a server string for a computer that has MP3 support, 1600 x 1200 pixel resolution,
is running Windows XP, and Flash Player 7 (7.0.19.0):
"A=t&SA=t&SV=t&EV=t&MP3=t&AE=t&VE=t&ACC=f&PR=t&SP=t&SB=f&DEB=t&V=WIN%207%2C0%2
C19%2C0&M=Macromedia%20Windows&R=1600x1200&DP=72&COL=color&AR=1.0&OS=Window
s%20XP&L=en&PT=External&AVD=f&LFD=f&WD=f"
Property summary for the System.capabilities object
All properties of the System.capabilities object are read-only
string
System.capabilities.avHardwareDisable Specifies whether the user’s camera and
microphone are enabled or disabled.
AVD
System.capabilities.hasAccessibility Indicates whether the player is running on a
system that supports communication between Flash Player and accessibility aids.
ACC
System.capabilities.hasAudio Indicates whether the player is running on a
system that has audio capabilities.
A
System.capabilities.hasAudioEncoder Indicates whether the player is running on a
system that can encode an audio stream, such as that coming from a microphone.
AE
System.capabilities.hasEmbeddedVideo Indicates whether the player is running on a
system that supports embedded video.
EV
System.capabilities.hasMP3 Indicates whether the player is running on a
system that has an MP3 decoder.
MP3
System.capabilities.hasPrinting Indicates whether the player is running on a
system that supports printing.
PR
CHAPTER 2
ActionScript Language Reference
Trang 16System.capabilities.hasScreenBroadcast Indicates whether the player supports the
development of screen broadcast applications to be run through the Flash Communication Server
SB
System.capabilities.hasScreenPlayback Indicates whether the player supports the
playback of screen broadcast applications that are being run through the Flash Communication Server.
System.capabilities.hasVideoEncoder Indicates whether the player can encode a
video stream, such as that coming from a web camera.
VE
System.capabilities.isDebugger Indicates whether the player is an officially
released version or a special debugging version.
DEB
System.capabilities.language Indicates the language of the system on
which the player is running.
L
System.capabilities.localFileReadDisable Specifies whether the player will attempt to
read anything (including the first SWF file the player launches with) from the user’s hard disk.
System.capabilities.screenColor Indicates whether the screen is color,
grayscale, or black and white.
for each System.capabilities property.
Trang 24The following example traces the value of this read-only property:
trace(System.capabilities.hasScreenBroadcast);
Trang 26Read-only property: a Boolean value that is true if the player can play streaming audio; false
otherwise The server string is SA
Example
The following example traces the value of this read-only property:
trace(System.capabilities.hasStreamingAudio);
Trang 27Read-only property: a Boolean value that is true if the player can play streaming video; false
otherwise The server string is SV
Example
The following example traces the value of this read-only property:
trace(System.capabilities.hasStreamingVideo);
Trang 30This property changed in two ways for Flash Player 7 First, the language code for English systems
no longer includes the country code In Flash Player 6, all English systems return the language code and the two-letter country code subtag (en-US) In Flash Player 7, English systems return only the language code (en) Second, on Microsoft Windows systems this property now returns the User Interface (UI) Language In Flash Player 6 on the Microsoft Windows platform,
System.capabilities.language returns the User Locale, which controls settings for
formatting dates, times, currency and large numbers In Flash Player 7 on the Microsoft Windows platform, this property now returns the UI Language, which refers to the language used for all menus, dialog boxes, error messages and help files
Trang 32LoadMovie(), or LoadVars.load() will fail if this property is set to true
Reading runtime shared libraries will also be blocked if this property is set to true, but reading local shared objects is allowed without regard to the value of this property The server string is
LFD
Example
The following example traces the value of this read-only property:
trace(System.capabilities.localFileReadDisable);
Trang 33Read-only property; a string that indicates the manufacturer of Flash Player, in the format
"Macromedia OSName" (OSName could be "Windows", "Macintosh", "Linux", or "Other OS Name") The server string is M
Example
The following example traces the value of this read-only property:
trace(System.capabilities.manufacturer);
Trang 34"Windows 95", "Windows CE" (available only in Flash Player SDK, not in the desktop version),
"Linux", and "MacOS" The server string is OS
Example
The following example traces the value of this read-only property:
trace(System.capabilities.os);
Trang 36• "StandAlone" for the Flash StandAlone Player
• "External" for the Flash Player version used by test movie mode,
• "PlugIn" for the Flash Player browser plug-in
• "ActiveX" for the Flash Player ActiveX Control used by Microsoft Internet Explorer The server string is PT
Example
The following example traces the value of this read-only property:
trace(System.capabilities.playerType);
Trang 37Read-only property; a string that indicates the screen color This property can have the value
"color", "gray" or "bw", which represents color, grayscale, and black and white, respectively The server string is COL
Example
The following example traces the value of this read-only property:
trace(System.capabilities.screenColor);