[英]Want to use Clippy.JS' agent object outside of load function
I am using Clippy.JS, this is a fun little Javascript library that resurrects Microsofts assistant. 我正在使用Clippy.JS,这是一个有趣的小Javascript库,可以复活Microsoft的助手。
Say I wish to summon the wizard Merlin: 假设我想召唤巫师梅林:
clippy.load('Merlin', function(agent){
// do anything with the loaded agent
agent.show();
agent.moveTo(100,100);
agent.speak("Arthur, you are the chosen one to slay the dragon");
});
This works and is easy to implement. 这有效并且易于实现。 The problem arises when I want to move Merlin around: 当我想移动Merlin时出现问题:
$( "#target" ).click(function() {
agent.moveTo(333,333);
});
The agent object is not inititalized in this scope and I don't know how to retrieve the agent object once it is loaded. 代理对象在此范围内未初始化,并且不知道如何在加载后检索代理对象。
The console gives this error: 控制台显示此错误:
Uncaught ReferenceError: agent is not defined
agent
is not a global variable and is only available within your agent callback function. agent
不是全局变量,仅在您的代理回调函数中可用。
To get around this you need to make a create a variable outside of the callback function and then use this to action the agent globally. 为了解决这个问题,您需要在回调函数之外创建一个变量,然后使用它在全局范围内操作代理。
The following should work. 以下应该工作。
//define the personal agent outside the callback function
let merlin;
clippy.load('Merlin', function(agent){
merlin = agent;
merlin.show();
merlin.moveTo(100,100);
merlin.speak("Arthur, you are the chosen one to slay the dragon");
});
$( "#target" ).click(function() {
merlin.moveTo(333,333);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.