2012-10-23 75 views
1

所以,這就是我想要做的,坦率地說,我相信它應該是顯而易見的,但我無法弄清楚。我正在創建一個非常簡單的人工智能模擬。在這個模擬中,屏幕底部有一個輸入框(正好叫做「輸入」)。 「輸入」在其屬性中有一個變量,它被稱爲「收件箱」(正好)。使用鍵監聽器時,腳本在按下輸入按鈕時調用函數。這個函數有2個if語句和一個用於規定AI響應的else語句(命名爲「nistra」)。問題是這樣的,當我輸入我想說的內容並按回車鍵時,它總是使用第二個響應(下面代碼中的「lockpick」)。我已經嘗試了代碼的變體,但我仍然沒有看到解決方案。我認爲問題在於「typein」變量包含文本框以及變量中的所有格式信息,但我可能錯了,該​​信息也位於代碼本身下方。任何幫助,我可以得到將不勝感激。actionscript 2.0輸入文本框

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> 

回答

0

由於動作腳本是基於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"; 
} 
+0

不幸的是,沒有工作,使用改變它只是開始響應與其他案件的一切。 「我不明白」。即使這是一個正確的輸入。 – user1769760

+0

也想指出,我只是糾正了它的痕跡,當我第一次發佈這個問題時,它似乎粘貼了錯誤的信息。 – user1769760

+0

好吧,我目前正在玩它,並且我已經添加了3個按鈕,基本上做同樣的事情,而不必鍵入命令。這是按鈕的代碼。 b_hello.onRelease = function() { \t typein =「Hello」; \t nistraresponse(); (「致電」); \t } b_lock.onRelease = function() { \t typein =「lockpick」; \t nistraresponse(); } b_dontunderstand.onRelease = function() { \t typein =「dont get it」; \t nistraresponse(); } 這是正常工作 – user1769760