5 Replies Latest reply on Sep 30, 2011 12:48 PM by kglad

    error #1007 - attempting to instantiate a non-constructor

    birnerseff Level 2

      I am receiving said message on an actionscript-only project. I tried to reduce the application to a nonsensical example:

      package

      {       import flash.display.Sprite;

             

              public class zz extends Sprite

              {

                      public function zz()

                      {      

                              var dummies:Array = [];

                              dummies.push(new dummy());

                              dummies.push(new dummy());

                     

                              var newdata:Array = dummy.mixup(dummies);

                      }

              }

      }

       

      package

      {

              import flash.geom.Point;

       

              public dynamic class dummy extends Array

              {

                      public function addp(pt:Point):void

                      {

                              this.push(pt.clone());

                      }

                     

                      public static function mixup(dummies:Array):Array

                      {

                              var ret:Array = [];

                              function local_makeresult():void

                              {       for(var n:int = 0 ; n < 3 ; n++)

                                      {       var b:dummy = new dummy();

                                              ret.push(b);

                                      }

                              }

                              local_makeresult();

                              for(var m:int = 0 ; m < dummies.length ; m++)

                              {       for(var n:int = 0 ; n < 3 ; n++)

                                      {       var i:int = int(dummies[m].length * Math.random());

                                              var j:int = int(3 * Math.random());

                                              ret[j].push(dummies[m][i]);

                                      }

                              }

                              return ret;

                      }

              }

      }

       

      So the problem class

      a) extends array

      b) includes a static utility function (converting an array of class objects into another one)

      c) that utility function includes nested functions

       

      Tge error message looks like this

      TypeError: Error #1007: Instantiation attempted on a non-constructor.

          at dummy/mixup/local_makeresult()

          at dummy$/mixup()


      Does this mean we cannot use nested functions inside static ones?

        • 1. Re: error #1007 - attempting to instantiate a non-constructor
          kglad Adobe Community Professional & MVP

          whoa, is that a mess.  the only surprise is that you get such a benign error message with such egregious code.

           

          first, never nest named functions.  second, what the heck are you trying to do?

          • 2. Re: error #1007 - attempting to instantiate a non-constructor
            birnerseff Level 2

            Hi

             

            as I said this is not actual code (several hundred lines) but just a contrived example modelling the application structure. While it is still something different, lets assume that the dummy class represents polygons (so it derives from array and would implement useful methods like finding total length, area, center of gravity).

            The static function could be intersection

             

            I am using quite a few nested functions in this project - they have access to local variables of the enclosing function. Without the m I wouldhave to pass in a handful of variables (or an object grouping them together)

             

            I have already noticed that nested functions are a mixed blessing, because there are fewer compile-time checks (such as number and type of arguments). However, they exist, so why shouldn't I use them?

             

            One of the things I stumbled over: when I got the error, I tried one thing ... that did make the code work in the end: I removed the static attribute from the function and changed the main class to call it as a method of the first element of the array (which is certainly making it unreadable)

            • 3. Re: error #1007 - attempting to instantiate a non-constructor
              kglad Adobe Community Professional & MVP

              nested named functions don't work.  and, i can't help you with abstracted code.  it's too frustrating on my part.

               

              someone else may be willing and able to help you, if you're patient.

              1 person found this helpful
              • 4. Re: error #1007 - attempting to instantiate a non-constructor
                birnerseff Level 2

                Hi,

                 

                >> nested named functions don't work

                it is a bit disappointing to find that, after writing some 2000 lines that make heavy use of a feature. Is that some knowledge buried inside a manual, or is it just common experiece by others who tried it before?

                 

                Thanks for replying - even if it is somewhat bad news

                • 5. Re: error #1007 - attempting to instantiate a non-constructor
                  kglad Adobe Community Professional & MVP

                  i don't have much formal training so almost all my knowledge is trial and error and reading the flash help files.  but it doesn't take much to see that flash handles named functions in a, perhaps, non-intuitive way.  for example:

                   

                  function a(){

                      trace("a");

                      function b(){

                          trace("b");

                      }

                  }

                   

                  a();

                  b();

                   

                  //  b() is undefined.  the compiler should complain if you're using strict mode (which you should be using).

                   

                   

                  also, if you define a named function on a movieclip frame (that may eventually or may not ever play), that function will be defined as soon as the movieclip exists.  eg, you can define a named function on frame 200 of movieclip mc and, on the frame frame where mc is defined, mc.thatfunction() will work before frame 200 plays (and frame 200 need not ever play).