We have seen
how to pass some values from Flash CS3 to PHP that allows us to maintain in communication our Flash applications with the server.
We often need to assign to a SWF a value in entry using the HTML of the page in which the SWF itself is inserted.
With Flash 8 all it took, was defining a variable on the timeline and assigning it the value through the HTML.
With Flash CS3, things have a bit changed and we need to work with the LoaderInfo class. So let us follow the next example...
In this example, I use Adobe Dreamweaver to insert a SWF in a HTML page and to pass a value to the SWF.
I create a FLA and save it as "main.fla".
I create an HTML page with Dreamwearver and save it as "main.html".
I add "main.swf" to the HTML page "main.html".
I select the SWF and I open the Parameters Panel as shown in the following pic:
as we can be notice, I insert as a parameter name "flashvars" (in this way Flash knows that there is a parameter - let us see it this way), otherwise it does not work.
In the field Value (Valore), I insert "id=321". "id" will be the name of the variable that Flash will look for and 321 is its value.
Back to "main.fla", I assign a Document Class, an AS file saved as "Main.as", implemented the following way:
Code:
package
{
import flash.display.MovieClip;
import flash.text.TextField;
import flash.display.LoaderInfo;
public class Main extends MovieClip
{
private var id:String;
public function Main()
{
getHTMLvars();
}
private function getHTMLvars():void
{
var value:String;
var obj:Object=LoaderInfo(root.loaderInfo).parameters;
for (value in obj)
{
id=String(obj[value]);
try_txt.text=id;
}
}
}
}
In brief, I told Flash to retrieve the value of "id" passed by the HTML.
Let us analyse the code.
Properties
a String variable with the same name (we are not obliged to do so) as the variable passed by the HTML
private var id:String;
Methods
getHTMLvars();
this methods implements the logic to retrieve the variable "id" from the HTML
a String variable used in a "for in" cycle
var value:String;
an Object variable to which I assign the property "parameters" of LoaderInfo from the "main.fla""s root
var obj:Object=LoaderInfo(root.loaderInfo).parameters;
with a "for in" cycle, I check if there is a String in the object "obj"
for (value in obj)
{
I assign to the property "id" a value as a "String" type equal to a fake property retrieved from "obj" (we could also see it as "obj.value" but Flash CS3 would not accept it)
id=String(obj[value]);
I assign the value of the property "id" as a text of a text field so to be sure that Flash sees that variable passed by the HTML
try_txt.text=id;
}
Clearly, I added the text field only to demonstrate this example. Once, the value of the variable "id" is retrieved from the HTML and is assigned to the property "id" of "Main.as",
we can use that value as wanted all through our Flash application.
Stay tuned!