Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
// 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"));
}
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
again, // in watched class, dispatch event whenever changed from inside the class and from outside:
Copy link to clipboard
Copied
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();
}
}
Copy link to clipboard
Copied
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"));
}
}
}
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
Nobody can explain?
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
I meet ths same situation,something like javascript's Object.defineProperty.I just want it dispatch auto,so that I can update my pane
Copy link to clipboard
Copied
if you want to dispatch a js event, use:
whatever_element.dispatchEvent(new Event("whatever_event"));
Copy link to clipboard
Copied
In fact,I'm going to make my panel update when an object's specify property change.Yes,I can bind a listener and dispacth it after this object's specify change,but the question is I don't want to do the dispatch step,like hide it in somewhere.In ES5 method Object.defineObjectProperty() can help me do this,it use getter() and setter() do something more when an object' property change,but not a proxy.
So I'm wondering whether if this is a way can achieve some like this in AS3.
Copy link to clipboard
Copied
i don't understand what you're asking.
do you mean you want to detect when an object's property changes?
Copy link to clipboard
Copied
In JavaScript method Object.defineObjectProperty() can help me do something more in it's setter(),when an object' property change,no need to dispatch.
So I'm wondering whether if this is a way can achieve some like Object.defineObjectProperty() in AS3.
Copy link to clipboard
Copied
Just try to use MVVM instead of MVC
Copy link to clipboard
Copied
Yes,detect when an object's property changes.
Copy link to clipboard
Copied
you simply need to define that yourself.
Copy link to clipboard
Copied
for example, to automatically dispatch an event (or take any other action) when a movieclip property changes, define a class (eg, MC), and make each of your movieclip's members.
define a function that enumerates each class property (ie, use describeType) and in a for-loop (or two) parse the xml, and assign listeners for property changes.