→ cjcat2266:Proxy是可以做到此物件任何property被修改都丟事件 05/13 20:59
→ cjcat2266:不然你就弄個Proxy當做一個data holder吧? 05/13 21:00
→ cjcat2266:它擁有的property變更的時候都丟出式見這樣 05/13 21:00
→ etrexetrex:如果能動態的幫一個屬性加丟出事件也是可以 05/13 21:14
使用Proxy監聽property變更的方法如下
1. 繼承Proxy class,需要有dynamic modifier
2. override setProperty(name:*, value:*)和getProperty(name:)
注意namespace是flash_proxy
3. implement IEventDispatcher
實作所有此interface標明的methods
已達到丟事件的目的
其實這個proxy就只是把一個Object包裝起來
並且側錄存取此Object的property之所有行為而已
這是你的事件class
package {
import flash.events.Event;
public class PropertyChangeEvent extends Event{
public static const CHANGE:String = "propertyChange";
private var _name:*;
public function get name():* { return _name; }
private var _value:*;
public function get value():* { return _value; }
public function PropertyChangeEvent(
type:String,
name:*,
value:*) {
super(type, false, false);
_name = name;
_value = value;
}
}
}
}
這是你的Proxy class
最重要的method是setProperty(name:*, value:*)
其他的methods都很好理解
package {
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.IEventDispatcher;
import flash.utils.flash_proxy;
import flash.utils.Proxy;
public dynamic class DataProxy
extends Proxy
implements IEventDispatcher {
private var _dataHolder:Object = new Object();
private var _dispatcher:EventDispatcher =
new EventDispatcher();
public function addEventListener(
type:String,
listener:Function,
useCapture:Boolean = false,
priority:int = 0,
useWeakReference:Boolean = false):void {
_dispatcher.addEventListener(
type,
listener,
useCapture,
priority,
useWeakReference
);
}
public function removeEventListener(
type:String,
listener:Function,
useCapture:Boolean = false):void {
_dispatcher.removeEventListener(
type,
listener,
useCapture
);
}
public function dispatchEvent(event:Event):Boolean {
return _dispatcher.dispatchEvent(event);
}
public function hasEventListener(type:String):Boolean {
return _dispatcher.hasEventListener(type);
}
public function willTrigger(type:String):Boolean {
return _dispatcher.willTrigger(type);
}
override flash_proxy function getProperty(name:*):* {
return _dataHolder[name];
}
override flash_proxy function setProperty(
name:*,
value:*):void {
_dataHolder[name] = value;
dispatchEvent(
new PropertyChangeEvent(
PropertyChangeEvent.CHANGE,
name,
value
)
);
}
}
}
這是測試用的小程式
package {
import flash.display.Sprite;
import flash.events.Event;
public class Main extends Sprite {
public function Main():void {
var dataProxy:DataProxy = new DataProxy();
dataProxy.addEventListener(
PropertyChangeEvent.CHANGE,
onPropertyChange
);
dataProxy.x = 10;
dataProxy.y = 20;
/**
* OUTPUT
* --------------------------------
* property x has changed to: 10
* property y has changed to: 20
*/
}
private function onPropertyChange(
e:PropertyChangeEvent):void {
trace(
"property" + e.name +
"has changed to: " + e.value
);
}
}
}
--
CJ Cat = Croa'J Cat = Cockroach Cat = 西街凱特 = 蜚蠊貓 = 蟑螂貓
Blog http://cjcat.blogspot.com
Gallery http://cjcat2266.deviantart.com
ptt2 Board CJWorkshop - 阿多比閃光(Adobe Flash)研討區
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 114.45.164.148
※ 編輯: cjcat2266 來自: 114.45.164.148 (05/13 21:55)
推 etrexetrex:謝啦 05/13 22:09