
Chapter 452
For example, the code below shows how you could create a network connection on the client side,
and then make a call to it from the server side:
// This is client-side ActionScript in the FLA
my_nc = new NetConnection();
my_nc.someClientMethod = function()
{
// code here
}
// This is server-side ActionScript in the main.asc file
clientObj.call("someClientMethod");
The argument passed to clientObj.call must be a previously defined method of the client-side
NetConnection object. This is because any method in the client code that may be called by the
server must be a property of a client-side NetConnection object.
In contrast, suppose you use the
call method from the client, to call a method on the server side:
// This is client-side ActionScript in the FLA
NetConnection.call("someServerMethod");
// This is server-side ActionScript in the main.asc file
client.prototype.someServerMethod = function()
{
// code here
}
// The following code would also work
onConnect(newClient)
{
newClient.someServerMethod = function()
{
// code
}
}
In this case, the argument passed to NetConnection.call must be a method of the server-side
Client object that was previously defined in the main.asc file. This is because any method in the
server code that may be called by the client must be a property of a server-side Client object.
Using multiple files
As your application increases in complexity, don’t try to use a single script to do everything: break
your functionality up into multiple scripts, each of which provides a specific set of functionality.
If you do use more than one file, you can optimize performance by using a “once-only inclusion”
definition to prevent a file from being evaluated more than once.
On the client side
For example, on the client side, if you want a file named my_file.as to be available to other files in
your application, include the following code in my_file.as:
if (_root._my_file_as == null) {
_root._my_file_as = true;
// All the code for myfile.as goes here
}
Then, in your other files, issue the following command:
#include "my_file.as";
Komentarze do niniejszej Instrukcji