6 Replies Latest reply on May 21, 2007 10:14 PM by kglad

    LoadVars problem

    Some1Won
      Hello,

      I seem to have ran into one of the many oddities in flash. Only this time, I can’t seem to get around it, nor can I even tell what is wrong...

      My code is pretty simple I think, just 1 method right now (at the bottom)

      The file that both of these loaders, ('loaderObject' and 'anotherLoader') are loading are the exact same. The only difference between the two is that ‘loaderObject ‘ is a class variable (so it can be access in another method), while ‘anotherLoader’ exists only inside the method. If it matters, the class (that this method is in) extends MovieClip. The file itself is a text file that contains 1 line: “text=This is a sample string of text.”

      When I run this I get:

      inside loader: undefined
      inside anotherLoader: This is a sample string of text.

      Any ideas why 'loaderObject' can’t load the text/file?
      // ---------------------------------------------------------------------------------
        • 1. Re: LoadVars problem
          Greg Dove Level 4
          If its a local text file it seems unusual to try and load it twice and particularly in quick succession like that. For testing its interesting but in practice you should never need to retrieve the same information from a text file twice if it doesn't change, and if it is being changed (by some external process) then you would probably need to poll it with a reasonable interval between loads.
          Perhaps its the fact that the loads are being issued in rapid succession that causes it, with really no time (well not much anyway) between calls.

          To test this try changing the order of the two .load commands.

          anotherLoader.load(inputFile);
          loaderObject.load(inputFile);

          See if it works the other way around. It might be that the last issued load when called in quick succession from the same source takes precedence . Don't know...just guessing could be way off track.

          • 2. Re: LoadVars problem
            Some1Won Level 1
            Hi,

            You are right, that it is true that I should never need to reload anything twice like that. I only put in “anotherLoader” to test when I noticed that “loaderObject” was unable to load the file initially. Then it struck me as very odd that “anotherLoader” was able to load it, while “loaderObject” couldn’t.

            To test your theory, I did swap the lines and I get the same output (just different order printed). And yes, I should have mentioned this is a local text file in the same folder.
            • 3. Re: LoadVars problem
              kglad Adobe Community Professional & MVP
              you have a reference problem inside loaderObject.onLoad. "this" will refer to your loaderObject within that methods scope, but loaderObject will not.
              • 4. Re: LoadVars problem
                Some1Won Level 1
                Ah okay, I think I can see what is going on here. When I wrote ‘loaderObject’ inside the onload for ‘loaderObject’ , it was trying to find a ‘loaderObject’ within the ‘loaderObject’. So, since I want to get the current ‘loaderObject’, I used ‘this’ to refer to itself inside the onload method. That part works fine now. Thanks for cleaing that part up. =)

                But now, I have another problem, I can’t set the contents of what I read in to a variable ‘outside’ of the ‘loaderObject’.

                It might make it a bit easier if I post my whole class (below).

                ‘contentString’ is originally set to “null “. After the init() method runs, I want it to contain the results read in, it should be: “null This is a sample string of text.“ However, onLoad method, can’t even read ‘contentString’, and the resulting string ends up: “undefinedThis is a sample string of text.”

                Then of course, when I access ‘contentString’, in another method (such as getText()), I get “null “.

                This is the exact output I get when it runs:

                inside(before): undefined
                this: This is a sample string of text.
                inside(after): undefinedThis is a sample string of text.
                getText: null

                I think the problem is that inside the onLoad method, it is trying to find a ‘contentString’ inside of ‘loaderObject’. But how can I get ‘loaderObject’ to see ‘contentString’ (the class variable) and modify it?

                //---------------------------------------------------------------------------------------- --------------
                • 5. Re: LoadVars problem
                  Some1Won Level 1
                  Hello again,
                  (adding a reply as it is a different post on a different question)

                  Ah, I’ve gotten it now. All I did was add a reference to ‘this’ (the class), inside of the loaderObject and accessed the class variable through that reference in the onload method

                  So, I added this line in the constructor:
                  loaderObject._outside = this;
                  (where ‘this’ refers to the class, I believe?)

                  Then, in the onload method, I was able to access the variable by going:
                  this._outside.contentString = this.text;
                  (where ‘this’ refers to the loaderObject)

                  I guess my question is now, is there a better way to do this? This feels like a quick ‘hack’ to me, or is it actually the ‘correct’ way?
                  • 6. Re: LoadVars problem
                    kglad Adobe Community Professional & MVP
                    this in the constructor is going to reference a class instance, not the class. i don't think you can reference the class from within the class.