我有一个游戏,你和NPCs互动,他们给出了多个答案。我已经搜索过教程和演示,但它们大多都与Unity相关。
我是Javascript的新手,所以我不知道从哪里开始使用文本对话系统,它允许我(1)在初始“点击”时显示文本(我已经能够这样做了),(2)给出问题的分支答案,(3)并在特定文本行结束,(4)同时能够按"Enter“键继续对话。
我现在能想到的唯一方法就是使用大量的If语句。但是有没有一种更干净的方法来做这件事呢?
发布于 2013-10-25 21:19:55
一种方法是创建一个函数,其中输入是用户选择的:
function askNPC(question) {
switch(question){
case 'buy sword':
return 'here you go!';
break;
case 'sell fish':
return 'here you go!';
break;
}
}
var answer = askNPC('buy sword');
var answer = askNPC('sell fish');另一种方法是将所有问题和答案存储在一个对象中:
var questions = {
'buy sword': 'here you go',
'sell fish': 'thank you'
}
function askNPC(question){
if(typeof questions[question] !== "undefined"){
return questions[question];
} else {
return 'Did not understand you question!';
}
}
var answer = askNPC('buy sword');
var answer = askNPC('sell fish');发布于 2013-10-13 13:28:55
是的,请查看以下有关交换机的页面:
https://stackoverflow.com/questions/19330642
复制相似问题