转:http://www.blueidea.com/tech/web/2004/1643.asp 作者windy_sk 邮箱:windy_sk@126.com
下面是原文:
本文完全原创,所有参考都是
msdn.microsoft.com/library/en-us/msagent/agentstartpage_7gdh.asp
引子:
本来两年前就打算写了,结果拖了这么长时间,近日看到有朋友问及相关的东西,终于决定在还没有完全遗忘之前把自己残存的记忆表达出来,纸、笔…写!
不要说你完全了解 MSAgent,可能你对它的认识也不过是知道而已~~~~~~~~
开篇:
可能 MSAgent 这个名字你不清楚,但如果提起 Office 助手我想在这个目前 MicroSoft 独霸天下的电脑世界应该鲜有人不知道,本文就是交给你如何在网页中调用这个在线尤物。
入题:
一、抛砖引玉
首先,我们先来看一下一个最简单的效果:
- “visibility:hidden” id=“MSAgent” classid=“CLSID:D45FD31B-5C6E-11D1-9EC1-00C04FD7081F”>
- <script language=< span=””>“JavaScript”>
- //Coded by Windy_sk <windy_sk@126.com> 20040214
- var Agent = null;
- var AgentID = “Merlin”;
- var AgentACS = “merlin.acs”;
- MSAgent.Connected = true;
- MSAgent.Characters.Load(AgentID,AgentACS);
- Agent = MSAgent.Characters.Character(AgentID);
- Agent.Show();
- </script>
AgentID 内部索引字串,由用户定义;
AgentACS 所调用的角色文件,可以为浏览者本地或远程文件,后面会有单独的部分说明。
MSAgent.Connected 建立连接;
MSAgent.Characters.Load 读取角色;
MSAgent.Characters.Character 返回角色对象;
Agent.Show 显示角色;
好了,我现在已经把 MSAgent 带到你面前了。什么?什么东西都没看到?只有浏览器的报错!没关系,在后面的文章里,我也会告诉你如何才能看到他,当然,这个看到指的是所有浏览你网页的人!
二、伶牙俐齿
下面,我们就让他来做一点实际的东西 —— 说话!还是接续上例:
- “visibility:hidden” id=“MSAgent” classid=“CLSID:D45FD31B-5C6E-11D1-9EC1-00C04FD7081F”>
- <script language=< span=””>“JavaScript”>
- //Coded by Windy_sk <windy_sk@126.com> 20040214
- var Agent = null;
- var AgentID = “Merlin”;
- var AgentACS = “merlin.acs”;
- MSAgent.Connected = true;
- MSAgent.Characters.Load(AgentID,AgentACS);
- Agent = MSAgent.Characters.Character(AgentID);
- Agent.LanguageID = 0x0409;
- Agent.Show();
- Agent.Speak(“Hello Everybody, I am Merlin!”);
- Agent.Think(“What shall I do the next?”);
- </script>
Merlin 说话了(如果要读出声音来的话,需要客户端在 MicroSoft 的网站上下载并安装相应的语音引擎)!这里涉及到这么几个新的东西:
Agent.LanguageID 声明语言种类,0x0409是英文的编号(有关语言编号请参考 www.microsoft.com/globaldev/reference/oslocversion.mspx ),目前如果没有这个声明,或声明为错误的语种,则语言只是一次性完全显示。
Agent.Speak() 和 Agent.Think() 是 MSAgent 的两个语言表达显示行为,只有显示图形的区别。
了解了这些功能,是不是正在陶醉呀?别急,还有更好的东西呢!
三、活灵活现
总是看着一个呆头呆脑的东西一动不动,即使是很 Q ,也会有感到多少的厌烦,下面我们就让他动起来。
这个例子由于调用的是网络文件,所以会慢一点,请耐心等待一下!
- “visibility:hidden” id=“MSAgent” classid=“CLSID:D45FD31B-5C6E-11D1-9EC1-00C04FD7081F”>
- <script language=< span=””>“JavaScript”>
- //Coded by Windy_sk <windy_sk@126.com> 20040214
- var Agent = null;
- var AgentID = “Merlin”;
- var AgentACS = “http://agent.microsoft.com/agent2/chars/merlin/merlin.acf”;
- var AgentStates = “Showing, Hiding, Speaking, Moving”;
- var AgentAnimations = “GetAttention, GetAttentionReturn, Congratulate, Acknowledge, Read, WriteContinued, WriteReturn, wave”;
- var AgentLoadRequest, AgentStateRequest, AgentAnimationRequest;
- MSAgent.Connected = true;
- AgentLoadRequest = MSAgent.Characters.Load(AgentID,AgentACS);
- Agent = MSAgent.Characters.Character(AgentID);
- Agent.LanguageID = 0x0409;
- AgentStateRequest = Agent.get(“state”, AgentStates);
- AgentAnimationRequest = Agent.get(“animation”, AgentAnimations);
- Agent.Show();
- Agent.MoveTo(400,300);
- Agent.Play(“GetAttention”);
- Agent.Play(“GetAttentionReturn”);
- Agent.speak(“Hi, may I have your attention, please?”);
- Agent.Play(“Congratulate”);
- Agent.speak(“So nice to meet you!”);
- Agent.Play(“Think”);
- Agent.speak(“How do think about me?”);
- Agent.Play(“Acknowledge”);
- Agent.Speak(“It’s very cool, ya?”);
- Agent.Play(“Read”);
- Agent.Play(“WriteContinued”);
- Agent.Play(“WriteReturn”);
- Agent.Speak(“Oh, I have lots of things to do, see you !”);
- Agent.Play(“wave”);
- Agent.Speak(“Bye-bye!”);
- Agent.Hide();
- </script>
看到没有?其实只要你善于调动它的积极性,MSAgent 也蛮活泼的!信息观察,不难发现,原来让 MSAgent 动起来,也不过就这么简单:
Agent.MoveTo(x, y) 是角色移动到指定的坐标;
Agent.Play(action) 命令角色做某个动作,动作列表见: msdn.microsoft.com/library/default.asp?url=/library/en-us/msagent/deschar_3pgy.asp (这里需要说明一下,不是所有的角色都支持这些动作的,处理方法后面会有说明!)
Agent.Hide() 隐藏角色(不是释放角色,通过 Agent.Show() 可以再次显示)
Agent.get(Request, list) 预载相关 MSAgent 动画数据,MSAgent人物数据文件支持单结构角色文件(.acs,角色数据与动画数据存于同一个文件),也支持分离结构角色文件(.acf,角色数据存于.acf中,动画数据存于.aca中)。基于本地硬盘和网络调用均可采用这两种模式,当调用网络 acf 文件时,由于角色数据与动画数据分别下载,所以需要预载相关动画数据,使用 acs 文件(一般没有本地 acf 文件的可能性),不需要预载。
AgentLoadRequest, AgentLoadRequest 和 AgentAnimationRequest 这三个参数本例并没有实际用到,返回应相关操作的状态对象(相当于 readystatus 属性),在调用网络 acf 文件时有比较实际的用途,这个会在后面说明!
四、改头换面
MSAgent = Merlin ? 错!MSAgent 是指一系列动画人物的总称,最常见的 office 中的那些活宝,各位应该都熟悉吧?先来看看下面的这个例子:
- “visibility:hidden” id=“MSAgent” classid=“CLSID:D45FD31B-5C6E-11D1-9EC1-00C04FD7081F”>
- <script language=< span=””>“JavaScript”>
- //Coded by Windy_sk <windy_sk@126.com> 20040214
- var Agent = null;
- var AgentID, AgentACS;
- var AgentLoad = false;
- function LoadAgent(NewAgent) {
- if(AgentLoad) {
- MSAgent.Characters.Unload(AgentID);
- MSAgent.Connected = false;
- Agent = null;
- }
- AgentID = NewAgent;
- AgentACS = “http://agent.microsoft.com/agent2/chars/” + NewAgent + “/” + NewAgent + “.acf”;
- MSAgent.Connected = true;
- MSAgent.Characters.Load(AgentID, AgentACS);
- AgentLoad = true;
- Agent = MSAgent.Characters.Character(AgentID);
- Agent.get(“state”, “Showing, Hiding”);
- Agent.MoveTo(400, 300);
- Agent.Show();
- return;
- }
- LoadAgent(“Merlin”);
- </script>
- MSAgent Select :
- “Agent_select” οnchange=“LoadAgent(this[this.selectedIndex].text)”>
- Merlin
- Peedy
- Genie
- Robby
看到没有? 原来 MSAgent 还有这么多可爱的造型呢!上面的例程中,我定义了一个读取角色的函数 LoadAgent ,通过这个函数更换角色,其中的大部分的功能在前面的章节中已经介绍了,这里仅仅说明一下,新的方法:
MSAgent.Characters.Unload() 卸载角色,其中 AgentID ,必须是 MSAgent.Characters.Load() 中声明过的
Agent.MoveTo() 这个方法上一节中介绍过,但是如果把它放在 Agent.Show() ,则相当于定义角色的出现位置
其实,MSAgent 绝对不仅仅是那么多,但是 MicroSoft 的官方网站上只提供了那么多……怎么办?可以从他的网站上连接角色,当然可可以从你的网站上呀!你可以在这里 www.microsoft.com/msagent/downloads/user.asp 下载官方角色以及语音引擎(可惜没有中文);当然,如果你有兴趣的话也可以开发一个属于自己的 Agent ,相关开发工具 www.microsoft.com/msagent/downloads/developer.asp ,网上也有很多高手做好的动画人物,推荐一个网站 www.msagentring.org/chars.htm 你也可以自己搜索一下。
安装后角色文件存放在 %WINDOWS%\msagent\chars 目录下的 *.acs 文件,上传到服务器上,直接引用到那个路径就可以了!(你也可以在你的硬盘里搜索一下 *.acs 会有不小的收获呦)这里要说明一下,请自行更改程序中标明网络路径的相关语句,且注意扩展名是 .acs !
如果要让本机支持相应的 MSAgent ,也就是说不用网络调用,只要把 *.acs 文件 copy 到 %WINDOWS%\msagent\chars 目录就可以了,但如果是 *.exe 的安装文件,则会自动把角色文件放置到相应的路径下。
五、排难解错
能否显示 MSAgent 的关键在于是否安装了 MSAgent 的核心组件
( Microsoft Agent core components – activex.microsoft.com/activex/controls/agent2/MSagent.exe ),但是如何让这一被动行为变为主动呢?可以用下面两种方法:
方法一:
- “visibility:hidden” id=“MSAgent” classid=“CLSID:D45FD31B-5C6E-11D1-9EC1-00C04FD7081F” CodeBase=“http://activex.microsoft.com/activex/controls/agent2/MSagent.exe#VERSION=2,0,0,0”>
特点:自动下载组件并安装,比较方便,但会下载的等待时间不会提示,在网速慢的时候会以为页免死掉,且不是很方便控制。
方法二:
- <script language=< span=””>“javascript”>
- //Coded by Windy_sk <windy_sk@126.com> 20040214
- function Agent_load_error(){
- alert(“To make the MSAgent available, /nplease install Microsoft Agent core components first !”);
- window.open(“http://activex.microsoft.com/activex/controls/agent2/MSagent.exe”);
- return;
- }
- </script>
- “visibility:hidden” id=“MSAgent” classid=“CLSID:D45FD31B-5C6E-11D1-9EC1-00C04FD7081F” οnerrοr=“Agent_load_error()”>
特点:方便出错控制,用户控制下载,但是不能当时显示,需要安装后再次刷新页面。
使用哪种方法就属于见仁见智了,但是最不明智的方法就是两种方法一起上,实践证明 CodeBase 会先于 onerror 生效!
不管怎么说,调用本地角色也比网络角色速度上要快得多,但是你如何预知客户端是否安装了该角色呢?看看下面的例子:
- “visibility:hidden” id=“MSAgent” classid=“CLSID:D45FD31B-5C6E-11D1-9EC1-00C04FD7081F”>
- <script language=< span=””>“JavaScript”>
- //Coded by Windy_sk <windy_sk@126.com> 20040214
- var Agent = null;
- var AgentID, AgentACS;
- var AgentLoad = false;
- function LoadAgent(NewAgent) {
- var remote = false;
- if(AgentLoad) {
- MSAgent.Characters.Unload(AgentID);
- MSAgent.Connected = false;
- Agent = null;
- }
- AgentID = NewAgent;
- AgentACS = NewAgent + “.acs”;
- MSAgent.Connected = true;
- try {
- MSAgent.Characters.Load(AgentID, AgentACS);
- agent_exist.innerText = “Local MSAgent load successfully!”;
- } catch(e) {
- AgentACS = “http://agent.microsoft.com/agent2/chars/” + NewAgent + “/” + NewAgent + “.acf”;
- remote = true;
- MSAgent.Characters.Load(AgentID, AgentACS);
- agent_exist.innerText = “Local MSAgent load unsuccessfully, as a advice, you’d better to download the charactor file to your hard disk!”;
- }
- AgentLoad = true;
- Agent = MSAgent.Characters.Character(AgentID);
- if(remote) Agent.get(“state”, “Showing, Hiding”);
- Agent.MoveTo(400, 300);
- Agent.Show();
- return;
- }
- window.onload = function(){LoadAgent(“Merlin”);};
- </script>
- MSAgent Select :
- “Agent_select” οnchange=“LoadAgent(this[this.selectedIndex].text)”>
- Merlin
- Peedy
- Genie
- Robby
- Load Status: “agent_exist”>
通过设置错误捕获,可以方便的找到调用 MSAgent 的最佳方式,当然,你还可以通过 DHTML 加上相应的角色下载链接,并指导用户将 *.acs 文件 copy 到相应目录(%WINDOWS%\msagent\chars)或直接安装 *.exe 的角色安装文件以方便下次浏览,我仅仅是为你提供一个思路,具体实践还是自己发挥吧!
六、事件响应
像所有 OOP 一样,MSAgent 也设置有相应的事件响应,看看下面的例子,试试在角色或任务栏的图标上点击鼠标(单/双击),你也可以移动一下角色,看看它有什么反应:
- “visibility:hidden” id=“MSAgent” classid=“CLSID:D45FD31B-5C6E-11D1-9EC1-00C04FD7081F”>
- <script language=< span=””>“JavaScript” For=“MSAgent” Event=“Click(CharacterID, Button, Shift, X, Y)”>
- //Coded by Windy_sk <windy_sk@126.com> 20040214
- if(Button==1 && Agent.Visible) {
- if(remote) {
- Agent.get(“state”, “Speaking”);
- Agent.get(“animation”, “Acknowledge, Pleased”);
- }
- Agent.Play(“Acknowledge”);
- Agent.Speak(“Yes sir! “ + CharacterID + ” is right here!”);
- Agent.Play(“Pleased”)
- Agent.Speak(“What can I do for you?”);
- } else if(Button==4097) {
- Agent.Visible?Agent.Hide():Agent.show();
- }
- </script>
- <script language=< span=””>“JavaScript” For=“MSAgent” Event=“DblClick(CharacterID, Button, Shift, X, Y)”>
- //Coded by Windy_sk <windy_sk@126.com> 20040214
- if(Button==1 || Button==4097) {
- Agent.StopAll();
- if (!Agent.HasOtherClients) {
- MSAgent.Characters.Unload(AgentID);
- MSAgent.Connected = false;
- Agent = null;
- AgentLoad = false;
- }
- }
- </script>
- <script language=< span=””>“JavaScript” For=“MSAgent” Event=“Move(CharacterID, X, Y, Cause)”>
- //Coded by Windy_sk <windy_sk@126.com> 20040214
- if(Cause == 1) {
- if(remote) {
- Agent.get(“state”, “Moving, Speaking”);
- Agent.get(“animation”, “Confused, RestPose”);
- }
- Agent.MoveTo(400, 300);
- Agent.Play(“Confused”);
- Agent.Speak(“Don’t move me OK?”);
- Agent.Play(“RestPose”);
- }
- </script>
- <script language=< span=””>“JavaScript”>
- //Coded by Windy_sk <windy_sk@126.com> 20040214
- var Agent = null;
- var AgentID, AgentACS;
- var AgentLoad = false;
- var remote = false;
- function LoadAgent(NewAgent) {
- if(AgentLoad) {
- MSAgent.Characters.Unload(AgentID);
- MSAgent.Connected = false;
- Agent = null;
- }
- AgentID = NewAgent;
- AgentACS = NewAgent + “.acs”;
- MSAgent.Connected = true;
- try {
- MSAgent.Characters.Load(AgentID, AgentACS);
- } catch(e) {
- AgentACS = “http://agent.microsoft.com/agent2/chars/” + NewAgent + “/” + NewAgent + “.acf”;
- remote = true;
- MSAgent.Characters.Load(AgentID, AgentACS);
- if(confirm(“Cannot find the MSAgent charactor file on your hard disk! \nWould you like to download the MSAgent charactor file for the next show?”))
- window.open(“http://www.msagentring.org/download.asp?char=”+NewAgent.toLowerCase(),“_blank”,“top=2000px”);
- }
- AgentLoad = true;
- Agent = MSAgent.Characters.Character(AgentID);
- Agent.LanguageID = 0x0409;
- if(remote) Agent.get(“state”, “Showing, Hiding”);
- Agent.MoveTo(400, 300);
- Agent.Show();
- return;
- }
- LoadAgent(“Merlin”);
- </script>
- MSAgent Select :
- “Agent_select” οnchange=“LoadAgent(this[this.selectedIndex].text)”>
- Merlin
- Peedy
- Genie
- Robby
是不是感觉交互性强了许多?我们来看一下事件处理的声明方法:
<script language=”JavaScript” for=”MSAgent_Object” event=”Event_Name()”>
//code…
</script>
熟悉一点 JS 编程的应该不会陌生这种声明方式,也就是对某一对象某一事件的单独处理的声明方法,但是如果是该成 MSAgent_Object.Event_Name = function() {//code…} 的事件处理声明是不可以的!(the only way to do this)
如果是采用网络调用的话,如果向用户通知相应的调用进度呢?
- “visibility:hidden” id=“MSAgent” classid=“CLSID:D45FD31B-5C6E-11D1-9EC1-00C04FD7081F”>
- <script language=< span=””>“JavaScript” For=“MSAgent” Event=“RequestStart(RequestObject)”>
- //Coded by Windy_sk <windy_sk@126.com> 20040214
- switch (RequestObject) {
- case AgentLoadRequest :
- window.status = “Loading MSAgent File From Internet For “ + AgentID + ” …”;
- break;
- case AgentStateRequest :
- window.status = “Loading MSAgent State From Internet For “ + AgentID + ” …”;
- break;
- case AgentAnimationRequest :
- window.status = “Loading MSAgent Animation From Internet For “ + AgentID + ” …”;
- break;
- default:
- break;
- }
- </script>
- <script language=< span=””>“JavaScript” For=“MSAgent” Event=“RequestComplete(RequestObject)”>
- //Coded by Windy_sk <windy_sk@126.com> 20040214
- switch (RequestObject) {
- case AgentLoadRequest :
- if(RequestObject.Status == 0) {
- window.status = “MSAgent File For “ + AgentID + ” Has Been Loaded Successfully !”;
- } else {
- window.status = “Cannot Load MSAgent File For “ + AgentID + ” From “ + AgentACS + ” !”;
- }
- break;
- case AgentStateRequest :
- if(RequestObject.Status == 0) {
- window.status = “MSAgent State For “ + AgentID + ” Has Been Loaded Successfully !”;
- } else {
- window.status = “Cannot Load MSAgent State For “ + AgentID + ” From “ + AgentACS + ” !”;
- }
- break;
- break;
- case AgentAnimationRequest :
- if(RequestObject.Status == 0) {
- window.status = “MSAgent Animation For “ + AgentID + ” Has Been Loaded Successfully !”;
- } else {
- window.status = “Cannot Load MSAgent Animation For “ + AgentID + ” From “ + AgentACS + ” !”;
- }
- break;
- break;
- default:
- window.status = “”;
- break;
- }
- </script>
- <script language=< span=””>“JavaScript” For=“MSAgent” Event=“DragStart(CharacterID, Button, Shift, X, Y)”>
- //Coded by Windy_sk <windy_sk@126.com> 20040214
- cur_x = X – Agent.width/2;
- cur_y = Y – Agent.height/2;
- </script>
- <script language=< span=””>“JavaScript” For=“MSAgent” Event=“DragComplete(CharacterID, Button, Shift, X, Y)”>
- //Coded by Windy_sk <windy_sk@126.com> 20040214
- if(remote) {
- AgentStateRequest = Agent.get(“state”, “Moving, Speaking”);
- AgentAnimationRequest = Agent.get(“animation”, “Confused, RestPose”);
- }
- Agent.MoveTo(cur_x, cur_y);
- Agent.Play(“Confused”);
- Agent.Speak(“Don’t move me OK?”);
- Agent.Play(“RestPose”);
- </script>
- <script language=< span=””>“JavaScript”>
- //Coded by Windy_sk <windy_sk@126.com> 20040214
- var Agent = null;
- var AgentID, AgentACS;
- var AgentLoad = false;
- var remote = false;
- var cur_x, cur_y;
- var AgentLoadRequest, AgentStateRequest, AgentAnimationRequest;
- function LoadAgent(NewAgent) {
- if(AgentLoad) {
- MSAgent.Characters.Unload(AgentID);
- MSAgent.Connected = false;
- Agent = null;
- }
- AgentID = NewAgent;
- AgentACS = NewAgent + “.acs”;
- MSAgent.Connected = true;
- try {
- AgentLoadRequest = MSAgent.Characters.Load(AgentID, AgentACS);
- } catch(e) {
- AgentACS = “http://agent.microsoft.com/agent2/chars/” + NewAgent + “/” + NewAgent + “.acf”;
- remote = true;
- AgentLoadRequest = MSAgent.Characters.Load(AgentID, AgentACS);
- if(confirm(“Cannot find the MSAgent charactor file on your hard disk! \nWould you like to download the MSAgent charactor file for the next show?”))
- window.open(“http://www.msagentring.org/download.asp?char=”+NewAgent.toLowerCase(),“_blank”,“top=2000px”);
- }
- AgentLoad = true;
- Agent = MSAgent.Characters.Character(AgentID);
- Agent.LanguageID = 0x0409;
- if(remote) {
- AgentStateRequest = Agent.get(“state”, “Showing, Thinking, Hiding”);
- AgentAnimationRequest = Agent.get(“animation”, “GetAttention, GetAttentionContinued, GetAttentionReturn”);
- }
- Agent.MoveTo(400, 300);
- Agent.Show();
- Agent.Play(“GetAttention”);
- Agent.Play(“GetAttentionContinued”);
- Agent.Play(“GetAttentionReturn”);
- Agent.speak(“Hi, I am “ + NewAgent + “!”);
- Agent.think(“Oh so bad, I just wanna take a nap…”);
- return;
- }
- LoadAgent(“Merlin”);
- </script>
- MSAgent Select :
- “Agent_select” οnchange=“LoadAgent(this[this.selectedIndex].text)”>
- Merlin
- Peedy
- Genie
- Robby
注意到窗口底部状态栏的显示了吗?虽然无法获取具体的下载进度,但是至少也可以让浏览者知道角色的动作为什么会有停顿(这个停顿只在某动画第一次调用的时候出现,调用后该动画会被缓存)。
更多的事件说明请参看 msdn.microsoft.com/library/en-us/msagent/paface_2xet.asp 但是浏览器所调用的 MSAgeng 对象并不是所有的事件都支持。
今天的文章mlagents教程_MS是什么游戏分享到此就结束了,感谢您的阅读。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:http://bianchenghao.cn/75495.html