Kun Janos around Adobe’s <Flex> and <Flash>

about me, my life and my work

Archive for the '<Flash>' Category


Cloning Arrays and Cutoms Classes in AS3

Posted by admin on 18th June 2009

Cloning an array using concat will not clone the Objects contained by the array. Cloning with concat and then modifying one of the object will be modified in both arrays because the array contains references to the same object. That’s why we need to do a deep copy of the array, like in the following function.

 
        /**
         * will do a deep copy of the Object
         */
        public static function deepClone(source:Object):*{
            var myBA:ByteArray = new ByteArray();
            myBA.writeObject(source);
            myBA.position = 0;
            return(myBA.readObject());
        }

The above function will clone the Array and all the objects that are contained, however it has a major disadvantage. Custom Objects contained in the cloned array will lose their class definition and array[i] as CustomClassVO will give null. This is hapening because during the deep copy the objects loose their class association. To prevent this, the following line(s) has to be added just before doing the deep copy:

 
import flash.net.registerClassAlias;
 
registerClassAlias("com.client.vo.CustomClassVO", CustomClassVO);

Now, there is a more easy way to handle deep copy without loosing the custom class definition, just add the following line just before your custom class definition

	[RemoteClass] /*this will help cloning the item and retaining class information*/
	public class CustomClassVO{
 
		public var variableName:String = '';
 
		public var variableValue:Object;
 
		public function CustomClassVO(pVariableName:String = '', pVariableValue:Object = null){
			variableName = pVariableName;
			variableValue = pVariableValue;
		}
	}

Noticed the [RemoteClass] ? It does all the job, now copying an array of CustomClassVO’s using the deepClone() is simple.

Posted in <Flash>, <Flex> | No Comments »

Reading and Writing Local Files in Flash Player 10

Posted by admin on 22nd October 2008

Flash player prior to version 10 was unable to directly read and write data/files from and to the local drives. We could browse for files, upload/download them, but only by using server side script(we are talking about flex applications, not AIR ones). Adobe just made our life a little bit easier, by adding the load() and save() methods to the FileReference class.

Points to keep in mind:

- the location files are not exposed to ActionScript
- we can call the load() and save() APIs only on user action(such as a mouse click)
- The APIs are asynchronous (non-blocking)

You can read more about this on Mike Chambers blog, there are also sample applications to review.

Posted in <Flash>, <Flex> | No Comments »