Example In the following example, a receiving SWF file accepts commands only from SWF files located in the same domain or at macromedia.com: // If both the sending and receiving SWF file
Trang 1LocalConnection class 401
Event handler summary for the LocalConnection class
Constructor for the LocalConnection class
// Code in the receiving SWF file
this.createTextField("result_txt", 1, 10, 10, 100, 22);
result_txt.border = true;
var receiving_lc:LocalConnection = new LocalConnection();
receiving_lc.methodToExecute = function(param1:Number, param2:Number) { result_txt.text = param1+param2;
};
receiving_lc.connect("lc_name");
The following SWF file sends the request to the first SWF file
// Code in the sending SWF file
var sending_lc:LocalConnection = new LocalConnection();
sending_lc.send("lc_name", "methodToExecute", 5, 7);
LocalConnection.allowDomain Invoked whenever the current (receiving) LocalConnection
object receives a request to invoke a method from a sending LocalConnection object.
LocalConnection.allowInsecureDomain Invoked whenever the current (receiving) LocalConnection
object, which is in a SWF file hosted at a domain using a secure protocol (HTTPS), receives a request to invoke a method from a sending LocalConnection object that is in a SWF file hosted at a non-secure protocol
LocalConnection.onStatus Invoked after a sending LocalConnection object tries to
send a command to a receiving LocalConnection object.
Trang 2402 Chapter 2: ActionScript Language Reference
See also
LocalConnection.connect() , LocalConnection.send()
Trang 3receiving_lc.allowDomain = function([sendingDomain:String]) : Boolean {
// Your statements here return true or false
do declare sendingDomain, you probably want to compare the value of sendingDomain with domains from which you want to accept commands The following examples show both implementations
In files authored for Flash Player 6, the sendingDomain parameter contains the superdomain of the caller In files authored for Flash Player 7 or later, the sendingDomain parameter contains the exact domain of the caller In the latter case, to allow access by SWF files hosted at either www.domain.com or store.domain.com, you must explicitly allow access from both domains
// For Flash Player 6
Trang 4404 Chapter 2: ActionScript Language Reference
sendingDomain=="store.domain.com");
}
Also, for files authored for Flash Player 7 or later, you can’t use this method to let SWF files hosted using a secure protocol (HTTPS) allow access from SWF files hosted in nonsecure protocols; you must use the LocalConnection.allowInsecureDomain event handler instead.Occasionally, you might encounter the following situation Suppose you load a child SWF file from a different domain You want to implement this method so that the child SWF file can make LocalConnection calls to the parent SWF file, but you don’t know the final domain from which the child SWF file will come This can happen, for example, when you use load-balancing redirects or third-party servers
In this situation, you can use the MovieClip._url property in your implementation of this method For example, if you load a SWF file into my_mc, you can then implement this method by checking whether the domain argument matches the domain of my_mc._url (You must parse the domain out of the full URL contained in my_mc._url.)
If you do this, make sure that you wait until the SWF file in my_mc is loaded, because the _url
property will not have its final, correct value until the file is completely loaded The best way to determine when a child SWF file finishes loading is to use MovieClipLoader.onLoadComplete.The opposite situation can also occur: You might create a child SWF file that wants to accept LocalConnection calls from its parent but doesn’t know the domain of its parent In this situation, implement this method by checking whether the domain argument matches the domain of
_parent._url. Again, you must parse the domain out of the full URL from _parent._url In this situation, you don’t have to wait for the parent SWF file to load; the parent will already be loaded by the time the child loads
Example
The following example shows how a LocalConnection object in a receiving SWF file can permit SWF files from any domain to invoke its methods Compare this to the example in
LocalConnection.connect(), in which only SWF files from the same domain can invoke the
trace() method in the receiving SWF file For a discussion of the use of the underscore (_) in the connection name, see LocalConnection.send()
this.createTextField("welcome_txt", this.getNextHighestDepth(), 10, 10, 100, 20);
var my_lc:LocalConnection = new LocalConnection();
Trang 5See also
LocalConnection.connect() , LocalConnection.domain() , LocalConnection.send() ,
MovieClip._url , MovieClipLoader.onLoadComplete , _parent
Trang 6406 Chapter 2: ActionScript Language Reference
not invoked
By default, SWF files hosted using the HTTPS protocol can be accessed only by other SWF files hosted using the HTTPS protocol This implementation maintains the integrity provided by the HTTPS protocol
Using this method to override the default behavior is not recommended, as it compromises HTTPS security However, you might need to do so, for example, if you need to permit access to HTTPS files published for Flash Player 7 or later from HTTP files published for Flash Player 6
A SWF file published for Flash Player 6 can use the LocalConnection.allowDomain event handler
to permit HTTP to HTTPS access However, because security is implemented differently in Flash Player 7, you must use the LocalConnection.allowInsecureDomain() method to permit such access in SWF files published for Flash Player 7 or later
Example
The following example allows connections from the current domain or from
www.macromedia.com, or allows insecure connections only from the current domain
this.createTextField("welcome_txt", this.getNextHighestDepth(), 10, 10, 100, 20);
var my_lc:LocalConnection = new LocalConnection();
my_lc.allowDomain = function(sendingDomain:String) {
domain_txt.text = sendingDomain;
return (sendingDomain == this.domain() || sendingDomain ==
"www.macromedia.com");
Trang 8408 Chapter 2: ActionScript Language Reference
this.createTextField("status_txt", this.getNextHighestDepth(), 10, 42, 100,44);
var receiving_lc:LocalConnection = new LocalConnection();
Trang 9connectionName A string that corresponds to the connection name specified in the
LocalConnection.send() command that wants to communicate with receiving_lc
Returns
A Boolean value: true if no other process running on the same client computer has already issued this command using the same value for the connectionName parameter; false otherwise
Description
Method; prepares a LocalConnection object to receive commands from a
LocalConnection.send() command (called the sending LocalConnection object) The object used with this command is called the receiving LocalConnection object The receiving and sending
objects must be running on the same client computer
Make sure you define the methods attached to receiving_lc before calling this method, as shown in all the examples in this section
By default, Flash Player resolves connectionName into a value of
"superdomain:connectionName", where superdomain is the superdomain of the SWF file containing the LocalConnection.connect() command For example, if the SWF file
containing the receiving LocalConnection object is located at www.someDomain.com,
connectionName resolves to "someDomain.com:connectionName" (If a SWF file is located on the client computer, the value assigned to superdomain is "localhost".)
Also by default, Flash Player lets the receiving LocalConnection object accept commands only from sending LocalConnection objects whose connection name also resolves into a value of
"superdomain:connectionName" In this way, Flash makes it simple for SWF files located in the same domain to communicate with each other
If you are implementing communication only between SWF files in the same domain, specify a string for connectionName that does not begin with an underscore (_) and that does not specify a domain name (for example, "myDomain:connectionName") Use the same string in the
LocalConnection.connect(connectionName) command
Trang 10410 Chapter 2: ActionScript Language Reference
If you are implementing communication between SWF files in different domains, specifying a string for connectionName that begins with an underscore (_) will make the SWF with the receiving LocalConnection object more portable between domains Here are the two possible cases:
• If the string for connectionName does not begin with an underscore (_), Flash Player adds a prefix with the superdomain and a colon (for example, "myDomain:connectionName") Although this ensures that your connection does not conflict with connections of the same name from other domains, any sending LocalConnection objects must specify this
superdomain (for example, "myDomain:connectionName") If the SWF with the receiving LocalConnection object is moved to another domain, the player changes the prefix to reflect the new superdomain (for example, "anotherDomain:connectionName") All sending LocalConnection objects would have to be manually edited to point to the new superdomain
• If the string for connectionName begins with an underscore (for example,
"_connectionName"), Flash Player does not add a prefix to the string This means that the receiving and sending LocalConnection objects will use identical strings for connectionName
If the receiving object uses LocalConnection.allowDomain to specify that connections from any domain will be accepted, the SWF with the receiving LocalConnection object can be moved to another domain without altering any sending LocalConnection objects
For more information, see the discussion of connectionName in LocalConnection.send() and also the LocalConnection.allowDomain and LocalConnection.domain() entries
Note: Colons are used as special characters to separate the superdomain from the connectionName
string A string for connectionName that contains a colon is not valid.
Example
The following example shows how a SWF file in a particular domain can invoke a method named
Trace in a receiving SWF file in the same domain The receiving SWF file functions as a trace window for the sending SWF file; it contains two methods that other SWF files can call—
Trace and Clear Buttons pressed in the sending SWF files call these methods with
Trang 11LocalConnection.connect() 411
playback_pb.setStyle("themeColor", "haloBlue");
this.createEmptyMovieClip("timer_mc", this.getNextHighestDepth());
var receiving_lc:LocalConnection = new LocalConnection();
receiving_lc.playMP3 = function(mp3Path:String, mp3Name:String) {
play_btn.onRelease = function() {
var sending_lc:LocalConnection = new LocalConnection();
sending_lc.send("lc_name", "playMP3", "song1.mp3", "Album - 01 - Song"); };
SWF 3 contains a button called play_btn When you click the button, it connects to SWF 1 and passes two variables The first variable contains the MP3 file to stream, and the second variable is the filename that you display in the Label component instance in SWF 1
play_btn.onRelease = function() {
var sending_lc:LocalConnection = new LocalConnection();
sending_lc.send("lc_name", "playMP3", "song2.mp3", "Album - 02 - Another Song");
};
See also
LocalConnection.send()
Trang 12412 Chapter 2: ActionScript Language Reference
Method; returns a string representing the domain of the location of the current SWF file
In SWF files published for Flash Player 6, the returned string is the superdomain of the current SWF file For example, if the SWF file is located at www.macromedia.com, this command returns
"macromedia.com"
In SWF files published for Flash Player 7 or later, the returned string is the exact domain of the current SWF file For example, if the SWF file is located at www.macromedia.com, this
command returns "www.macromedia.com"
If the current SWF file is a local file residing on the client computer, this command returns
"localhost"
The most common way to use this command is to include the domain name of the sending LocalConnection object as a parameter to the method you plan to invoke in the receiving LocalConnection object or with LocalConnection.allowDomain to accept commands from a specified domain If you are enabling communication only between LocalConnection objects that are located in the same domain, you probably don’t need to use this command
Example
In the following example, a receiving SWF file accepts commands only from SWF files located in the same domain or at macromedia.com:
// If both the sending and receiving SWF files are Flash Player 6,
// then use the superdomain
var my_lc:LocalConnection = new LocalConnection();
// a different subdomain, e.g livedocs.macromedia.com, will not.
var my_lc:LocalConnection = new LocalConnection();
Trang 13In the following example, a sending SWF file located at www.yourdomain.com invokes a method
in a receiving SWF file located at www.mydomain.com The sending SWF file includes its domain name as a parameter to the method it invokes, so the receiving SWF file can return a reply value to a LocalConnection object in the correct domain The sending SWF file also specifies that it will accept commands only from SWF files at mydomain.com
Line numbers are included for reference purposes The sequence of events is described in the following list:
• The receiving SWF file prepares to receive commands on a connection named "sum" (line 11) The Flash Player resolves the name of this connection to "mydomain.com:sum" (see
LocalConnection.connect())
• The sending SWF file prepares to receive a reply on the LocalConnection object named
"result" (line 67) It also specifies that it will accept commands only from SWF files at mydomain.com (lines 51 to 53)
• The sending SWF file invokes the aSum method of a connection named "mydomain.com:sum"
(line 68) and passes the following parameters: its superdomain, the name of the connection to receive the reply ("result"), and the values to be used by aSum (123 and 456)
• The aSum method (line 6) is invoked with the following values:
sender = "mydomain.com:result", replyMethod = "aResult", n1 = 123, and n2 = 456 It then executes the following line of code:
this.send("mydomain.com:result", "aResult", (123 + 456));
• The aResult method (line 54) shows the value returned by aSum (579)
// The receiving SWF at http://www.mydomain.com/folder/movie.swf
// contains the following code
1 var aLocalConnection:LocalConnection = new LocalConnection();
// The sending SWF at http://www.yourdomain.com/folder/movie.swf
// contains the following code
50 var lc:LocalConnection = new LocalConnection();
51 lc.allowDomain = function(aDomain) {
// Allow connections only from mydomain.com
Trang 14414 Chapter 2: ActionScript Language Reference
52 return (aDomain == "mydomain.com");
53 }
54 lc.aResult = function(aParam) {
55 trace("The sum is " + aParam);
56 }
// determine our domain and see if we need to truncate it
57 var channelDomain:String = lc.domain();
58 if (getVersion() >= 7 && this.getSWFVersion() >= 7)
59 {
// split domain name into elements
60 var domainArray:Array = channelDomain.split(".");
// if more than two elements are found,
// chop off first element to create superdomain
Trang 15sending_lc.onStatus = function(infoObject:Object) : Void{
// your statements here
If the information object returned by this event handler contains a level value of status, Flash successfully sent the command to a receiving LocalConnection object This does not mean that Flash successfully invoked the specified method of the receiving LocalConnection object; it means only that Flash could send the command For example, the method is not invoked if the receiving LocalConnection object doesn’t allow connections from the sending domain or if the method does not exist The only way to know for sure if the method was invoked is to have the receiving object send a reply to the sending object
If the information object returned by this event handler contains a level value of error, Flash cannot send the command to a receiving LocalConnection object, most likely because there is no receiving LocalConnection object connected whose name corresponds to the name specified in the sending_lc.send() command that invoked this handler
In addition to this onStatus handler, Flash also provides a “super” function called
System.onStatus If onStatus is invoked for a particular object and there is no function assigned
to respond to it, Flash processes a function assigned to System.onStatus if it exists
In most cases, you implement this handler only to respond to error conditions, as shown in the following example
Example
The following example displays a status message about whether the SWF file connects to another local connection object called lc_name A TextInput component called name_ti, a TextArea instance called status_ta and a Button instance called send_button are used to display content
var sending_lc:LocalConnection;
var sendListener:Object = new Object();
sendListener.click = function(evt:Object) {
Trang 16416 Chapter 2: ActionScript Language Reference
sending_lc = new LocalConnection();
Trang 17connectionName A string that corresponds to the connection name specified in the
LocalConnection.connect() command that wants to communicate with sending_lc
method A string specifying the name of the method to be invoked in the receiving
LocalConnection object The following method names cause the command to fail: send,
connect, close, domain, onStatus, and allowDomain
p1, pN Optional parameters to be passed to the specified method
Returns
A Boolean value: true if Flash can carry out the request; false otherwise
Note: A return value of true does not necessarily mean that Flash successfully connected to a receiving LocalConnection object; it means only that the command is syntactically correct To determine whether the connection succeeded, see LocalConnection.onStatus
Description
Method; invokes the method named method on a connection opened with the
LocalConnection.connect(connectionName) command (the receiving LocalConnection object) The object used with this command is called the sending LocalConnection object The SWF files that contain the sending and receiving objects must be running on the same client computer
There is a limit to the amount of data you can pass as parameters to this command If the command returns false but your syntax is correct, try dividing the LocalConnection.send()
requests into multiple commands
As discussed in the entry LocalConnection.connect(), Flash adds the current superdomain to
connectionName by default If you are implementing communication between different domains, you need to define connectionName in both the sending and receiving
LocalConnection objects in such a way that Flash does not add the current superdomain to
connectionName You can do this in one of the following two ways:
• Use an underscore (_) at the beginning of connectionName in both the sending and
receiving LocalConnection objects In the SWF file containing the receiving object, use
LocalConnection.allowDomain to specify that connections from any domain will be accepted This implementation lets you store your sending and receiving SWF files in any domain
Trang 18418 Chapter 2: ActionScript Language Reference
• Include the superdomain in connectionName in the sending LocalConnection object—for example, myDomain.com:myConnectionName In the receiving object, use
LocalConnection.allowDomain to specify that connections from the specified superdomain will be accepted (in this case, myDomain.com) or that connections from any domain will
communicating between LocalConnection objects located in specified domains, see
LocalConnection.allowDomain and LocalConnection.domain()
See also
LocalConnection.allowDomain , LocalConnection.connect() ,
LocalConnection.domain() , LocalConnection.onStatus
Trang 19Math class 419
Math class
Availability
Flash Player 5 In Flash Player 4, the methods and properties of the Math class are emulated using
approximations and might not be as accurate as the non-emulated math functions that Flash
Player 5 supports
Description
The Math class is a top-level class whose methods and properties you can use without using
a constructor
Use the methods and properties of this class to access and manipulate mathematical constants and
functions All the properties and methods of the Math class are static and must be called using the
syntax Math.method(parameter) or Math.constant In ActionScript, constants are defined
with the maximum precision of double-precision IEEE-754 floating-point numbers
Several Math class methods use the measure of an angle in radians as a parameter.You can use the
following equation to calculate radian values before calling the method and then provide the
calculated value as the parameter, or you can provide the entire right side of the equation (with
the angle’s measure in degrees in place of degrees) as the radian parameter
To calculate a radian value, use the following formula:
radians = degrees * Math.PI/180
The following is an example of passing the equation as a parameter to calculate the sine of a 45º
angle:
Math.sin(45 * Math.PI/180) is the same as Math.sin(.7854)
Method summary for the Math class
Math.abs() Computes an absolute value.
Math.acos() Computes an arc cosine.
Math.asin() Computes an arc sine.
Math.atan() Computes an arc tangent.
Math.atan2() Computes an angle from the x-axis to the point.
Math.ceil() Rounds a number up to the nearest integer.
Math.cos() Computes a cosine.
Math.exp() Computes an exponential value.
Math.floor() Rounds a number down to the nearest integer.
Math.log() Computes a natural logarithm.
Math.max() Returns the larger of the two integers.
Math.min() Returns the smaller of the two integers.
CHAPTER 2
ActionScript Language Reference
Trang 20420 Chapter 2: ActionScript Language Reference
Property summary for the Math class
All the following properties for the Math class are constants:
Math.pow() Computes x raised to the power of the y.
Math.random() Returns a pseudo-random number between 0.0 and 1.0
Math.round() Rounds to the nearest integer.
Math.sin() Computes a sine.
Math.sqrt() Computes a square root.
Math.tan() Computes a tangent.
Property Description
Math.E Euler’s constant and the base of natural logarithms (approximately 2.718).
Math.LN2 The natural logarithm of 2 (approximately 0.693).
Math.LOG2E The base 2 logarithm of e (approximately 1.442).
Math.LN2 The natural logarithm of 10 (approximately 2.302).
Math.LOG10E The base 10 logarithm of e (approximately 0.434).
Math.PI The ratio of the circumference of a circle to its diameter (approximately 3.14159).
Math.SQRT1_2 The reciprocal of the square root of 1/2 (approximately 0.707).
Math.SQRT2 The square root of 2 (approximately 1.414).
Trang 22422 Chapter 2: ActionScript Language Reference
Math.acos()
Availability
Flash Player 5 In Flash Player 4, the methods and properties of the Math class are emulated using approximations and might not be as accurate as the non-emulated math functions that Flash Player 5 supports
Trang 24424 Chapter 2: ActionScript Language Reference
Math.atan()
Availability
Flash Player 5 In Flash Player 4, the methods and properties of the Math class are emulated using approximations and might not be as accurate as the non-emulated math functions that Flash Player 5 supports
Trang 25Math.atan2(y:Number, x:Number) : Number
Parameters
y A number specifying the y coordinate of the point.
x A number specifying the x coordinate of the point.
Returns
A number
Description
Method; computes and returns the angle of the point y/x in radians, when measured
counterclockwise from a circle’s x axis (where 0,0 represents the center of the circle) The return
value is between positive pi and negative pi
Trang 26426 Chapter 2: ActionScript Language Reference
Math.ceil()
Availability
Flash Player 5 In Flash Player 4, the methods and properties of the Math class are emulated using approximations and might not be as accurate as the non-emulated math functions that Flash Player 5 supports
Trang 28428 Chapter 2: ActionScript Language Reference
Math.E
Availability
Flash Player 5 In Flash Player 4, the methods and properties of the Math class are emulated using approximations and might not be as accurate as the non-emulated math functions that Flash Player 5 supports
var continuouslyCompoundedInterest:Number = (100 * Math.E) - principal;
trace ("Beginning principal: $" + principal);
trace ("Simple interest after one year: $" + simpleInterest);
trace ("Continuously compounded interest after one year: $" +
continuouslyCompoundedInterest);
/*
Output:
Beginning principal: $100
Simple interest after one year: $100
Continuously compounded interest after one year: $171.828182845905
*/
Trang 29Method; returns the value of the base of the natural logarithm (e), to the power of the exponent
specified in the parameter x The constant Math.E can provide the value of e.
Example
The following example displays the logarithm for two number values
trace(Math.exp(1)); // output: 2.71828182845905
trace(Math.exp(2)); // output: 7.38905609893065
Trang 30430 Chapter 2: ActionScript Language Reference
Math.floor()
Availability
Flash Player 5 In Flash Player 4, the methods and properties of the Math class are emulated using approximations and might not be as accurate as the non-emulated math functions that Flash Player 5 supports
Trang 31The following example displays the logarithm for three numerical values.
trace(Math.log(0)); // output: -Infinity
trace(Math.log(1)); // output: 0
trace(Math.log(2)); // output: 0.693147180559945
Trang 32432 Chapter 2: ActionScript Language Reference
Math.LN2
Availability
Flash Player 5 In Flash Player 4, the methods and properties of the Math class are emulated using approximations and might not be as accurate as the non-emulated math functions that Flash Player 5 supports
Trang 34434 Chapter 2: ActionScript Language Reference
Math.LOG2E
Availability
Flash Player 5 In Flash Player 4, the methods and properties of the Math class are emulated using approximations and might not be as accurate as the non-emulated math functions that Flash Player 5 supports
Constant; a mathematical constant for the base-2 logarithm of the constant e (Math.E), expressed
as log2e, with an approximate value of 1.442695040888963387
Example
This example traces the value of Math.LOG2E
trace(Math.LOG2E);
// Output: 1.44269504088896
Trang 36436 Chapter 2: ActionScript Language Reference
Math.max()
Availability
Flash Player 5 In Flash Player 4, the methods and properties of the Math class are emulated using approximations and might not be as accurate as the non-emulated math functions that Flash Player 5 supports
var date1:Date = new Date(2004, 11, 25);
var date2:Date = new Date(2004, 11, 30);
var maxDate:Number = Math.max(date1.getTime(), date2.getTime());
trace(new Date(maxDate).toString());
See Also
Math.min()
Trang 37var date1:Date = new Date(2004, 11, 25);
var date2:Date = new Date(2004, 11, 30);
var minDate:Number = Math.min(date1.getTime(), date2.getTime());
trace(new Date(minDate).toString());
See Also
Math.max()
Trang 38438 Chapter 2: ActionScript Language Reference
Math.PI
Availability
Flash Player 5 In Flash Player 4, the methods and properties of the Math class are emulated using approximations and might not be as accurate as the non-emulated math functions that Flash Player 5 supports
mc.curveTo(Math.tan(Math.PI/8)*r+x, r+y, x, r+y);
mc.curveTo(-Math.tan(Math.PI/8)*r+x, r+y, -Math.sin(Math.PI/4)*r+x,
Math.sin(Math.PI/4)*r+y);
mc.curveTo(-r+x, Math.tan(Math.PI/8)*r+y, -r+x, y);
mc.curveTo(r+x, Math.tan(Math.PI/8)*r+y, Math.sin(Math.PI/4)*r+x, Math.sin(Math.PI/4)*r+y);
-mc.curveTo(-Math.tan(Math.PI/8)*r+x, -r+y, x, -r+y);
mc.curveTo(Math.tan(Math.PI/8)*r+x, r+y, Math.sin(Math.PI/4)*r+x,
-Math.sin(Math.PI/4)*r+y);
mc.curveTo(r+x, -Math.tan(Math.PI/8)*r+y, r+x, y);
}
Trang 39Math.pow(x:Number , y:Number) : Number
Parameters
x A number to be raised to a power
y A number specifying a power the parameter x is raised to
var minY = Math.min(this.origY, this.newY);
var nextDepth:Number = canvas_mc.getNextHighestDepth();
Mouse.addListener(mouseListener);
Trang 40440 Chapter 2: ActionScript Language Reference
Math.random()
Availability
Flash Player 5 In Flash Player 4, the methods and properties of the Math class are emulated using approximations and might not be as accurate as the non-emulated math functions that Flash Player 5 supports
The following example returns a random number between two specified integers
function randRange(min:Number, max:Number):Number {
var randomNum:Number = Math.round(Math.random()*(max-min))+min;