4 Replies Latest reply on Mar 16, 2007 9:54 AM by mclem

    having problems with a object

    numerical07
      function wassup(emotions){
      this.feelings = "Shut up";

      this.talk = function(){
      this.say = function(){
      trace("wassup");
      }
      }
      }
      person = new wassup();
      hi = person.talk();
      hi.say();

      could someone tell me whats wrong with this . all i want to do is recieve a method within a method and I cant :(
        • 1. Re: having problems with a object
          mclem
          I would use a class file to describe the person. Class files are a great way to encapsulate variables and functions that refer to an object. The current way you are doing it is very confusing. A much clearer representation is the class i've included below.

          With class files you need to name the AS file the same thing as the class so if the class is called person the file should be person.as there are many good tutorials and information about creating class files, like here, here and here

          in your swf all you need to do is import the person.as file using the import keyword: import person; then create a new instance of person: var myPerson = new person(); Now that you've created a new person you can now access it's functions. myPerson.talk("hi"); or myPerson.talk("feelings");

          so all together the root code would look something like this:
          import person;
          var myPerson:person = new person();
          myPerson.talk("hi");
          myPerson.talk("feelings");

          and here is the code for the person class.
          • 2. Re: having problems with a object
            numerical07 Level 1
            wow the tutorials was great. But One question. Whats the difference in setting up a function like this...

            varName = function(){
            //....string
            }

            and like this

            function varName(){
            //...string
            }
            • 3. Re: having problems with a object
              Charles Parcell Level 1
              I like the class approach myself.

              But to answer your question...

              The second function statement is a function declaration. The first is a method used to inject a function into an existing object.

              Well, that is not 100% true about the first version you have. It is actually a hold over from previous ECMA specifications.

              I personally would stay away from the first version you have listed as it breaks OOP and is being phased out.
              • 4. Re: having problems with a object
                mclem Level 1
                I agree with Charles about not using the first method. OOP is the way to go.