4 Replies Latest reply on Jul 13, 2006 9:34 AM by _Jenkins_

    Help with loading multiple images via LoadVars

      Hello everybody.

      I need a hand loading multiple images using the LoadVars method with a text file. I can get it to load 1 image but not anymore than that. I am aware of other methods like using components but I am looking for a method where I can access and change all data from 1 text file (there will be text variable text within the file aswell to, but I am more concerned with the images at the moment).

      Anyway on to the issue. I have created a much simple .fla file that outlines my problem.

      The movie contains 3 layers:

      - top layer contains AS

      - middle layer contains an empty movie clip with the instance name of mcImage1

      - bottom layer contains an empy movie clip with the instance name of mcImage2

      The AS layer contains the following code:

      imagedata = new LoadVars()
      imagedata.onLoad = function(ok){
      } else trace("Problem Loading")

      In the same folder of my .swf file I have a text file called data.txt which contains the following


      Also in the same folder of my .swf file I have two images image1.gif and image2.gif.

      When I run the flash the image2.gif is imported correctly. image1.gif does not appear.

      You can download my source files and all images here


      Any help that can be shed on this problem is much appreciated.


        • 1. Re: Help with loading multiple images via LoadVars
          Rothrock Level 5
          Couple of things that I would like to help you with here.

          First you can't load an image with the LoadVars class. LoadVars loads text files. You have to use loadMovie (which you are) or the MovieClipLoader class to load an image. I'm not pointing this out to be a jerk or anything, but it might help clear things up in your head for the future. Just because you are using the two things together doesn't mean they have anything to do with each other.

          So if you follow this line of thinking you have a couple steps to check. Make sure that LoadVars is getting the correct information before it tries to pass it off to the loadMovie. Add these trace statements inside the if(ok) part of your onLoad event handler.

          trace("Image 1 is ["+this.image1+"]");
          trace("Image 2 is ["+this.image2+"]");

          What do you see? My guess is that there is a return after image1 and therefor you won't be able to load image1.gif[return] because that is an invalid file name.

          If that is the case, here is a handy trick when working with LoadVars. Do you text file like this:


          The double ampersands prevent the addition of invisible characters to the ends of your variables.

          If that isn't what is causing the problem, then look at the loadMovie part of the problem. In this case the most common problem in that arena is that things have different names. Typos, capitalization, etc. those are the things to look for.
          • 2. Re: Help with loading multiple images via LoadVars
            _Jenkins_ Level 1
            Hi Rothrock

            Thanks for the reply. I guess I should be more specific with my question as you are correct that I am not actually using loadVars to bring in the image I am reading the file name from the data.txt file and bringing the image in using loadMovie as you pointed out.

            Anyway, thanks for the help as it was the carriage return that was causing the problem.

            I was hoping you could help me with another issue. Forgive me if it is a dumb question as I am new to this.

            If the frame with the AS and the frames with the movie clips do not occur withen the same frame number the movie fails to render the image. Say I want image1 to appear in frame1, and image2 to appear at frame 10 how would I go about calling that image once the data.txt file has been loaded in the first frame. Is there something that is needed for each instance of a frame that is after the initial loading of the data.txt file?

            Thanks for your help, it is much appreciated.
            • 3. Re: Help with loading multiple images via LoadVars
              Rothrock Level 5
              Glad to help. It is just that I see so many folks who have two different parts of a problem smushed together – each problem isn't so hard on its own, but together they are difficult to wrap your head around. Always try and break down each step of the problem and it will help you in the end.

              That being said, I'm not quite so sure on this next problem. I don't do so much timeline stuff along with the Actionscript. I can get tricky. So this I don't quite have as much of clear mental picture of what you are describing. But here are some questions that I would ask – either of myself if I was doing it or of you.

              Is there a stop() on frame one to wait for the LoadVars.onLoad? Does the onLoad then say to play?

              If not, what happens if we get to Frame 10 before the LoadVars has even finished? That could be a problem.

              Remember that the LoadVars object is an actual object that will exist until it is deleted or removed in various ways. You can access it at any time after it is loaded. The onLoad event handler is just that thing you want to happen immediately after it is loaded.

              So my design would probably be.

              LoadVars on Frame 1.
              Where I am stopped.
              In the onLoad handler load the first image and tell the timeline to play
              On frame 10, the LoadVars object will still exist (unless you've deleted it)
              Get the variable out of the LoadVars and load the image.

              If you want to check this. Put a stop() in frame 10 and run it in the testing environment. When it gets to that frame, go to the debug menu and List Variables. You should see that your LoadVars object is still there.

              Does that answer your question or am I totally missing the point?
              • 4. Re: Help with loading multiple images via LoadVars
                _Jenkins_ Level 1
                Hi again Rothrock.

                Just wanted to say thanks for taking the time to help me out. I have the file working as intended now.

                For those who are interested here is what I ended up doing

                In my data.txt file:


                Very first frame spanning timeline with movie clips.

                var img1:String;
                var img2:String;
                var img3:String;
                var img4:String;
                var img5:String;
                imagedata = new LoadVars() ;
                imagedata.onLoad = function(ok){
                } else trace("Problem Loading");

                imagedata.load("data.txt") ;

                And on each individual frame where I wanted my mcImage movieclips

                mcImage1.loadMovie(_root.img1); or mcImage2.loadMovie(_root.img2); and so on.

                Thanks again for your time. It was much appreciated.