coding and other fun

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

Entries Comments



Cloning an object in ActionScript

15 April, 2008 (10:19) | ActionScript | By: admin

If you too asked yourself “how can I clone an object in AS?” - fortunately it is as simple as in other programming languages. Again, the challenge is to find the right utility class for it.

Just use ObjectUtil.copy:

var clone:Object = ObjectUtil.copy(originalObject) as Object;

Thats about it.

But remember Adobes documentation about this function: “This method is designed for copying data objects, such as elements of a collection. It is not intended for copying a UIComponent object, such as a TextInput control. If you want to create copies of specific UIComponent objects, you can create a subclass of the component and implement a clone() method, or other method to perform the copy.”

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);
}