所以问题是我无法在quizIntent处理程序的循环中循环遍历问题的数组。只显示第一个问题,然后它不再移动。其余的代码工作正常。 我想要做的是继续提问,直到数组用完它们并在用户给出错误答案时突破循环。
我只是Alexa Programming的新手。请帮忙。
这是我的数组
const got = [
{
question: "Grey Wind, Lady, Ghost, Shaggydog, Summer and the sixth direwolve's name is?",
answer: 'nymeria',
},
{
question: "What was the name of the sinister castle where Arya and Gendry were held prisoner in season two?",
answer: 'harrenhal',
},
{
question: "What is a person called that can enter the minds of animals?",
answer: 'warg',
},
{
question: "What was the name of the Stark ancestral sword that was melted down by Tywin Lannister?",
answer: 'ice',
}
];
这是我的Alexa意图
var handlers = {
"customIntent": function () {
this.response.speak("Would you like to appear for a trial by combat");
this.emit(":responseReady");
},
"quizIntent": function () {
var mydecision = this.event.request.intent.slots.decision.value;
if(mydecision=='no'||mydecision=='nope'||mydecision=='naah'){
this.response.speak("A five year old has more courage than you.");
this.emit(":responseReady");
}
for(var i = 0; i <got.length; i++){
var myanswer = this.event.request.intent.slots.answer.value;
var item = got[i].question;
this.response.speak(item).listen();
if(myanswer!=got[i].answer){
this.response.speak("Wrong Answer. You are dead");
this.emit(':responseReady');
}
if(i==got.length-1){
this.response.speak("You won");
this.emit(':responseReady');
}
}
},
"LaunchRequest": function () {
this.response.speak("Valar Morghulis").listen("You are supposed to say Valar Dohareis");
this.emit(":responseReady");
}
};
这是我的意图架构,以防你想看一下
{
"languageModel": {
"types": [
{
"name": "answerSlot",
"values": [
{
"id": null,
"name": {
"value": "Nymeria",
"synonyms": []
}
},
{
"id": null,
"name": {
"value": "Harrenhal",
"synonyms": []
}
},
{
"id": null,
"name": {
"value": "Warg",
"synonyms": []
}
},
{
"id": null,
"name": {
"value": "Ice",
"synonyms": []
}
}
]
},
{
"name": "decisionSlot",
"values": [
{
"id": null,
"name": {
"value": "Yes",
"synonyms": []
}
},
{
"id": null,
"name": {
"value": "No",
"synonyms": []
}
},
{
"id": null,
"name": {
"value": "Naah",
"synonyms": []
}
},
{
"id": null,
"name": {
"value": "Yeah",
"synonyms": []
}
},
{
"id": null,
"name": {
"value": "Yup",
"synonyms": []
}
},
{
"id": null,
"name": {
"value": "Nope",
"synonyms": []
}
}
]
}
],
"intents": [
{
"name": "AMAZON.CancelIntent",
"samples": []
},
{
"name": "AMAZON.HelpIntent",
"samples": []
},
{
"name": "AMAZON.StopIntent",
"samples": []
},
{
"name": "customIntent",
"samples": [
"Valar Dohareis",
"hello",
"hola"
],
"slots": []
},
{
"name": "quizIntent",
"samples": [
"{decision}",
"The answer is {answer}"
],
"slots": [
{
"name": "decision",
"type": "decisionSlot"
},
{
"name": "answer",
"type": "answerSlot"
}
]
}
],
"invocationName": "quiz game"
}
}
答案 0 :(得分:1)
for循环不能用于任何目的,因为intent旨在为每次调用提供单个响应。因此,虽然你的for循环会运行,但它不会在第一次迭代的listen方法从一开始就开始执行后发出第二个响应,因为它是一个新的调用
使用会话属性可以成功实现您想要达到的目的。
var iterations = 0; //defined globally
// Later on, for every iteration, you simply need to call
// into the attributes property of the alexa object to change the value.
this.attributes['iterations'] = iterations + 1;
答案 1 :(得分:0)
我想出解决这个问题的一种方法是:
var i = 0;
var handlers = {
"customIntent": function () {
this.response.speak("Would you like to take part in a trial by combat?").listen();
this.emit(":responseReady");
},
"quizIntent": function () {
var mydecision = this.event.request.intent.slots.decision.value;
if(mydecision=='no'||mydecision=='nope'||mydecision=='naah'){
this.response.speak("Battles have been won against harder odds! Even little Lyanna Mormont has more courage than you.");
this.emit(":responseReady");
}
if(i<=got.length){
var item = got[i].question;
if(i == 0){
this.response.speak("Be attentive; just like life, I won't repeat or give you a second chance. Here you go; " + item).listen();
this.emit(":responseReady");
}
else {
this.response.speak(item).listen();
this.emit(":responseReady");
}
}
},
"answerIntent": function () {
var myanswer = this.event.request.intent.slots.answer.value;
if(myanswer!=got[i].answer){
this.response.speak("Wrong Answer. The correct answer is " + got[i].answer + ". You are dead.");
this.emit(':responseReady');
}
i++;
if(i==got.length){
i=0;
this.response.speak("You emerged the ultimate victor. The best in all of Planetos.");
this.emit(':responseReady');
}
this.response.speak("You survived. Say ready, when you are, for the next combat!").listen();
this.emit(':responseReady');
},
这个想法是通过测验意图和答案意图来回切换。