3 Replies Latest reply on Jul 16, 2008 8:48 AM by pcather

    Loading different variables into the same instance

    pcather
      I'm very new to flash. I have a txt file with various variables in it. I want to load different variables into one instance depending on which button is clicked. Here's a simplified version of what I'm trying to do.

      animal.txt contains
      &animal1=Dog&animal2=Cat

      My flash file contains two buttons- One is a picture of a dog the other is a picture of a cat. The file also contains one dynamic text box with a variable assigned to it. The word Dog or Cat will show in the text box depending on which box is clicked.

      If I had just the picture of the dog and one text box, and if I assign the text box the var=animal1, I know that I can attach this action to the dog button:

      on (release) {
      loadVariables ("animal.txt", this);
      }

      This works. I click the dog picture and the word dog shows up in the box

      But what do I do about the cat button? Since the variable in the text box is animal1 not animal2

      I tried this: I gave the text box the var name: animal

      Then I attached this action to the dog button:

      on (release) {
      loadVariables ("animal.txt", this);
      var animal:String=animal1;
      }

      And this to the cat button:

      on (release) {
      loadVariables ("animal.txt", this);
      var animal:String=animal2;
      }

      This doesn't work.

      Can anyone help? Please keep it simple as I'm very new to this.

      Thanks so much.



        • 1. Loading different variables into the same instance
          Noelbaland Level 1
          Hello there,

          I'll show you a much simpler and more productive way to accomplish what you want. We'll start with a few changes to your objects on stage.

          1) Click on your dog button and in the Properties panel give it an instance name of btnDog. While the button is still selected, open the Actions panel and delete all that script off it.
          2) Do the same for the cat button but give it an instance name of btnCat and delete the script off it.
          3) Click on your textbox and cut the 'animal' variable from the Var field and paste it into the instance name field.

          So you should have 2 buttons with instance names of btnDog and btnCat and also your dynamic textbox should have an instance name of animal.

          4) Create a new layer in your timeline and name it "Actions". Drag it to the top of the layers.
          5) Click on the first frame of this layer in the timeline and open the Actions panel.

          Type in or copy the code below.

          this.loadVariables("animals.txt");

          btnDog.onRelease = function(){
          animal.text = animal1;
          }

          btnCat.onRelease = function(){
          animal.text = animal2;
          }

          All we've done here is moved your code to a frame so that you keep everything in a central place. This is a more organized way of doing things and makes finding your code faster. You refer to objects on the stage using their instance names.
          As your variables animal1 and animal2 have been loaded into Flash from the text file, you can now access them and assign them to the textbox depending on which button is pressed.
          Giving objects(movieclips,buttons,textboxes,etc) instance names allows you to do amazing things via Actionscript. You can search for tutorials or read the Flash help pages for more info on that.

          Hope that helps
          • 2. Re: Loading different variables into the same instance
            pcather Level 1
            Thanks so much. I'll give this a try. You've explained it well. I had tried adding it to the frame but I wasn't using a function (which I guess I should learn more about) and I didn't make it an instance. I was still trying to use a variable name.

            I did search and search for tutorials but they all were too simple (i.e. what I was doing with just one button) or they were way over my head. Can anyone suggest tutorials for the "Tween" (between a beginner and an intermediate) ? Or even a book. I have a book on ActionScripts from a class I took but it stinks. Even the instructor agreed that it was a poor choice.
            • 3. Re: Loading different variables into the same instance
              pcather Level 1
              Worked like a charm. Thanks so much