LocalConnection class 401
Event handler summary for the LocalConnection class
Constructor for the LocalConnection class
Availability
Flash Player 6.
Usage
new LocalConnection() : LocalConnection
Parameters
None.
Returns
A reference to a LocalConnection object.
Description
Constructor; creates a LocalConnection object.
Example
The following example shows how receiving and sending SWF files create LocalConnnection
objects. The two SWF files can use the same name or different names for their respective
LocalConnection objects. In this example they use different names.
// 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);
Event handler Description
LocalConnection.allowDomain
Event handler; invoked whenever
receiving_lc
receives a request to invoke a method from a
sending LocalConnection object. Flash expects the code you implement in this handler to return
a Boolean value of
true
or
false
. If the handler doesn’t return
true
, the request from the
sending object is ignored, and the method is not invoked.
When this event handler is absent, Flash Player applies a default security policy, which is
equivalent to the following code:
my_lc.allowDomain = function (sendingDomain)
{
return (sendingDomain == this.domain());
}
Use
LocalConnection.allowDomain
to explicitly permit LocalConnection objects from
specified domains, or from any domain, to execute methods of the receiving LocalConnection
object. If you don’t declare the
sendingDomain
parameter, you probably want to accept
commands from any domain, and the code in your handler would be simply
return true
. If you
do declare
sendingDomain
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
used to display content.
var sending_lc:LocalConnection;
var sendListener:Object = new Object();
sendListener.click = function(evt:Object) {
sending_lc = new LocalConnection();
sending_lc.onStatus = function(infoObject:Object) {
switch (infoObject.level) {
case 'status' :
status_ta.text = "LocalConnection connected successfully.";
break;
case 'error' :
status_ta.text = "LocalConnection encountered an error.";
break;
}
};
sending_lc.send("_mylc", "sayHello", name_ti.text);
};
send_button.addEventListener("click", sendListener);
In the following example, the receiving SWF file, which resides in thisDomain.com, accepts
commands only from SWF files located in thisDomain.com or thatDomain.com:
var aLocalConn:LocalConnection = new LocalConnection();
aLocalConn.Trace = function(aString) {
aTextField += aString+newline;
};
aLocalConn.allowDomain = function(sendingDomain) {
return (sendingDomain == this.domain() || sendingDomain ==
"www.macromedia.com");
};
aLocalConn.connect("_mylc");
When published for Flash Player 7 or later, exact domain matching is used. This means that the
. If the handler
doesn’t return
true
, the request from the sending object is ignored, and the method is
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");
LocalConnection.allowInsecureDomain 407
};
my_lc.allowInsecureDomain = function(sendingDomain:String) {
component instance called
close_button
:
this.createTextField("welcome_txt", this.getNextHighestDepth(), 10, 10, 100,
22);
this.createTextField("status_txt", this.getNextHighestDepth(), 10, 42,
100,44);
var receiving_lc:LocalConnection = new LocalConnection();
receiving_lc.sayHello = function(name:String) {
welcome_txt.text = "Hello, "+name;
};
receiving_lc.connect("lc_name");
var closeListener:Object = new Object();
closeListener.click = function(evt:Object) {
receiving_lc.close();
status_txt.text = "connection closed";
};
close_button.addEventListener("click", closeListener);
See also
LocalConnection.connect()
LocalConnection.connect() 409
LocalConnection.connect()
Availability
Flash Player 6.
Usage
receiving_lc.connect(connectionName:String) : Boolean
Parameters
connectionName
A string that corresponds to the connection name specified in the
LocalConnection.send()
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.
410 Chapter 2: ActionScript Language Reference
If you are implementing communication between SWF files in different domains, specifying a
string for
connectionName
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
specified parameters.
// Receiving SWF
var aLocalConnection:LocalConnection = new LocalConnection();
aLocalConnection.Trace = function(aString):String{
aTextField += aString + newline;
}
aLocalConnection.Clear = function(){
};
my_sound.loadSound(mp3Path, true);
};
receiving_lc.connect("lc_name");
SWF 2 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", "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()
412 Chapter 2: ActionScript Language Reference
LocalConnection.domain()
Availability
Flash Player 6; behavior changed in Flash Player 7.
Usage
var my_lc:LocalConnection = new LocalConnection();
my_lc.allowDomain = function(sendingDomain):String{
return (sendingDomain==this.domain() || sendingDomain=="macromedia.com");
}
// If either the sending or receiving SWF file is Flash Player 7 or later,
// then use the exact domain. In this case, commands from a SWF file posted
// at www.macromedia.com will be accepted, but those from one posted at
// a different subdomain, e.g. livedocs.macromedia.com, will not.
var my_lc:LocalConnection = new LocalConnection();
LocalConnection.domain() 413
my_lc.allowDomain = function(sendingDomain):String{
return (sendingDomain==this.domain() ||
sendingDomain=="www.macromedia.com");
}
In 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()).
•
= 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();
2 aLocalConnection.allowDomain = function()
3 {
// Allow connections from any domain
4 return true;
5 }
6 aLocalConnection.aSum = function(sender, replyMethod, n1, n2)
7{
8 this.send(sender, replyMethod, (n1 + n2));
9}
10
11 aLocalConnection.connect("sum");
// 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
414 Chapter 2: ActionScript Language Reference
52 return (aDomain == "mydomain.com");
}
Parameters
infoObject
A parameter defined according to the status message. For details about this
parameter, see the Description section.
Returns
Nothing.
Description
Event handler; invoked after a sending LocalConnection object tries to send a command to a
receiving LocalConnection object. If you want to respond to this event handler, you must create a
function to process the information object sent by the LocalConnection object.
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.onStatus = function(infoObject:Object) {
switch (infoObject.level) {
case 'status' :
status_ta.text = "LocalConnection connected successfully.";
break;
case 'error' :
status_ta.text = "LocalConnection encountered an error.";
break;
}
};
sending_lc.send("lc_name", "sayHello", name_ti.text);
};
send_button.addEventListener("click", sendListener);
See also
LocalConnection.send(), System.onStatus
LocalConnection.send() 417
LocalConnection.send()
Availability
Flash Player 6.
Usage
sending_lc.send (connectionName:String, method:String [, p1,...,pN]) : Boolean
Parameters
connectionName
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
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.
418 Chapter 2: ActionScript Language Reference
•
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
Method Description
Math.abs()
Computes an absolute value.
Math.acos()
Computes an arc cosine.
.
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).
Method Description
Math.abs() 421
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.
Usage
Math.acos(x:Number) : Number
Parameters
x
A number from -1.0 to 1.0.
Returns
A number; the arc cosine of the parameter
x
.
Description
Method; computes and returns the arc cosine of the number specified in the parameter
x
,
in radians.
Example
The following example displays the arc cosine for several values.
trace(Math.acos(-1)); // output: 3.14159265358979
trace(Math.acos(0)); // output: 1.5707963267949
trace(Math.acos(1)); // output: 0
See also
Math.asin(), Math.atan(), Math.atan2(), Math.cos(), Math.sin(), Math.tan()
Math.asin() 423
Math.asin()
Availability
Flash Player 5. In Flash Player 4, the methods and properties of the Math class are emulated using
tangent
A number that represents the tangent of an angle.
Returns
A number between negative pi divided by 2 and positive pi divided by 2.
Description
Method; computes and returns the value, in radians, of the angle whose tangent is specified in the
parameter
tangent
. The return value is between negative pi divided by 2 and positive pi divided
by 2.
Example
The following example displays the angle value for several tangents.
trace(Math.atan(-1)); // output: -0.785398163397448
trace(Math.atan(0)); // output: 0
trace(Math.atan(1)); // output: 0.785398163397448
See also
Math.acos(), Math.asin(), Math.atan2(), Math.cos(), Math.sin(), Math.tan()
Math.atan2() 425
Math.atan2()
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.
Usage
Math.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.