coding and other fun

Flex, Java …and all the rest - by Bernhard Hirschmann

Entries Comments



Accessing FlashVars in ActionScript

17 March, 2008 (14:16) | ActionScript, FlashVars, Flex | By: admin

Using request parameters inside Flex/Flash is a common way to initialize the application during runtime startup. Adobe calls this FlashVars. Accessing those variables inside the code is often a bit confusing. I’d like to provide two examples for different environments, to show how to access the values.

A FlashVar can be set as a request parameter in the URL of the SWF application:

http://www.mydomain.com/apps/myapp.swf?var1=value1&var2=value2

In the upper example we call our “myapp.swf” and set 2 variables “var1″ and “var2″ with the values “value1″ and “value2″.

Accessing FlashVars in a MXML file

In a MXML file you can simply use the static reference “Application” to reach your FlashVars:

var myVar1:String = Application.application.parameters.var1;

Accessing FlashVars in a ActionScript file

Accessing the FlashVars within a AS file can be done using the flash.display.LoaderInfo object, which can be accessed using the DisplayObject root. All FlashVars are located in the object “parameters”:

// try getting the FlashVars
try {
	var myVar1:String = root.loaderInfo.parameters.var1;
	var myVar2:String = root.loaderInfo.parameters.var2;
}
catch (e:Error) {
	trace(e);
}

Write a comment