this.onEnterFrame=function (){
if(已经出版){
购买;
delete this.onEnterFrame;
}
else
继续等待;
}
当用基于事件的程序设计来处理这两者之间的关系时,读者只需要在家等待来自出版社的通知就行了,如果书已经出版,出版社给你打电话发出通知:“书已经出版了,你过来买一下。”然后你就出发去购买,这样你的就不必要每间隔一段时间就要跑去杂志社查询。这里出版社就是事件源,你就是监听者,监听来至出版社的“购买”事件。
我们要广播自已的事件就必须要用到AsBrosdcaster对象,在flash mx 中此对象是ASBrosdcaster,还是个隐藏对象,开始还已为此方法在2004版本中给拿掉了,用书中的另一隐藏对象ASSetPropFlags遍历_global对象发现变是大"S"变成小"s"了:)
任何一个自定义对象想要成为事件源,就必需要有这_listeners(一个数组,用来存储监听者对象的列表),removeListener(方法,删除监听者),addListener(方法,增加监听者),broadcasMessage(方法,广播事件)4个关键的事件源特征。要用AsBrosdcaster对象的initialize方法初始化自定义对象后,自定义对象就能富含这四个关键特征。更改之后的NewsFeed类:
NewsFeed.as
class NewsFeed
{
/*下面的三个方法在AS2.0中在使用前还必须要进行声明,(感谢blueidea中坛友noahgenius
的提醒)不然无法使用,在AS1.0中不需要,实在是不理解MM要多此一举*/
var addListener : Function;
var removeListener : Function;
var broadcastMessage : Function;
var name : String;
function NewsFeed (name)
{
this.name = name;
AsBroadcaster.initialize (this);
addListener (this);
}
function toString () : String
{
return this.name;
}
function sendNews (headline : String, summary : String, url : String) : Void
{
broadcastMessage ("onNews", this, headline, summary, url);
}
natPress = new NewsFeed ("National Press");
NABC = new Object();
AICN = new Object();
NABC.onNews = function (source, headline, summary, url) {
trace ("----- NABC News -----");
trace (">" + headline + "<");
trace (summary);
trace (">>" + url);
trace ("[via" + source +"]");
trace ("--");
};
AICN.onNews = function (source, headline, summary, url) {
trace ("///// Ain't It Crazy News /////");
trace ("Unbelievable!!");
trace (headline + "!!!");
trace (source + " reports: '" + summary + "'");
trace ("Read the rest of the incredible story here:");
trace (url);
trace ("//");
};
natPress.addListener (NABC);
natPress.addListener (AICN);
trace (natPress._listeners); // output: [object Object],[object Object]
natPress.sendNews ( "Marcosoft buys Murkimedia",
"After months of secret negotiations, Marcosoft absorbs its chief rival",
"http://www.marcosoft.com" )









