Question

I've an object contain all types of data

var obj1:Object={boo:true,num:0,str:"me",arr:[0,"me2",[0,1]],mc:myMc,obj:{boo:false,num:0,str:"me3",arr:[0,"me4",[0,1]]}};

when I clone this object to obj2 using ByteArray with writeObject(obj1)& readObject()properties, everything is fine except obj2.mc (mc is a variable that hold the linkage of a movie clip in the library whose class is set to myMc) can not be added to stage,

addChild(new obj2.mc)
//TypeError: Error #1007: Instantiation attempted on a non-constructor.

Help please!!

Was it helpful?

Solution 2

Guys After 2 hours of experimenting, I came out with this, This is simply the perfect solution... I changed to another way than using ByteArray, which is writing each variable in obj2 to its corresponding value in obj1, but faced another problem: when I change arrays in obj2, I found that arrays in obj1 is changed too, this means when writing variables of obj2, arrays were only referenced to that of obj1.. so I had to loop through arrays to write each value in an array individually..

var obj1:Object={mc:myMc,bo:true,num:0,str:"me",arr:[myMc,true,0,"me2",[0,1,[0,1]]],obj:{mc:myMc,bo:false,num:0,str:"me3",arr:[myMc,true,0,"me4",[0,1]]}};
var obj2:Object=clone(obj1);

function clone( source:Object ):* {
    var myOBJ:Object=new Object();
    for (var property in source) {
        if (source[property] is Array) {
            myOBJ[property]=clone(source[property]);
        } else {
            myOBJ[property]=source[property];
        }
    }
    return (myOBJ);
}

Thanks for all, you've inspired me..

OTHER TIPS

You cannot duplicate movie clips that easy. It should be something similar to:

var objectClass:Class = Object(obj2.mc).constructor;
var instance:MovieClip = new objectClass() as MovieClip;

It would be much easier if you pass a Class rather than instance. In your case, that should be myMc - the class name of the object that is in the library, right?

If so, you can simply instantiate it directly: var instance:MovieClip = new myMc();

The important think to remember is that you don't need to hold reference to an instance, but the class instead!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top