Skip navigation
XELAD111
Currently Being Moderated

Analog of Watch() in AS3

Apr 24, 2012 12:40 PM

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.

 
Replies
  • kglad
    72,373 posts
    Jul 21, 2002
    Currently Being Moderated
    Apr 25, 2012 6:34 AM   in reply to XELAD111

    // 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"));

    }

     
    |
    Mark as:
  • kglad
    72,373 posts
    Jul 21, 2002
    Currently Being Moderated
    Apr 25, 2012 10:44 PM   in reply to XELAD111

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

     
    |
    Mark as:
  • kglad
    72,373 posts
    Jul 21, 2002
    Currently Being Moderated
    Apr 27, 2012 8:30 AM   in reply to XELAD111

    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"));

                        }

                  

              }

    }

     
    |
    Mark as:
  • Currently Being Moderated
    May 1, 2012 8:54 AM   in reply to XELAD111

    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.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points