CallFunction(Request: String): String;
CallFunction(Request: String): String;
Request. An XML string which helps to specify the executing function and its parameters.
The CallFunction method executes the function implemented in the loaded Flash object and returns execution result.
Interaction of flash objects with containers, in which this objects are displayed, is performed in ActionScript by the special ExternalInterface class. To call any function, implemented in ActionScript code, this function must be registered in the ExternalInterface class. Registration is performed by the ExternalInterface.addCallback method.
Specify an XML string as a Request parameter, containing the name and parameter values of this function implemented in special format to call the ActionScript code by the CallFunction method. General type of XML string format looks as follows:
<invoke name="functionName" returntype="xml">
<arguments>
//Individual values of parameters
</arguments>
</invoke>
The "Invoke" Root Node contains two attributes: Name is the name of a function by which it is registered with a help of the ExternalInterface.addCallback method; Returntype is the value of a type returned by the CallFunction method. The "xml" is always used as an attribute value.
Separate parameter values are specified in special format containing information about the data type in addition to the actual value:
| ActionScript class or value | Format | Comments |
| null | <null/> | |
| Logical True | <true/> | |
| Logical False | <false/> | |
| String | <string>Value</string> | |
| Number, int, uint | <number>Value</number> | |
| Array (elements can be of different types) |
<array> <property id="0"> <number>Value</number> </property> <property id="1"> <string>Value</string> </property> ... </array> |
The "property" node determines a single array item and the "id" attribute determines an element index. Index specification starts with zero. |
| Object |
<object> <property id="Property1"> <string>Value</ String> </property> <property id="Property2"> <string>Value</String> </property> ... </object> |
The "property" node determines separate object properties and the "id" attribute determines names of this properties. |
| Other embedded or user classes |
<null/> or <object> </object> |
ActionScript encodes other objects as the null value or as empty objects. Any property values can be lost in any case. |
An XML string contains execution results of flash object specified function, that is a result of the CallFunction method work. This string also contains an information about the data type in addition to the actual value.
Executing the example requires a form, the FlashBox component with the FlashBox1 name located on it and two buttons named Button1 and Button2. An swf file which is loaded to the FlashBox1, contains the following code:
//...
function Run(): void
{
//...Function code in ActionScript.
}
function TestFunc(i1,i2: int): Number
{
var i3: Number = i1 * i2 / (i1 + i2);
return i3;
}
ExternalInterface.addCallback("TestRun", Run); //The function is available with the TestRun name in containers for the flash object
ExternalInterface.addCallback("TestFunc", TestFunc); /The function is available with the TestFunc name in containers for the flash object
//...
Implement two procedures to process an event of OnClick buttons to launch specified functions when executing a form:
Sub Button1OnClick(Sender: Object; Args: IMouseEventArgs);
Var
Request: String;
Begin
Request := ("<invoke name=""TestRun"" returntype=""xml""/>");
FlashBox1.CallFunction(Request);
End Sub Button1OnClick;
Sub Button2OnClick(Sender: Object; Args: IMouseEventArgs);
Var
Request, Result: String;
Begin
Request := "<invoke name=""TestFunc"" returntype=""xml"">" +
"<arguments>" +
"<number>2</number>" +
"<number>6</number>" +
"</arguments>" +
"</invoke>";
Result := FlashBox1.CallFunction(Request);
End Sub Button2OnClick;
The Run flash object function is performed by clicking the ferst button. This function has no parameters so the "arguments" node in the passed XML string is not specified.
The TestFunc flash object function is performed by clicking the second button. The values 2 and 6 will be specified as parameter values for this function. The result of this function performance is the "1,5" value. The CallFunction method returns this value represented as an XML string: <number>1.5</number>.
Executing the example requires a form, the FlashBoxNet component with the FlashBoxNet1 name located on it and two buttons named Button1 and Button2. An swf file containing the code, specified in the Fore Example above, is loaded to the FlashBoxNet1.
Implement two procedures to process an event of Click buttons to launch flash object functions when executing a form:
Private Sub button1_Click(sender: System.Object; e: System.EventArgs);
Var
Request: String;
Begin
Request := ("<invoke name=""TestRun"" returntype=""xml""/>");
FlashBoxNet1.CallFunction(Request);
End Sub;
Private Sub button2_Click(sender: System.Object; e: System.EventArgs);
Var
Request, Result: String;
Begin
Request := "<invoke name=""TestFunc"" returntype=""xml"">" +
"<arguments>" +
"<number>2</number>" +
"<number>6</number>" +
"</arguments>" +
"</invoke>";
Result := FlashBoxNet1.CallFunction(Request);
End Sub;
The Run flash object function is performed by clicking the ferst button. This function has no parameters so the "arguments" node in the passed XML string is not specified.
The TestFunc flash object function is performed by clicking the second button. The values 2 and 6 will be specified as parameter values for this function. The result of this function performance is the "1,5" value. The CallFunction method returns this value represented as an XML string: <number>1.5</number>.
See also: