1

所以,这就是我想要做的,坦率地说,我相信这应该是显而易见的,但我无法弄清楚。我正在创建一个非常简单的人工智能模拟。在这个模拟中,屏幕底部有一个输入框(精确地称为“输入”)。“输入”在其属性中有一个变量,称为“收件箱”(确切地说)。使用按键侦听器,脚本在按下回车按钮时调用一个函数。该函数有 2 个 if 语句和一个 else 语句,它们指示 AI 的响应(名为“nistra”)。问题是,当我输入我想说的话,然后按回车键时,它总是使用第二个响应(下面代码中的“lockpick”)。我已经尝试了代码的变体,但我仍然没有看到解决方案。我认为问题在于“typein”变量包含来自文本框以及变量的所有格式信息,但我可能错了,该​​信息也在这里,位于代码本身的下方。我能得到的任何帮助将不胜感激。

var typein = ""; //copies the text from inbox into here, this is what nistra responds to
var inbox = ""; //this is where the text from the input text box goes
var respond = ""; //nistra's responses go here
my_listener = new Object(); // key listener
my_listener.onKeyDown = function()
{
    if(Key.isDown(13)) //enter button pressed
    {
        typein = inbox; // moves inbox into typein
        nistraresponse(); // calles nistra's responses
    }
    //code = Key.getCode();
    //trace ("Key pressed = " + code);
}

Key.addListener(my_listener); // key listener ends here

nistraresponse = function() // nistra's responses
{
    trace(typein); // trace out what "typein" holds
    if(typein = "Hello") // if you type in "Hello"
    {
        respond = "Hello, How are you?";
    }
    if(typein = "lockpick") // if you type in "lockpick"
    {
        respond = "Affirmative";
    }
    else // anything else
    {
        respond = "I do not understand the command, please rephrase";
    }
    cntxtID = setInterval(clearnistra, 5000); // calls the function that clears out nistra's response box so that her responses don't just sit there
}

clearnistra = function() // clears her respond box
{
    respond = "";
    clearInterval(cntxtID);
}

// "typein" 跟踪以下内容

<TEXTFORMAT LEADING="2"><P ALIGN="CENTER"><FONT FACE="Times New Roman" SIZE="20" COLOR="#FF0000" LETTERSPACING="0" KERNING="0">test</FONT></P></TEXTFORMAT>
4

1 回答 1

0

由于 ActionScript 基于 ECMAScript,我很确定您需要使用==而不是=进行相等比较。

现在你的代码是这样工作的:

if(typein = "Hello") { // assign "Hello" to typein. always true.
    respond = "Hello, How are you?";
}
if(typein = "lockpick") { // assign "lockpick" tot ypein. always true.
    respond = "Affirmative";
}
// the else block is always false for obvious reasons

所以你只需要像这样更改代码:

if(typein == "Hello") {
    respond = "Hello, How are you?";
}
else if(typein == "lockpick") {
    respond = "Affirmative";
}
else {
    respond = "I do not understand the command, please rephrase";
}
于 2012-10-23T22:53:53.183 回答