6 Replies Latest reply on Aug 2, 2006 6:00 AM by aniebel

    I am new to Flash, I have a code question

    jimeestewart
      I have 2 movie clips , and 2 buttons, the buttons are going to be used to show/hide a movie clip. This is the actionscript I have entered, but it does not work:

      function showliner(eventObj:Object) {
      this.liner_mc._alpha = 99

      };

      function hideliner(eventObj:Object) {
      this.liner_mc._alpha = 0

      };

      this.ext_btn.addEventListener("click",hideliner);
      this.int_btn.addEventListener("click",showliner);

      I test the movie and it does not work, can someone let me know what I am doing wrong?
        • 1. Re: I am new to Flash, I have a code question
          Level 7
          jimeestewart,

          > I have 2 movie clips , and 2 buttons, the buttons are going
          > to be used to show/hide a movie clip.

          Showing and hiding just one of those two movie clips, right? And then,
          based on your code sample, those are Button Components rather than button
          symbols, correct? It's always good to provide details like this -- helps
          people help you. :)

          > This is the actionscript I have entered, but it does not work:

          Aw heck, the ActionScript works just fine! ActionScript almost always
          "works" ... it's just that the ActionScript people type doesn't always do
          what they expect it to. :) Let's step through it

          > function showliner(eventObj:Object) {
          > this.liner_mc._alpha = 99
          > };

          Here we have a function declared as showliner(). It accepts one
          argument, eventObj, of type Object. Inside, this function does one thing:
          by virtue of the global "this" property, it looks inside itself for a
          reference to liner_mc and sets that object's _apha property to 99. If this
          function doesn't have such a reference, liner_mc won't be found, and that's
          the first of your problems. Easy to fix, though. :) I'll show you in just
          a bit.

          > function hideliner(eventObj:Object) {
          > this.liner_mc._alpha = 0
          > };

          Here's another function, very similar to the first. Almost the same
          goal, except here the _alpha property is set to zero. Same problem as
          above.

          > this.ext_btn.addEventListener("click",hideliner);
          > this.int_btn.addEventListener("click",showliner);

          These two lines are what made me guess you're using Button Components.
          You've got *some* of the code you need here, but you're missing the part
          that makes it work. So let me show the correct ActionScript, then we'll
          discuss.

          function showliner(eventObj:Object) {
          liner_mc._alpha = 99;
          }
          function hideliner(eventObj:Object) {
          liner_mc._alpha = 0;
          }

          var listener1:Object = new Object();
          listener1.click = showliner;
          ext_btn.addEventListener("click", listener1);

          var listener2:Object = new Object();
          listener2.click = hideliner;
          int_btn.addEventListener("click", listener2);

          In the first two functions, I simply removed the "this," and here's why
          that's important. In the next two sections, *before* we assign the event
          listener, we have to create a generic Object instance to serve as our "event
          ambassador."

          The var statement declares a variable, arbitrarily named listener1 (of
          type Object), and sets it to a new Object instance. So now we have a
          generic object. What can we do with it? In this case, we need to give it a
          "click" property, so that's what happens in the next line. This click
          property is assigned to the previously declared showliner function.
          Finally, one of the Button Component instances invokes the addEventListener
          method to instruct the generic object, our "listener" object, to listen for
          the "click" event.

          Same goes for the other button.

          The reason "this" is inappropriate in this context is because it scopes
          the relevant reference to the object or timeline that contains it. In this
          context, it means that "this" would ultimately point to the generic Object
          instance, not the movie clip. Why? Let's follow the trail. In the
          showliner() function, the word "this" points to showliner() itself. But
          later, showliner() is assigned (without the parentheses, which are what
          actually execute the function) to the click property of our listener1
          object. Therefore, "this" *now* points to that object -- that's now where
          the buck stops, because the function was assigned to a property of this
          object. And this listener1 object contains nothing more than its one click
          property. I does not contain a reference to the liner_mc movie clip. Make
          sense?

          I think it will help you to study up a bit on objects in general, so
          that you understand terms like property, method, event, and the like. See
          if this article makes things a bit clearer.

          http://www.quip.net/blog/2006/flash/actionscript-20/ojects-building-blocks


          David Stiller
          Adobe Community Expert
          Dev blog, http://www.quip.net/blog/
          "Luck is the residue of good design."


          • 2. Re: I am new to Flash, I have a code question
            aniebel Level 2
            See if this works:

            ext_btn.onRelease = function(){
            this._parent.liner_mc._alpha = 0;
            };
            int_btn.onRelease = function(){
            this._parent.liner_mc._alpha = 100;
            };
            • 3. Re: I am new to Flash, I have a code question
              Level 7
              aniebel,

              > See if this works:
              >
              > ext_btn.onRelease = function(){
              > this._parent.liner_mc._alpha = 0;
              > };
              > int_btn.onRelease = function(){
              > this._parent.liner_mc._alpha = 100;
              > };

              That does work, but it kind of defeats the purpose of using a Button
              Component (why not just use a button symbol or movie clip instead, and avoid
              the UI Component overhead?). The reason the above works is because
              ultimately, the UI Components inherit from the MovieClip class, which means
              that every such Component *is* a MovieClip ... just a very specialized one.


              David Stiller
              Adobe Community Expert
              Dev blog, http://www.quip.net/blog/
              "Luck is the residue of good design."


              • 4. Re: I am new to Flash, I have a code question
                jimeestewart Level 1
                Thank you David and Aniebel,

                Your suggestions worked. I appreciate the help. I am coming from a PHP/MYSQL programming background and now that I am starting to use Flash, I am trying to understand the difference in Objects and syntaxes relating to movie clips vs. HTML objects. Even though most OOP languages are structured similarly, with Classes and functions, I have a lot to learn about syntax.

                Jim Stewart
                • 5. Re: I am new to Flash, I have a code question
                  Level 7
                  Jim,

                  > Even though most OOP languages are structured similarly,
                  > with Classes and functions, I have a lot to learn about syntax.

                  Flash is a wild ride. :) And just when you figure out ActionScript
                  2.0, watch out for ActionScript 3.0! ;)


                  David Stiller
                  Adobe Community Expert
                  Dev blog, http://www.quip.net/blog/
                  "Luck is the residue of good design."


                  • 6. Re: I am new to Flash, I have a code question
                    aniebel Level 2
                    oops! I misunderstood. I thought jimeestewart was referring to a regular button or mc.

                    Thanks for clarifying.