4 Replies Latest reply on Dec 31, 2006 8:22 PM by buabco

    God Help me! - Creating Objects

    Achernar6
      Ok...

      So i want to create a simple object that contains variables and some methods and I would like to make multiple copies of this object. This is easy to in Javascript:

      -----------------------------------------------------

      var myObjInstance = new myObj();

      function myObj(){
      // variables
      this.v = 0;

      // methods
      this.myMethod = myMethod;

      function myMethod(){
      etc etc...
      }
      }

      -------------------------------------------------------

      So how do I do the above in actionscript?

      I have tried everything. I have read in the manual that global functions can be used as constructors but when I try the following

      var myObjInstance = new myObj();
      function myObj() {
      this.v = 0;
      }

      -------------------------------------------------------------

      Yer when I try it in Flex2 I get this. 1048 Method cannot be used as a constructor.

      The manual says I can do this?? what gives?
        • 1. Re: God Help me! - Creating Objects
          ntsiii Level 3
          Don't use the same name for Classes, functions and variables. The compiler gets confused.

          Also, when you are doing this.v = 0; what are you trying to accomplish? that syntax will attempt to assign the value 0 to an instance(global) variable named "v" which must already exist. "this" is a reference to the component that hosts the script.

          If you want to dynamically add members to an instance of an Object, use bracket notation:
          myObjInstance["myVariableName"] = 0;

          Tracy
          • 2. Re: God Help me! - Creating Objects
            Achernar6 Level 1
            Thanks for the answer. Not sure if we are on the same page or not but just to clarify...

            I understand from your example how to add members to an instance. Can I use this same way to add methods to the instance.?

            If you could give me a full example of creating the object, adding the "members" and methods.

            This me help me explain. I am trying to have both value and methods in a single object which I can make copies of. I want these object to be public and assessable anywhere.

            here is the same question asked about javascript. I can seem the find the answer for flex
            http://www.irt.org/script/204.htm

            Thanks for helping
            • 3. Re: God Help me! - Creating Objects
              _Albertx_ Level 1
              Hi Ancherman.

              Well, as i understand what you are trying to do is an object instace of a class made in actionscript 1.0 mode. Because I remember that in AS1 we make the classes in that way.
              Defining variables is easy, as you typed here, you must only type "this.v = myValue".

              But defining methods is a little diferent. In AS2 we use the propotype keyword or we can create a class in an external file.

              Now in AS3 I think the only way to do that is by an external class. You should create a public class and make some objects which must be instances of that class.
              • 4. Re: God Help me! - Creating Objects
                buabco Level 1
                Hi Achernan,

                In theory if you are defining a specific object the best way to do this is through a class, since this way you'll be able to have a better control of its methods, inheritance and all the fun part of object orietned programing, but still you'll need some times to clone dinamically created objects. In my case I needed to clone movieclips from a loaded clip, and for your case it's the same.

                The code should go something like this:

                var myObject:Object = {var1: 0, var2:25, var3: 50};
                var myObjectClass:Class = myObject["constructor"];

                var myObjectCopy:Object = new myObjectClass();

                Hope it works for you as it worked form me :)