9 Replies Latest reply: May 1, 2012 5:33 PM by XELAD111 RSS

    Analog of Watch() in AS3

    XELAD111 Community Member

      Hello everyone!

      Here is a situation:

      var MAIN_OBJ:Object = new Object();

      MAIN_OBJ.array_1 = new Array(1, 2, 3);

      MAIN_OBJ.array_2 = new Array(A, B, C, D, E, [f,g,h,i,j]);

      MAIN_OBJ.array_3 = new Array([k,l,m,n,o,p], Q, R, S, T);

       

      ActionScript2:

      function someFunction(prop, oldVal, newVal, limit) { trace("prop is about to change"); return newVal;}

      MAIN_OBJ.watch("array_1", someFunction);

       

      ActionScript3:

      Use accessor properties (get/set functions) or the flash.utils.Proxy class for similar functionality.

       

      Here is the task:

      Watch some property of object to change, and do smth. Watch not the whole object, only one property.

       

      I know that ExternalInterface.addCallback provides smth like that, but for the whole object.

      I've never used get/set functions or override smth, please explain what should exactly be written to achieve the same effect as in AS2. The simplier it would be - the better.

       

      Sorry fo my English.

        • 1. Re: Analog of Watch() in AS3
          kglad MVP

          // in watching class:

           

          watchedClass.addEventListener("propertyChangedE",propertyChangedF);

          private function propertyChangedF(e:Event):void{

          // do whatever

          }

           

          // in watched class, dispatch event whenever changed from inside the class and from outside:

           

          public function set someProperty(someValue:ValueType):void{

          _someProperty=someValue;

          dispatchEvent(new Event("propertyChangedE"));

          }

          • 2. Re: Analog of Watch() in AS3
            XELAD111 Community Member

            I suppose that to change someProperty, I should use

            setProperty ( target, someProperty , value);

             

            I'd better add more details to the question:

            var MAIN_OBJ:Object = new Object();

            MAIN_OBJ.array_1 = new Array(1, 2, 3);

            MAIN_OBJ.array_2 = new Array(A, B, C, D, E, [f,g,h,i,j]);

            MAIN_OBJ.array_3 = new Array([k,l,m,n,o,p], Q, R, S, T);

             

            MAIN_OBJ.array_1.push(4);

            MAIN_OBJ.array_1.splice(1,1);

             

            I think, in this case it is not possible to use setProperty

            • 3. Re: Analog of Watch() in AS3
              kglad MVP

              again, // in watched class, dispatch event whenever changed from inside the class and from outside:

              • 4. Re: Analog of Watch() in AS3
                XELAD111 Community Member

                To tell the truth, bold words did not explain anything. I can read, may be I loose some meaning of sentence. Ok, here is the package, it has a mistake, because I don't understand what should be written there.

                 

                package  {

                          public class Main {

                                    import flash.events.*;

                                    public function Main()

                                    {

                                              MAIN_OBJ.array_1 = new Array(1, 2, 3);

                                              MAIN_OBJ.array_2 = new Array(A, B, C, D, E, [f,g,h,i,j]);

                                              MAIN_OBJ.array_3 = new Array([k,l,m,n,o,p], Q, R, S, T);

                                              this.addEventListener("propertyChangedE",propertyChangedF);

                                              MAIN_OBJ.array_1.push(4);

                                    }

                 

                                    private function propertyChangedF(e:Event):void

                                    {

                                              trace("smth changed");

                                    }

                 

                                    public function set MAIN_OBJ.array_1(someValue):void //???

                                    {

                                              MAIN_OBJ.array_1 = someValue;

                                              dispatchEvent(new Event("propertyChangedE"));

                                    }

                                    public var MAIN_OBJ:Object = new Object();

                          }

                }

                • 5. Re: Analog of Watch() in AS3
                  kglad MVP

                  it looks like you don't really understand how to use class files.  there are numerous problems with those few lines of code.

                   

                  i think you would be better off learning how to crawl before trying to run.  i would recommend studying some of the basics of oop and class file structure.

                   

                  so, i'm not sure how much this will help you, but you should use:

                   

                   

                  package  {

                         import flash.events.EventDispatcher;

                            public class Main {

                              

                       public var MAIN_OBJ:Object = new Object();

                                      public function Main()

                                      {

                                                MAIN_OBJ.array_1 = new Array(1, 2, 3);

                                                MAIN_OBJ.array_2 = new Array(A, B, C, D, E, [f,g,h,i,j]);

                                                MAIN_OBJ.array_3 = new Array([k,l,m,n,o,p], Q, R, S, T);

                  //the line below belongs elsewhere in the class where you want to detect a change

                                           //     this.addEventListener("propertyChangedE",propertyChangedF);

                                                MAIN_OBJ.array_1.push(4);

                  // dispatch event whenever changed from inside the class:

                  dispatchEvent(new Event("propertyChangedE"));

                                      }

                   

                                     

                   

                                      public function set AddArray_1(someValue):void

                                      {

                                                MAIN_OBJ.array_1.push(someValue);

                  //dispatch event whenever changed from outside:

                                                dispatchEvent(new Event("propertyChangedE"));

                                      }

                                

                            }

                  }

                  • 6. Re: Analog of Watch() in AS3
                    XELAD111 Community Member

                    I've learnt much of that AS3, and don't still understand, why simple things in one line on AS2 turned out to be a scope of user defined functions on AS3.

                    Let's go by steps:

                     

                    1) I've created new Object, added some properties there.

                    2) You said:

                    // in watching class:

                    watchedClass.addEventListener("propertyChangedE",propertyChangedF);

                     

                    So, if we are watching an object in Main class, I wrote:

                    this.addEventListener("propertyChangedE",propertyChangedF);

                     

                    3) I added a function, which EventListener would call each time the property changed

                    4) I added some new values to our property (.push(4)), so in AS2 it would make EventListener run its function.

                     

                    That makes sense, isn't it ?

                    And still there are questions:

                    1) If I do smth with object's property, I should always call dispatchEvent after that?

                    2) //the line below belongs elsewhere in the class where you want to detect a change

                    I want to detect a change after setting basic properties. Can't I write it before .push(4) ?

                    3) I suppose we should mention MAIN_OBJ somewhere, because How EventListener would understand that MAIN_OBJ has changed?

                    • 7. Re: Analog of Watch() in AS3
                      XELAD111 Community Member

                      Nobody can explain?

                      • 8. Re: Analog of Watch() in AS3
                        sinious MVP

                        There is no event that will fire off "automagically" when you change a property of an object.

                         

                        Read what I said above a few times.

                         

                        People here are telling you to make a function to change a property of the object. The reason why is because you can dispatch an event in the same function when you change that property.

                         

                        That is the only way you will know when the property changes.

                         

                        I'll say it again. Read line 1. There is no event that fires "automagically" when you change a property of an event.

                         

                        That is programming 101. You create a "getter" and a "setter" function for the objects properties then you do whatever you want in those functions. For your needs, you want to dispatch an event. So do it, like has been mentioned.

                         

                        Leave anything you ever learned in AS2 in the grave, where it belongs.

                        • 9. Re: Analog of Watch() in AS3
                          XELAD111 Community Member

                          sinious, Thank you for the clear explanation.

                          You've said, I should create a "getter" and a "setter" function for the objects properties, so there is no way to do this

                          MAIN_OBJ.array_1.push(4);

                          without writing "getter" and "setter" functions, and still catch this event? I think they just can't do the same. I suppose, you'll answer "just always write dispatch after that".

                          It seems like people never used that in AS2, never wanted to catch such events automatically, and AS3 just removed watch() for no need... that's strange.