2 Replies Latest reply on Mar 10, 2011 6:10 PM by Nuwan,Chamara

    Dynamic objects

    Maximist Level 1

      Hallo. I have created 2 objects: myObjectOne and myObjectTwo. Now I have an object which can use only 1 of those 2 objects depending from a variable in setData. if this var which is named objectType is "One", i will create the first object. if this var is "Two", i will create the second object.

       

      What i want to do is:

       

      var myObject:myObjectOne = new myObjectOne();   // but only if objectType= "One", so i tried:

       

      var myObject:myObject[objectType] = new myObject[objectType]();    // but this doesn't work

       

      What i want to do is to replace One in the word myObjectOne with my var (objectType) but i don't know how to do it.

       

      I hope you understood.

       

      Thx

       

      Max

        • 1. Re: Dynamic objects
          UbuntuPenguin Level 4

          Why don't you just make the objects and pass them to a function.  Observe...

           

          private function useDifferentObjects( object:Object ):void

          {

            if( object is InstanceOfObjectOne )

            {

                 ///...

            }

            else

            {

               ////....

            }

          }

          • 2. Re: Dynamic objects
            Nuwan,Chamara Level 1

            Hi,

             

            I think instead of replacing the "objectType" property, you can delete that property using

            delete keyword and then add the new property "One" to the object.

             

            if(objectType == "One"){

                 var myObject:myObjectOne = new myObjectOne(); 

                 var objectTypePropVal:Object = myObject["objectType"];

                 delete myObject.objectType;

                 myObject["One"] = myObject["objectTypePropVal"];

            }