2 Replies Latest reply on Dec 29, 2008 6:16 AM by Newsgroup_User

    Class Variables

    BeniRose
      So I'm writing a Class in AS2 and I'm having problems in this one method. I have a method that uses a Tween class and I want to use the onMotionFinished method of the Tween class to change a class scope variable, but I seem to loose scope once I'm inside the onMotionFinished method. I'll attach the method code, but basically when I trace "this" inside the onMotionFinished, I get the Tween, and even tracing this.obj._parent doesn't give the class instance, it gives level that the tweened object was created on. "_state" is the class level variable I'm trying to change, but when I trace it from within onMotionFinished I get "undefined". Any suggestions to getting back to the class level?
        • 1. Re: Class Variables
          BeniRose Level 1
          Actually I figured it out just a few minutes after posting! I found it in some old code I had from a previous job. At the begging of the method I include:

          var me = this;

          and then I can access any class variables by prefacing it with me. Is there an easier/better way to do this though?
          • 2. Re: Class Variables
            Level 7
            BeniRose,

            > Actually I figured it out just a few minutes after posting! I
            > found it in some old code I had from a previous job. At
            > the begging of the method I include:
            >
            > var me = this;

            That will certainly do it (and of course, that "me" can be named
            whatever you like; it can be classRef, thisInstance, me, or whatever makes
            sense to you). Another approach is to use the Delegate class, which
            features a static create() method that lets you re-route scope:

            unblur.onMotionFinished = Delegate.create(
            this,
            function() {
            _state = "shown";
            }
            );

            Note the scope (via "this") as the first parameter, and the function as
            the second parameter. You're basically doing this: Delegate.create(scope,
            function);, and the function performs in the scope specified.

            Since you're getting into AS2 classes, I encourage you to use strong
            typing everywhere you can. It's a good habit to get in to, because the
            strong typing gives you better error messages and sometimes helps with code
            completion (note the :Void):

            unblur.onMotionFinished = Delegate.create(
            this,
            function():Void {
            _state = "shown";
            }
            );

            You'll have to import the Delegate class at the top of your custom class
            in order to use it.


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