Tải bản đầy đủ (.pdf) (100 trang)

Tài liệu Flash: ActionScript Language Reference- P9 ppt

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (324.16 KB, 100 trang )

String.length 801
String.length
Availability
Flash Player 5.
Usage
my_str.length:Number
Description
Property; 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
x.length - 1
.
Example
The following example creates a new String object and uses
String.length
to count the number
of characters:
var my_str:String = "Hello world!";
trace(my_str.length); // output: 12
The following example loops from 0 to
my_str.length
. The code checks the characters within a
string, and if the string contains the
@
character,
true
displays in the Output panel. If it does not
contain the
@
character, then


false
displays in the Output panel.
function checkAtSymbol(my_str:String):Boolean {
for (var i = 0; i<my_str.length; i++) {
if (my_str.charAt(i) == "@") {
return true;
}
}
return false;
}
trace(checkAtSymbol("")); // 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/

802 Chapter 2:
String.slice()
Availability
Flash Player 5.
Usage
my_str.slice(start:Number, [end:Number]) : String
Parameters
start
A number; the zero-based index of the starting point for the slice. If
start
is a negative

number, the starting point is determined from the end of the string, where -1 is the last character.
end
A number; an integer that is 1+ the index of the ending point for the slice. The character
indexed by the
end
parameter is not included in the extracted string. If this parameter is omitted,
String.length
is used. If
end
is a negative number, the ending point is determined by counting
back from the end of the string, where -1 is the last character.
Returns
A string; a substring of the specified string.
Description
Method; returns a string that includes the
start
character and all characters up to, but not
including, the
end
character. The original String object is not modified. If the
end
parameter is
not specified, the end of the substring is the end of the string. If the character indexed by
start
is
the same as or to the right of the character indexed by
end
, the method returns an empty string.
Example
The 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):
String.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()
804 Chapter 2:
String.split()
Availability
Flash Player 5.
Usage
my_str.split("delimiter":String, [limit:Number]) : Number
Parameters
delimiter
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++) {
trace(my_array[i]);
}
/* output:
P
A
T
S
Y
*/
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]);
}
String.split() 805
/* output:
J
o
e
*/
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
Array.join()
806 Chapter 2:
String.substr()
Availability

Flash Player 5.
Usage
my_str.substr(start:Number, [length:Number]) : String
Parameters
start
A number; an integer that indicates the position of the first character in
my_str
to be
used to create the substring. If
start
is a negative number, the starting position is determined
from the end of the string, where the -1 is the last character.
length
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.
Returns
A string; a substring of the specified string.
Description
Method; returns the characters in a string from the index specified in the
start
parameter
through the number of characters specified in the
length
parameter. The
substr
method does
not change the string specified by
my_str

; it returns a new 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/

String.substring() 807
String.substring()
Availability
Flash Player 5.
Usage
my_str.substring(start:Number, [end:Number]) : String
Parameters

start
A number; an integer that indicates the position of the first character of
my_str
used to
create the substring. Valid values for
start
are

0 through
String.length
- 1. If
start
is a
negative value, 0 is used.
end
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/

808 Chapter 2:
String.toLowerCase()
Availability
Flash Player 5.
Usage
my_str.toLowerCase() : String
Parameters
None.
Returns
A string.
Description
Method; returns a copy of the String object, with all uppercase characters converted to lowercase.
The original value is unchanged.
Example
The following example creates a string with all uppercase characters and then creates a copy of
that string using
toLowerCase()
to convert all uppercase characters to lowercase characters:
var 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()
String.toUpperCase() 809
String.toUpperCase()
Availability
Flash Player 5.
Usage
my_str.toUpperCase() : String
Parameters
None.
Returns
A string.
Description
Method; returns a copy of the String object, with all lowercase characters converted to uppercase.
The original value is unchanged.
Example
The following example creates a string with all lowercase characters and then creates a copy of that
string using
toUpperCase()
:
var 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()
810 Chapter 2: ActionScript Language Reference
" " (string delimiter)
Availability
Flash Player 4.
Usage
"text"
Parameters
text
A sequence of zero or more characters.
Returns
Nothing.
Description
String 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";
submit_btn.onRelease = function() {
trace(yourGuess);
};
// displays Prince Edward Island in the Output panel
See also
String class, String()
CHAPTER 2
ActionScript Language Reference
super 811
super
Availability
Flash Player 6.
Usage
super.method([arg1, ..., argN])
super([arg1, ..., argN])
Parameters
method
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:
class Clothes {
private var color:String;

function Clothes(param_color) {
this.color = param_color;
trace("[Clothes] I am the constructor");
}
function getColor():String {
trace("[Clothes] I am getColor");
return this.color;
}
function setColor(param_color:String):Void {
this.color = param_color;
trace("[Clothes] I am setColor");
}
}
CHAPTER 2
ActionScript Language Reference
812 Chapter 2: ActionScript Language Reference
Then 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;
function Socks(param_color:String) {
this.color = param_color;
trace("[Socks] I am the constructor");
}
function getColor():String {
trace("[Socks] I am getColor");
return super.getColor();
}
function setColor(param_color:String):Void {
this.color = param_color;

trace("[Socks] I am setColor");
}
}
Then create a new AS or FLA file and enter the following ActionScript in the document:
import Socks;
var mySock:Socks = new Socks("maroon");
trace(" -> "+mySock.getColor());
mySock.setColor("Orange");
trace(" -> "+mySock.getColor());
The following result is displayed in the Output panel:
[Clothes] I am the constructor
[Socks] I am the constructor
[Socks] I am getColor
[Clothes] I am getColor
-> maroon
[Socks] I am setColor
[Socks] I am getColor
[Clothes] I am getColor
-> Orange
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.
switch 813
switch
Availability
Flash Player 4.
Usage
switch (expression){
caseClause:
[defaultClause:]
}
Parameters
expression
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();
listenerObj.onKeyDown = function() {
switch (String.fromCharCode(Key.getAscii())) {
case "A" :
trace("you pressed A");
break;
case "a" :
trace("you pressed a");
break;
case "E" :
case "e" :
trace("you pressed E or e");
break;
case "I" :

CHAPTER 2
ActionScript Language Reference
814 Chapter 2: ActionScript Language Reference
case "i" :
trace("you pressed I or i");
break;
default :
trace("you pressed some other key");
}
};
Key.addListener(listenerObj);
See also
=== (strict equality), break, case, default, if
System.capabilities object 815
System.capabilities object
Availability
Flash Player 6.
Description
You 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.
Property Description Server
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
816 Chapter 2: ActionScript Language Reference
System.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.
SP
System.capabilities.hasStreamingAudio
Indicates whether the player can play
streaming audio.
SA
System.capabilities.hasStreamingVideo
Indicates whether the player can play

streaming video.
SV
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.
LFD
System.capabilities.manufacturer
Indicates the manufacturer of Flash Player.
M
System.capabilities.os
Indicates the operating system hosting
Flash Player.
OS
System.capabilities.pixelAspectRatio

Indicates the pixel aspect ratio of the screen.
AR
System.capabilities.playerType
Indicates the type of player: stand-alone,
external, plug-in, or ActiveX.
PT
System.capabilities.screenColor
Indicates whether the screen is color,
grayscale, or black and white.
COL
System.capabilities.screenDPI
Indicates the dots-per-inch screen
resolution, in pixels.
DP
System.capabilities.screenResolutionX
Indicates the horizontal size of the screen.
R
System.capabilities.screenResolutionY
Indicates the vertical size of the screen.
R
System.capabilities.serverString
A URL-encoded string that specifies values
for each
System.capabilities
property.
n/a
System.capabilities.version
A string containing Flash Player version and
platform information.
V

Property Description Server
string
System.capabilities.avHardwareDisable 817
System.capabilities.avHardwareDisable
Availability
Flash Player 7.
Usage
System.capabilities.avHardwareDisable:Boolean
Description
Read-only property; a Boolean value that specifies whether access to the user’s camera and
microphone has been administratively prohibited (
true
) or allowed (
false
). The server string is
AVD
.
Example
The following example traces the value of this read-only property:
trace(System.capabilities.avHardwareDisable);
See also
Camera.get(), Microphone.get(), System.showSettings()
818 Chapter 2: ActionScript Language Reference
System.capabilities.hasAccessibility
Availability
Flash Player 6.
Usage
System.capabilities.hasAccessibility:Boolean
Description
Read-only property: a Boolean value that is

true
if the player is running in an environment that
supports communication between Flash Player and accessibility aids;
false
otherwise. The server
string is
ACC
.
Example
The following example traces the value of this read-only property:
trace(System.capabilities.hasAccessibility);
See also
Accessibility.isActive(), Accessibility.updateProperties(), _accProps
System.capabilities.hasAudio 819
System.capabilities.hasAudio
Availability
Flash Player 6.
Usage
System.capabilities.hasAudio:Boolean
Description
Read-only property: a Boolean value that is
true
if the player is running on a system that has
audio capabilities;
false
otherwise. The server string is
A
.
Example
The following example traces the value of this read-only property:

trace(System.capabilities.hasAudio);
820 Chapter 2: ActionScript Language Reference
System.capabilities.hasAudioEncoder
Availability
Flash Player 6.
Usage
System.capabilities.hasAudioEncoder:Boolean
Description
Read-only property: a Boolean value that is
true
if the player can encode an audio stream, such as
that coming from a microphone;
false
otherwise. The server string is
AE
.
Example
The following example traces the value of this read-only property:
trace(System.capabilities.hasAudioEncoder);
System.capabilities.hasEmbeddedVideo 821
System.capabilities.hasEmbeddedVideo
Availability
Flash Player 6 r65.
Usage
System.capabilities.hasEmbeddedVideo:Boolean
Description
Read-only property: a Boolean value that is
true
if the player is running on a system that
supports embedded video;

false
otherwise. The server string is
EV
.
Example
The following example traces the value of this read-only property:
trace(System.capabilities.hasEmbeddedVideo);
822 Chapter 2: ActionScript Language Reference
System.capabilities.hasMP3
Availability
Flash Player 6.
Usage
System.capabilities.hasMP3:Boolean
Description
Read-only property: a Boolean value that is
true
if the player is running on a system that has an
MP3 decoder;
false
otherwise. The server string is
MP3
.
Example
The following example traces the value of this read-only property:
trace(System.capabilities.hasMP3);
System.capabilities.hasPrinting 823
System.capabilities.hasPrinting
Availability
Flash Player 6 r65.
Usage

System.capabilities.hasPrinting:Boolean
Description
Read-only property: a Boolean value that is
true
if the player is running on a system that
supports printing;
false
otherwise. The server string is
PR
.
Example
The following example traces the value of this read-only property:
trace(System.capabilities.hasPrinting);
824 Chapter 2: ActionScript Language Reference
System.capabilities.hasScreenBroadcast
Availability
Flash Player 6 r79.
Usage
System.capabilities.hasScreenBroadcast:Boolean
Description
Read-only property: a Boolean value that is
true
if the player supports the development of screen
broadcast applications to be run through the Flash Communication Server;
false
otherwise. The
server string is
SB
.
Example

The following example traces the value of this read-only property:
trace(System.capabilities.hasScreenBroadcast);
System.capabilities.hasScreenPlayback 825
System.capabilities.hasScreenPlayback
Availability
Flash Player 6 r79.
Usage
System.capabilities.hasScreenPlayback:Boolean
Description
Read-only property: a Boolean value that is
true
if the player supports the playback of screen
broadcast applications that are being run through the Flash Communication Server;
false

otherwise. The server string is
SP
.
Example
The following example traces the value of this read-only property:
trace(System.capabilities.hasScreenPlayback);

×