6 Replies Latest reply on Feb 15, 2009 2:19 PM by Ned Murphy

    OK, this should be easy, but...

    BobMelnyk
      Hi,

      I'm using actionscript to control the fading in and fading out of images. I have 15 images defined, and initially loaded to the stage with an alpha set to 0. then I use this code to fade them in and out sequentially:

      this.onEnterFrame = function() {
      showFood1();
      }

      showFood1 = function() {
      this.onEnterFrame = function() {
      if (food1._alpha < 100) {
      food1._alpha += 4;
      }
      if (food1._alpha > 99) {
      keepFood1();}
      }
      }

      keepFood1 = function() {
      this.onEnterFrame = function() {
      if (food1._alpha < 160) {
      food1._alpha += 1;
      }
      if (food1._alpha > 160) {
      hideFood1();}
      }
      }

      hideFood1 = function() {
      this.onEnterFrame = function() {
      if (food1._alpha > 0) {
      food1._alpha -= 4;
      }
      if (food1._alpha < 1) {
      showFood2();}
      }
      }
      .
      .
      .
      this is repeated for all 15 images, and the last function calls the first again. It works great, but it seems there has to be a way to just use 1 set of functions (showFood, keepFood, and hideFood) which basically loops through repeatedly, the first time through processing the first image, the second time through processing the second image, etc., doing all 15 images, then repeating continuously so it just keeps looping through all the images. This way I could add/delete images and just have to change the loop counter - I would not have to keep adding or deleting functions in the code. Any ideas? Thanks for any help.

      Bob
        • 1. OK, this should be easy, but...
          Ned Murphy Adobe Community Professional & MVP
          Look into using the Tween class for the fade in and fade out, along with some listeners to tell you when to move on to subsequent stages. Some timing elements may come in handy as well.

          You can feed in the object from an array that will be tweened at the start of each cycle, incrementing a counter so that the next object in the lineup will be assigned the next time the sequence starts (resetting it to zero when the time comes).

          You could probably achieve a similar effect using your current code (though Tween-ing is smoother), but you need to have functions being fed objects rather than dedicating functions to individual objects.
          • 2. Re: OK, this should be easy, but...
            BobMelnyk Level 1
            Ned,

            Thanks for your reply. Unfortunately, I don't really follow what you mean. Not sure what you mean by "but you need to have functions being fed objects rather than dedicating functions to individual objects."

            I was hoping to be able to define an array such as:

            var images:Array = new Array();

            Then setting the array such that images[0]="food1"; , etc.

            Then instead of saying food1._alpha += 4; for example, I could say images ._alpha +=4;, and loop through incrementing the value of i. But that did not work.

            Can you give me some more detail about what it means to have "functions being fed objects rather than dedicating functions to individual objects."?

            Thanks!

            Bob
            • 3. Re: OK, this should be easy, but...
              Ned Murphy Adobe Community Professional & MVP
              You were heading on the irght track with what you tried, but you were targeting names of things (Strings) rather than the actual objects by those names. If you have the name of an object, that being a String, you can target the object itself using the following:

              this[ object-name]._alpha = ...

              The brackets cause the program to evaluate what's inside as an object. So if you had used..

              this["food1"].alpha =

              OR

              this[images[0]].alpha =

              You would have been targeting the instance named food1. You could even store the actual object target in the array...

              images[0] = this["food1"];

              such that when it comes time to control it, you would only have to use...

              images[0]._alpha = ...

              I'll see if I can come up with a simple example.
              • 4. Re: OK, this should be easy, but...
                Ned Murphy Adobe Community Professional & MVP
                This should be sufficient to do what you are after, you'd just have to fill up the array with your 15 items.
                • 5. Re: OK, this should be easy, but...
                  BobMelnyk Level 1
                  Ned,

                  Thanks so much for your time and your reply! I actually figured out a way to make it work, but this may be even easier (once I study it and understand it completely :-) ). Thanks again!

                  Bob
                  • 6. OK, this should be easy, but...
                    Ned Murphy Adobe Community Professional & MVP
                    You're welcome Bob. The best things are those you make yourself, so I'm glad you pursued it and got it working. If you have any questions about my approach, feel free to ask.