17 Replies Latest reply on Apr 18, 2006 4:20 PM by Newsgroup_User

    listeners, variables and properties

    maguskrool Level 1
      Hi.

      All code is from the 1st frame of the main timeline.
      In my .swf I attached several instances of a MC to the main timeline:

      for (k = 1; k <= nTotalMCs ; k++) {
      attachMovie("id1", "mc"+k, _root.getNextHighestDepth(), this["o"+k]);
      trace (this["mc"+k]); //returns _level0.mc1, _level0.mc2,..., _level0.mc3
      }

      I have a programatic tween and I have added listeners to handle events related to it. This is one of them:

      oListener01.onMotionChanged = function():Void {
      for (j=1; j<=nTotalMCs; j++) {
      trace (["mc"+j]); //returns mc1, mc2,..., mcN
      trace (this["mc"+j]); //returns undefined
      trace (this._parent["mc"+j]); //returns undefined
      trace (_root["mc"+j]); //returns _level0.mc1, _level0.mc2,..., _level0.mcN
      }
      };

      I need to access those MCs from the listner, but I can't use _root because the .swf this is in is going to be loaded by other .swfs, so I need to use relative paths. Can anyone tell me what I'm doing wrong and how to get the correct relative path? Thank you in advance.
        • 1. Re: listeners, variables and properties
          Level 7
          maguskrool,

          > oListener01.onMotionChanged = function():Void {

          The only onMotionChanged event I see in the ActionScript Language
          Reference is Tween.onMotionChanged. In the docs, the example shows an event
          handler, not an event listener. With listeners, a generic Object instance
          is used as a kind of proxy. Since you named your object oListener01, it
          *seems* like you're using a listener ...

          var oListener bject = new Object();
          oListener.onMotionChanged = function() {};
          oSomeOtherObject.addEventListener(oListener);

          Is that what you're doing? If so, the Tween class doesn't work that
          way. )Are you even using the Tween class?)

          David
          stiller (at) quip (dot) net
          Dev essays: http://www.quip.net/blog/
          "Luck is the residue of good design."


          • 2. Re: listeners, variables and properties
            maguskrool Level 1
            David, thank you for your reply.

            Yes, I'm using the Tween class. I did the following:

            //Listener
            var oListener01:Object = new Object();
            oListener01.onMotionChanged = function():Void {
            for (j=1; j<=nTotalMCs; j++) {
            trace (["mc"+j]); //returns mc1, mc2,..., mcN
            trace (this["mc"+j]); //returns undefined
            trace (this._parent["mc"+j]); //returns undefined
            trace (_root["mc"+j]); //returns _level0.mc1, _level0.mc2,..., _level0.mcN
            }
            };

            and later:

            var tw1:Tween = new Tween(....);
            tw1.addListener(oListener01);

            I see what you mean, after consulting the AS Language Reference, but this works. I also tried adding a oListener01.onMotionFinished = function () { trace ("motion finished"); }; and it worked, I got the correct traced message when the Tween ended. I didn't come up with these listeners myself, however, I adapted them from an example in

            LOTT, Joey, REINHARDT, Robert, "Flash 8 Actionscript Bible", Wiley Publishing, p.265. The code is attached.
            • 3. Re: listeners, variables and properties
              Level 7
              maguskrool,

              > var tw1:Tween = new Tween(....);
              > tw1.addListener(oListener01);
              >
              > I see what you mean, after consulting the AS Language
              > Reference, but this works. I also tried adding a
              > oListener01.onMotionFinished = function () { trace
              > ("motion finished"); }; and it worked,

              Well, I can't argue with your success. If it works, it works. But
              if you do it according to the AS Language Reference, your reference problem
              is solved. The global "this" property would refer to the Tween instance,
              and that instance knows which MC it manipulates.

              I ran a quick copy/paste taste from the Tween.onMotionChanged event
              entry. This comes directly from the docs.

              import mx.transitions.Tween;
              var myTween:Tween = new Tween(img1_mc, "_x",
              mx.transitions.easing.Elastic.easeOut,0, Stage.width - img1_mc._width, 3,
              true);

              myTween.onMotionChanged = function() {
              trace(this.position);
              };

              So right off, I know that the Tween class features a Tween.position
              property, because it refers to this.position. Of course, that information
              isn't new: the Tween class entry itself lists all the available properties.

              To see what other properties are available, I changed that trace()
              statement a bit ...

              myTween.onMotionChanged = function() {
              for (prop in this) {
              trace(prop + ": " + this[prop];
              }
              };

              Doing so put a bunch of useful information in the Output panel,
              including a property called "obj", whose value was the clip in question.
              So, from this bit of sleuthing I learned that Tween.obj refers to the movie
              clip in question.

              Maybe that'll give you what you're after. Or maybe you can use a
              similar process to discover another refernce (or whateverObj.obj may just
              work for you, too).


              David
              stiller (at) quip (dot) net
              Dev essays: http://www.quip.net/blog/
              "Luck is the residue of good design."


              • 4. Re: listeners, variables and properties
                maguskrool Level 1
                Thank you, David, but unfortunately I'm still having some trouble. What follows is what I discovered through trial and error and using your helpful insights, though I'm not 100% sure of how or why this works. Using the book I mentioned before as reference, I added and argument to the onMotionChanged function:

                var oListener01:Object = new Object();
                oListener01.onMotionChanged = function(twMyTween):Void {
                trace (this); // returns [object Object]
                trace (valueOf(this)); // returns _level0
                trace (twMyTween); // returns [Tween]
                trace (twMyTween.obj); // returns _level0.tw1, the tween to which this listener belongs to
                }

                Now, having a path to the tween (twMyTween.obj), I can reach the _root with a relative path (twMyTween.obj._parent). I used

                var oListener01:Object = new Object();
                oListener01.onMotionChanged = function(twMyTween):Void {
                for (k = 1; k <= nTotalMCs ; k++) {
                trace (twMyTween.obj._parent["mc"+k]); //returns _level0.mc1, _level0.mc2,...,_level0.mcN
                }

                What I still don't get is why the argument twMyTween automatically refers to the Tween to which the listener is attached to, but my guess is that it's explained in the documentation (if it exists, since there was none for listeners for Tweens, and they work, or then I'm just so confused about so much right now that I don't have a clue).

                Anyway, if someone can shed some light on the issues that weren't solved or provide a better solution, I'd greatly appreciate it. Thank you.
                • 5. Re: listeners, variables and properties
                  Level 7
                  maguskrool,

                  > unfortunately I'm still having some trouble. [...]
                  > Now, having a path to the tween (twMyTween.obj),
                  > I can reach the _root with a relative path
                  > (twMyTween.obj._parent).

                  Doesn't that solve your trouble? Maybe I don't fully understand your
                  question -- could very well be.

                  In your original post, you said ...

                  >>>>> I need to access those MCs from the listner

                  ... and twMyTween.obj gives you that, right? Unless you're wondering how to
                  path to the *other* movie clips from a given clip's event handler/listener.
                  If that's it, it looks like twMyTween.obj._parent would be your path --
                  assuming all these movie clips share the same parent.

                  // From mc1's point of view ...
                  twMyTween.obj.mc2
                  twMyTween.obj.mc3
                  twMyTween.obj.mc3
                  // etc.

                  > What I still don't get is why the argument twMyTween
                  > automatically refers to the Tween to which the listener is
                  > attached to

                  I'm afraid I can't answer that off the top of my head, but I've never
                  considered using listeners with the Tween class. Clearly, it works, but I'd
                  have to do some experimentation myself to satisfy myself as to why.

                  I have tons of respect for Joey Lott, so I'm definitely curious now
                  about t his alternative approach to Tween events. The ActionScript Language
                  Reference is certainly my first stop in researching any object, but it's not
                  my *only* stop.

                  > Anyway, if someone can shed some light on the issues
                  > that weren't solved or provide a better solution

                  I'll do my best to answer your issue, but I'll have to play with it for
                  a while.


                  David
                  stiller (at) quip (dot) net
                  Dev essays: http://www.quip.net/blog/
                  "Luck is the residue of good design."


                  • 6. Re: listeners, variables and properties
                    maguskrool Level 1
                    David, yes, my problem is solved, what I meant is that I'm having trouble fully understanding why it's solved, namely why the argument on the listener's functions automatically refers to the Mc that was Tweened. That's the only reason why I haven't marked this question as answered. Thank you for your kind help :)
                    • 7. listeners, variables and properties
                      Peter Lorent Level 2
                      Might be a bit complicated what you are doing...
                      Just set a reference to the timeline and use that reference in your listener.onMotionChanged. Attached pseudo code. The argument you mentioned is received from the Tween Class when it broadcasts the onMotionChanged event and holds the tweened object.

                      • 8. Re: listeners, variables and properties
                        Peter Lorent Level 2
                        About using the listener on the Tween Class.
                        from the Tween Class:

                        public var addListener:Function;
                        public var removeListener:Function;
                        • 9. Re: listeners, variables and properties
                          Level 7
                          LuigiL,

                          > About using the listener on the Tween Class.
                          > from the Tween Class:
                          >
                          > public var addListener:Function;
                          > public var removeListener:Function;

                          Good eye. Well, that explains that. Curious, then, why the AS
                          Language Reference prefers the handler approach.


                          David
                          stiller (at) quip (dot) net
                          Dev essays: http://www.quip.net/blog/
                          "Luck is the residue of good design."


                          • 10. Re: listeners, variables and properties
                            Peter Lorent Level 2
                            People tend to forget to cleanup these types of listeners?
                            • 11. Re: listeners, variables and properties
                              maguskrool Level 1
                              Wow, guess I was right not to mark this question as answered, all this new info has been very useful and you both provided me with good insight and solutions. Thank you!
                              • 12. Re: listeners, variables and properties
                                Peter Lorent Level 2
                                You're welcome.
                                David: MM even formaly discourages using event handler functions because the listener object is more scalable and flexible.
                                • 13. Re: listeners, variables and properties
                                  Level 7
                                  LuigiL,

                                  > David: MM even formaly discourages using event
                                  > handler functions because the listener object is
                                  > more scalable and flexible.

                                  I do not doubt you, but I'm curious where you heard that. Maybe a
                                  technote, somewhere? Certain events, like MovieClip.onRelease, seem to work
                                  well enough with event handlers, for example -- and certainly, event
                                  handlers are used in the docs for such entries.


                                  David
                                  stiller (at) quip (dot) net
                                  Dev essays: http://www.quip.net/blog/
                                  "Luck is the residue of good design."


                                  • 14. Re: listeners, variables and properties
                                    Peter Lorent Level 2
                                    I've read it in several books. One of them is Essential ActionScript 2.0 by Colin Moock.
                                    But I think it relates more to custom objects and components then the 'regular' event handlers.
                                    • 15. Re: listeners, variables and properties
                                      Level 7
                                      >> I do not doubt you, but I'm curious where you heard
                                      >> that. Maybe a

                                      > I've read it in several books. One of them is Essential
                                      > ActionScript 2.0 by Colin Moock. But I think it relates
                                      > more to custom objects and components then the
                                      > 'regular' event handlers.

                                      Thanks, LuigiL! If it's in Essential ActionScript 2.0, then it likely
                                      does relate to custom objects -- in fact, in that light, your observation
                                      almost rings a bell for me. I'll have to check the book again.


                                      David
                                      stiller (at) quip (dot) net
                                      Dev essays: http://www.quip.net/blog/
                                      "Luck is the residue of good design."


                                      • 16. Re: listeners, variables and properties
                                        Peter Lorent Level 2
                                        >> I'll have to check the book again.
                                        That particular book should be on your desk permanently... ;)
                                        • 17. Re: listeners, variables and properties
                                          Level 7
                                          Oh, it is! But I'll have to open it and read it again.


                                          "LuigiL" <webforumsuser@macromedia.com> wrote in message
                                          news:e225sp$939$1@forums.macromedia.com...
                                          >>> I'll have to check the book again.
                                          > That particular book should be on your desk permanently...