6 Replies Latest reply on Mar 9, 2007 3:22 AM by Newsgroup_User

    Class constructor won't call function

    asidfree
      Hello,

      I attached a pared down version of my .as file, which is imported into my flash (.fla) file. I can't seem to call the initCircularArray function from my class constructor. The trace function in the initCircularArray never gets called, and the onLoad(success) trace returns 'true'. Any idea what I might be doing wrong?

      Thanks,
      Dave

        • 1. Re: Class constructor won't call function
          Level 7
          The method falls out of scope in the XML object. Here's one way to handle
          it:


          class CircularArray {
          var circularArray:Array = new Array();
          public function CircularArray(inXML:String) {
          var newXML = new XML(); //the XML file

          newXML.OWNER = this; // give the XML object a reference to this
          class

          newXML.ignoreWhite = true;
          newXML.onLoad = function(success:Boolean) {
          if(success) {
          trace("XML loaded: " + success);

          this.OWNER.initCircularArray(this); // call the referenced
          object's method
          }
          }
          newXML.load(inXML);

          }
          public function initCircularArray(newXML:XML) {
          trace("\n\nGOT HERE\n\n");
          }
          }


          • 2. Re: Class constructor won't call function
            asidfree Level 1
            Thank you Raymond,

            That worked wonderfully. I'm not sure I've seen OWNER before. Would you be able to briefly explain what those two lines you added do, or where I could find that sort of information to learn more? I look to the Actionscript Language Reference a lot, and it didn't have much (that I could find) about it.

            Thanks again,

            David
            • 3. Re: Class constructor won't call function
              Level 7
              You need some way of referring to the CircularArray instance from within the
              XML object.
              The XML object is dynamic, so you can add properties at runtime. I added an
              OWNER field -- but it could be anything that doesn't conflict with reserved
              words or existing properties.



              public function CircularArray(inXML:String) {
              // here you create the XML object
              var newXML = new XML();

              // the XML object doesn't know this CircularArray instance
              // from Harry, so you need to add a callback reference
              // that you can use when the onLoad is fired
              newXML.OWNER = this;
              newXML.ANY_OTHER_FIELD_NAME = this;
              newXML.ignoreWhite = true;

              newXML.onLoad = function(success:Boolean) {
              if(success) {
              trace("XML loaded: " + success);

              // initCircularArray is out of scope
              // but we have a callback reference we
              // can use : OWNER
              this.OWNER.initCircularArray(this);

              // ... or we could use
              // this.ANY_OTHER_FIELD_NAME.initCircularArray(this);
              }
              }

              newXML.load(inXML);

              }


              • 5. Re: Class constructor won't call function
                asidfree Level 1
                Thanks, I've looked over the scope link you sent. I appreciate the reference. I don't seem to be getting the scope right again, and I thought I would ask while I search for the answer.

                I have a loop that I declare two variables outside of, but when I reference them from inside the loop, it calls them 'undefined' and sends flash into an infinite loop. (randNum is a function that returns a random Number)

                Thanks for the help Raymond,
                Dave

                • 6. Re: Class constructor won't call function
                  Level 7
                  Without seeing a complete example, it's impossible to tell where the problem
                  might be. Where are randNum and referenceArray declared? Is your loop inside
                  a class method -- or on the timeline? etc...


                  Here's a more complete version of your code snippet. In this case, there are
                  no errors because both randNum and referenceArray are within scope of the
                  loop.



                  function randNum(max:Number){
                  return Math.round(Math.random()*(isNaN(max)?1:max))
                  }

                  var referenceArray:Array = ["one","two","three","four","five","six"];



                  var newRandNum:Number;
                  var i = 0;
                  do {
                  newRandNum = randNum(referenceArray.length - 1);
                  trace(" oops: " + newRandNum);
                  i++;
                  }
                  while(newRandNum == curAd || i < 10);