3 Replies Latest reply on Jun 19, 2007 2:11 AM by Greg Dove

    passing a variable from within a function

    akiva.kent
      1. I have successfully parsed either an XML files using the XML object
      or variable=value pair using LoadVars in ACTIONSCRIPT 2 (I am using Flash8)

      2. However the scope of the variables' value is limited to inside the scope of the success function
      (pseudo code)

      if (success) {
      variable1=value;
      }

      Now outside this function block I want to be able to access this value whenever I reference the variable1 within the rest of the actionscript.

      3.Within the function block, I have tried using:
      a) return variable1;
      b) _global.variable1=value;
      c) _root.variable1=value;
      d) this.variable1=value;

      4.How do I use a screen name here in the forum? I feel dorky having "screennameisnotavailble" displayed?

      Thanks.
        • 1. passing a variable from within a function
          Greg Dove Level 4
          quote:

          2. However the scope of the variables' value is limited to inside the scope of the success function
          (pseudo code)

          if (success) {
          variable1=value;
          }

          Now outside this function block I want to be able to access this value whenever I reference the variable1 within the rest of the actionscript.


          I don't think the issue is scope... it's timing. With a LoadVars or XML, the value(s) are available via the LoadVars instance or the XML instance after the successful load. The onLoad is where you would call another function that would have access to them. The same would be true for any other variable assignments that you make after the load has completed - make sure you're not trying to access them in some other part of your code before the 'onLoad' has run.

          quote:


          4.How do I use a screen name here in the forum? I feel dorky having "screennameisnotavailble" displayed?

          Thanks.


          Hmmm. not sure... try Your Account at the top of the screen.
          • 2. Re: passing a variable from within a function
            akiva.kent Level 1
            The following was in test.fla:
            but when migrated to a different photogallery.fla file into a nested movieclip,
            with info.txt in the same folder as photogallery.fla
            doesn't have _global.TP[0] available (the dynamic text my_txt doesn't show it, nor do the photos scroll from 1 to 29 as when it does when I have the statement, _global.TP[0]=29;

            What do I have to do after I've migrated the script to the nested movieclip?
            -------------------------------------------------------------------------------
            var lorem_lv:LoadVars = new LoadVars();
            lorem_lv.onLoad = function (success:Boolean):Void {
            if (success) {
            p = lorem_lv.totalphotos;
            parameter();

            } else {
            trace("Unable to load external file.");
            }
            }
            lorem_lv.load("info.txt");

            function parameter():Void {
            trace(p);
            }
            //info.txt
            //totalphotos=29&topic=studio
            The output correctly displayed 29
            ================================
            foldnum=0;

            var lorem_lv:LoadVars = new LoadVars();
            lorem_lv.onLoad = function (success:Boolean) {
            if (success) {
            t = lorem_lv.totalphotos;
            parameter();
            } else {
            trace("Unable to load external file.");
            }
            }
            lorem_lv.load("info.txt");
            function parameter(){
            _global.TP[0]=t;
            my_txt.text=_global.TP[0];
            }
            //initialize variables and properties
            big._alpha = 0;
            //_global.WhichMainPic = 1;
            //initiate change to new image when buttons are clicked
            next.onPress = function() {
            if (_global.WhichMainPic< _global.TP[foldnum] && !fadeIn && !fadeOut) {
            fadeOut = true;
            _global.WhichMainPic++;
            input = _global.WhichMainPic;

            }
            };
            back.onPress = function() {
            if (_global.WhichMainPic>1 && !fadeIn && !fadeOut) {
            fadeOut = true;
            _global.WhichMainPic--;
            input = _global.WhichMainPic;
            }
            };
            _root.onEnterFrame = function() {
            // when a new Photo is selected, fade out, load new image, and fade in
            if (big._alpha>10 && fadeOut) {
            big._alpha -= 10;
            }
            if (big._alpha<10) {
            loadMovie(_global.PhotoFolder+"0/"+_global.WhichMainPic+".jpg", "big");
            fadeOut = false;
            fadeIn = true;
            }
            if (big._alpha<100 && fadeIn && !fadeOut) {
            big._alpha += 10;
            } else {
            fadeIn = false;
            }
            // limit input field
            if (input>_global.TP[foldnum]) {
            input =_global.TP[foldnum] ;
            }
            // initiate change to new image when Enter key is pressed
            if (Key.isDown(Key.ENTER)) {
            fadeOut = true;
            _global.WhichMainPic = input;
            }
            };
            // if a number is entered in the input field but Enter is not pressed, change
            // back to current Photo number when clicking anywhere else
            //inputField.onKillFocus = function() {
            input = _global.WhichMainPic;


            //};
            • 3. Re: passing a variable from within a function
              Greg Dove Level 4
              hmmm not sure.

              In this part:
              function parameter(){
              _global.TP[0]=t;
              my_txt.text=_global.TP[0];
              }

              try the following:

              function parameter(){
              trace("t ="+t);
              trace("lorem_lv.totalphotos is "+lorem_lv.totalphotos);
              trace("_global.TP is defined:"+(_global.TP != undefined));
              _global.TP[0]=Number(t);
              my_txt.text=_global.TP[0];
              }