2 Replies Latest reply on Jul 21, 2006 9:34 AM by injpix

    new instances are not created from class

    manslyavite
      Hello,
      I have created a class called surverySlide in which I have linked to a bunch of slides. each slide registers how many questions it has to the class (itself). The problem is I need each movieclip to be its own instance of SurveySlide ( I have a surveyManager class built to deal with each. I am using attachmovie to show them in sequence on the stage (loading one after the other). I assumed by linking each movie clip to the class a new instance would be created each time but only one instance is created. I.e. the questions are put into an array that should be only for that instance but everytime a new instance is loaded it is added on to the array.

      I don't know if I am explaining this clear enough so.....

      in each instance of the class (which is a movie clip) I will register the question to that slide...

      i.e.

      slide instance 1

      this.registerQuestion(1);
      this.registerQuestion(2);

      //if I output the array here I get 1,2

      slide instance 2

      this.registerQuestion(3);

      //if I output the array here I get 1,2,3
      //I want it to only output 3

      Does anyone know off hand why this is happening?

        • 1. new instances are not created from class
          manslyavite Level 1
          I resolved it by redelcaring the array in the contructor. Silly me
          • 2. Re: new instances are not created from class
            injpix Level 3
            To clarify…I think you may be thinking that by declaring a variable;

            private var myArray:Array;

            …like this it creates an instance of an Array, however it doesn’t. Copy and paste the following code in a main timeline;

            var myArray:Array;
            trace(myArray);

            It comes up as “undefined”. Its no different then this;

            var myNumber:Number;
            trace(myNumber);

            Its which, you haven’t assigned it any value. Now if you do this;

            var myArray:Array = new Array();
            trace(myArray);//returns nothing

            ...there is now an assignment operator ( = ). The expression on the right is now stored into the expression on the left. Without operator it is “undefined” as we have seen. This now gives the variable a value. We can do the following, which I believe you have discovered;

            private var myArray:Array;

            //somewhere else in the class
            this.myArray = new Array();
            trace(myArray);//returns nothing

            …and this also works because we now give it a value which we can work with.

            Also note when using an Array use the Debugger for troubleshooting. The Debugger will show me the instance name of the array, its elements and its elements attributes.