Hello :)
1 - Object watch failed with virtual properties (_x, _y, _width etc....)..
don't use addProperty and watch method !!
In your example you must return the new value in the change method !!!
var o:Object = {} ;
o.prop = 0 ;
var change = function ( id , oldValue, newValue)
{
trace("id : " + id + ", change " + oldValue + " to " + newValue) ;
mc._x = Math.round(newValue) ;
return newValue ;
}
o.watch("prop", change) ;
o.prop = 20 ;
o.prop = 30 ;
o.prop = 30 ;
o.prop = 40 ;
o.prop = 100 ;
2 - in your the constructor must be the same name with the class name !! If
the name of your class is MyUIObject .. your constructor must have the same
name :)
3 - is AS2 used get and set keywords to create virtual properties and don't
use addProperty method !
3 - You can use in your example the Asbroadcaster class to inject
addListener, removeListener and broadcastMessage method in your class :
class MyUIObject
{
/**
* Constructor of the class.
*/
public function MyUIObject()
{
}
/**
* Inject addListener, removeListener and broadcastMessage methods in
the prototype of the class.
*/
static private var __INITBROADCASTER__ = AsBroadcaster.initialize(
MyUIObject.prototype ) ;
// Public Properties
/**
* The code of this method is injected by the AsBroadcaster tool.
* Use this declaration in the AS2 compilation.
*/
public var addListener:Function ;
/**
* The code of this method is injected by the AsBroadcaster tool.
* Use this declaration in the AS2 compilation.
*/
public var broadcastMessage:Function ;
/**
* The code of this method is injected by the AsBroadcaster tool.
* Use this declaration in the AS2 compilation.
*/
public var removeListener:Function ;
/**
* (read-write) Returns the x position of the ui object.
*/
public function get x():Number
{
return getX() ;
}
/**
* (read-write) Sets the x position of the ui object.
*/
public function set x( value:Number ):Void
{
setX( value ) ;
}
// Public Methods
/**
* Returns the x position of the UI object.
*/
public function getX():Number
{
return _x;
}
/**
* Sets the x position of the UI object.
*/
public function setX( value:Number ):Void
{
_x = value ;
broadcastMessage("onX" , this, _x) ;
}
// Private Properties
/**
* The internal x position of the UI object.
*/
private var _x:Number ;
}
You can now use this class and this event model :
var onX = function ( who , x )
{
trace("onX : " + who + " with the value : " + x) ;
}
var ui:MyUIObject = new MyUIObject();
ui.addListener(this) ;
ui.x = 25 ;
EKA+ :)
2006/11/30, Matthias Dittgen <[EMAIL PROTECTED]>:
Hello,
I often need to recognize for some of my gui elemets when the embedded
gui elements (childs) have changed or vice-versa the parent elements
has changed in a property, like "_x", "_width", etc. to repaint the
necessary elements of the GUI.
So what is the best way to do this?
I stumbled over the methods of Object to add or watch properties. This
allows myself to build something like this:
class MyUIObject extends Object
{
public var _x:Number;
public function GUIObject() {
this.addProperty("_x", getX, setX);
this.watch("_x",onChange,{test: 123}); // possibility 2
}
// possibility 2
private function onChange(prop, oldVal, newVal, userData):Boolean
{
if (prop=="_x")
{
onX(newVal);
return true;
} else {
return false;
}
}
private function getX(Void):Number {
return _x;
}
private function setX(x:Number):Void {
onX(x); // possibility 1
}
public function onX(x:Number):Void {
}
}
This way I can set and get _x:
var muo:MyUIObject = new MyUIObject();
trace("1: "+muo._x)
muo._x.onX = function(x)
{
trace("2: "+this._x);
trace("3: "+x);
}
muo._x = 100;
trace("4: "+muo._x)
But the onX method is invoked BEFORE _x is actually set, why?
Output:
1: undefined
2: undefined
3: 100
4: 100
Is there a better way to have an onX method, which perhaps is invoked
immediately after _x was set?
Thank you,
Matthias
_______________________________________________
[email protected]
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com
_______________________________________________
[email protected]
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com