5 Replies Latest reply on Mar 14, 2010 7:13 PM by Harry Kunz

    How do I use event.target.name in AS2?

    stevensunsunsun

      Thanks to kglad I was able to see how event.target.name in AS3 could make a button load a movieclip with the same namesake.

       

      I'm doing the same thing now in AS2 but don't know what to write instead of event.target.name.

       

      And so at the moment each button pushes info into an array and then a function uses that to decide which movieclip to attach to a holder after it has faded out once, and then fades in again...

       

       

      // ***** IMAGE GALLERY START ***** //
      // Add image to holder
      imgholder.attachMovie("img0", "image0_0", 1)

       

      // Array
      var nextLoad = ["img0"];

       

      // Btn listeners
      img5.onRelease = function() { trace (nextLoad); nextLoad.pop(); nextLoad.push("img5"); btnClick() } // Written on 1 line
      img4.onRelease = function() { trace (nextLoad); nextLoad.pop(); nextLoad.push("img4"); btnClick() }
      img3.onRelease = function() { trace (nextLoad); nextLoad.pop(); nextLoad.push("img3"); btnClick() }
      img2.onRelease = function() { trace (nextLoad); nextLoad.pop(); nextLoad.push("img2"); btnClick() }
      img1.onRelease = function() {
      nextLoad.pop();
      nextLoad.push("img1");
      btnClick()
      }
      img0.onRelease = function() {
      nextLoad.pop();
      nextLoad.push("img0");
      btnClick()
      }

       


      // The btn function
      function btnClick() {
      trace ("click");
      var myImgTween:Object = new Tween(imgholder, "_alpha", Strong.easeOut, 100, 0, 1, true);
      myImgTween.onMotionFinished = function() {
      fadeOutImg();
      }
      }

       

       

      // The btn function part II
      function fadeOutImg() {
      trace ("fadeOutImg");
      imgholder.attachMovie(nextLoad, "image1_1", 1);
      var myImgTween:Object = new Tween(imgholder, "_alpha", Strong.easeOut, 0, 100, 1, true);
      }

       

      I know I should be able to push the button name into the array, but am having to use a string... I'm sure my code is cumbersome!! But it works. I've tried pushing the button name but it end up including the full stage reference.
      Is there a cleaner way of doing this using event.target.name?
      Thanks for looking!

        • 1. Re: How do I use event.target.name in AS2?
          Harry Kunz Level 4

          There is no event.target in AS2. However since AS2 has no way of remembering the scope object where the target object resides, you can use this to your advantage to retrieve the name of the target by using the "this" command in the function whenever you use the above format "mc.onPress = myfunc". FYI, the popular workaround Delegate, made it possible that when you called "this" in the function, you could retrieve the scope object where the target instance resides. So without its use, "this" will return the name of the target object. Anyway this is the code you can use:

           

           

          imgholder.attachMovie("img0","image0_0",1);

          var nextLoad = ["img0"];

           

          img5.onRelease = onImgRelease;

          img4.onRelease = onImgRelease;

          img3.onRelease = onImgRelease;

          img2.onRelease = onImgRelease;

          img1.onRelease = onImgRelease;

          img0.onRelease = onImgRelease;

           

          function onImgRelease():Void {

             nextLoad.pop();

             nextLoad.push(this._name);

             btnClick();

          }

           

          // The btn function

          function btnClick() {

             trace("click");

             var myImgTween:Object = new Tween(imgholder, "_alpha", Strong.easeOut, 100, 0, 1, true);

             myImgTween.onMotionFinished = function() {

                fadeOutImg();

             }

          }

           

          // The btn function part II

          function fadeOutImg() {

             trace("fadeOutImg");

             imgholder.attachMovie(nextLoad,"image1_1",1);

             var myImgTween:Object = new Tween(imgholder, "_alpha", Strong.easeOut, 0, 100, 1, true);

          }

          • 2. Re: How do I use event.target.name in AS2?
            stevensunsunsun Level 1

            Excellent. Thank you so much. That's some very nice and tidy code. I didn't realise you could line up the functions like that. What a difference! And I was missing ._name when trying to use this before! Now it works a charm. Thanks again, and for the explanation.

            • 3. Re: How do I use event.target.name in AS2?
              Harry Kunz Level 4

              You're welcome.

              1 person found this helpful
              • 4. Re: How do I use event.target.name in AS2?
                stevensunsunsun Level 1

                ...and I just shortened

                 

                img5.onRelease = onImgRelease;

                img4.onRelease = onImgRelease;

                img3.onRelease = onImgRelease;

                img2.onRelease = onImgRelease;

                img1.onRelease = onImgRelease;

                img0.onRelease = onImgRelease;

                to...
                for (l=0; l<5; l++) {
                this["img"+l].onRelease = onImgRelease;
                }
                ....seems to work ok – I think this counter is my favourite thing in AS!

                • 5. Re: How do I use event.target.name in AS2?
                  Harry Kunz Level 4

                  Yeah it's really cooL coz if you make your program really dynamic, you can reduce the work and effort to add additional functionalities too.