Search Box

Google
 

Wednesday, February 14, 2007

Cloning is illegal , but not for FLASH OBJECTS..........

Its been really a controversial topic. "CLONING" was a buzz word that started the dawn of the era where Biotechonology was seen by a common man. Ask Michael Crichton and he would tell you scores of information about it in the form of stories. Dolly was the first cloned living being. Then there were protests, controversies and conspiracies following.

Do not worry by reading this and learning to Clone an Object or Array will not end you up in Jail, for cloning in ActionScript is not controversial or understanding it is like a grasping a big conspiracy theory.

But before that let us understand why do we need to clone ??? yeah that was a great question and i see a reason why you should read the rest of the story. The real reason for why cloning can be best understood by the following example :

Example 1 :



//in the main timeline

var num1:Number = 20;

var num2:Number = num1;
trace(num1);//traces 20
num2 = 50;
trace(num1);//traces 20 again;


Did you see what happened?
1)we created a variable num1 with a value 20.
2)Again we created a variable num2 and pointed it to num1.
3)Traced the value of num1 and it was 20
4) changed the value of num2 to 50
5) Traced the value of num1 and it was still 20.

I hope you would have understood what i am meaning by this. Once again consider the following example.

Example 2:



//In the main Timeline

var obj1:Object = new Object();
obj1.name = 'Ashok';
obj1.age = 24;

var obj2:Object = obj1;

trace(obj1.name);//traces Ashok;

obj2.name = 'Srinivasan';
trace(obj1.name);//traces Srinivasan.



So what happened here. We did the same thing that was done in Example 1, but the difference was that first example was dealing with numbers and the second one was dealing with objects. In the first one it was "copying of the value operation" and in the second one it was "pointer operation" meaning it does not create a copy and store in the new variable but instead points the first object "obj1" to the new name "obj2". Coincidentally this is the same case when we do it with Arrays. Luckily we have the "slice" and "splice" methods which can get us a copy so as to leaving the choice to us for creating a copy or pointer for an Array. But there is not a method that gives us the choice of creating copies.

Now how can this be solved ? How can i make a copy for an object and not create a pointer instead. ?

The fundamental principle is to use the "for in " loop method. But how do we clone if there are objects inside object ?

Accidentally when i was searching for ActionScript blogs yesterday i stumbled upon a blog in the name of "Arul's blog" (link:http://www.shockwave-india.com/blog/services/as-highlight2/index.php) and there the blogger guy has mentioned about this problem which made me also to think and give a mention cum workaround solution for this problem.

Using the code in "Arul's Blog" and modifying and reducing the lines where-ever possible i am pasting the code below. Make sure to check it out.




Object.prototype.clone = function ()
{
if (typeof (this) == "object")
{
var to = (this instanceof Array )?[]:{};
for (var i in this)
{
to[i] = (typeof (this[i]) == "object") ? this[i].clone () : this[i];
}
return to;
}
trace ("Warning! Object.clone can not be used on MovieClip or XML objects");
return undefined;
};
Object.prototype.trace2 = function()
{
for(var i in this)
{
trace (i + ' : ' + this[i]);
}
}
ASSetPropFlags (Object.prototype, ["clone"], 1);
ASSetPropFlags (Object.prototype, ["trace2"], 1);
myObject = new Object ();
myObject.name = "Ashok";
myObject.gender = "Male";
myObject.age = 24;
myObject.trace2();
copyObject = myObject.clone ();
copyObject.trace2();



So what does the code do ?? i guess the code was self-explanatory.

Regards,

Ashok Srinivasan.