2 Replies Latest reply on Jun 18, 2006 6:50 AM by maguskrool

    changing variables in .as file

    maguskrool Level 1
      Hi.

      I have a main.swf and sub1.swf, sub2.swf,... subN.swf files. They all use variables contained in myCode.as file. The sub... files are loaded on empty movie clips on the main.swf.

      The myCode.as file has:

      class myCode {
      var bReady1:Boolean = false;
      var bReady2:Boolean = false;
      ...
      var bReadyN:Boolean = false;
      }

      All the sub... files have:

      var f_N:myCode = new myCode();
      this.onEnterFrame = function () {
      if (f_N.bReadyN == true) {
      trace ("Ready "+N);
      f_N.bReadyN = false;
      }
      }

      Finally, on the main.swf I have some buttons, each corresponding to a sub... file.

      var f_main:myCode = new myCode();
      this.btButtonSubN.onPress = function () {
      f_main.bReadyN = true;
      }

      So, the way I figured this would work was: I press the button; bReadyN's value becomes "true"; on the next onEnterFrame, subN.swf sees bReadyN is true and traces the string. But I guess this isn't the correct way to do this, f_main.bReadyN and f_N.bReadyN don't seem to see the same variable, and I don't know why. Do they "create" new variables? All help is most welcome, thanks in advance!

      João
        • 1. Re: changing variables in .as file
          Peter Lorent Level 2
          >>Do they "create" new variables?
          Not exactly but close.
          When you declare:
          var bReady1:Boolean=false;
          you are creating an instance property, meaning each instance of the class (object) has its own bReady1 property.

          Depending on the situation, you can solve this by using a class property which will be the same for every instance of the object:
          private static var bReady1:Boolean=false;
          Drawback: you can only access a static prop through a method, not directly as you do in your onPress

          Code example:

          • 2. Re: changing variables in .as file
            maguskrool Level 1
            Thanks a lot for your reply, your solution seems to work just fine. While trying to figure it out on my own and looking through a Flash manual, I was reminded that .as files are not function and variable libraries, but custom classes, so why f_main.bReadyN was different from f_N.bReadyN became clear. I did find another way around my problem and it seems to work at least in my particular case, though perhaps your solution is more generally useful, I'm not sure.

            I realised that since the sub... files were loaded into the main.swf, I could access their variables from there, so I used:

            //main.swf timeline
            //mcLoaded is the Movie Clip where the subN.swf is loaded
            var mcN:MovieClip = this.mcLoaded;
            this.btButtonSubN.onPress = function () {
            mcN.f_N.bReadyN = true;
            }

            This way I'm altering the value on each sub... file's instance. So far it seems to work but if I have any problems I'll definitely implement your solution. Thank you again for your help.