我有許多以「Question」開頭,然後以數字結尾的字符串變量。 (「Question1」) 每個變量都有一個問題(「它說E的次數是多少次」) 在舞臺上有一個可編輯的文本框,用戶鍵入要在不同的問題號碼中顯示的問題編號文本框。 (「1」) 當用戶點擊一個按鈕時,我希望文本框中顯示文本Question1。 我的代碼如下所示:如何合併兩個變量的內容
var Question1:String = "How many times does it say E?" ;
var Question2:String = "How many times does it say B?" ;
var Question3:String = "How many times does it say A?" ;
myButton.addEventListener(MouseEvent.CLICK, displayQuestion);
function displayQuestion(event:MouseEvent):void
{
var QuestionNumber:Number = Number(userInputQuestionNumber.text);
textBoxDisplayQuestion.text= Question(QuestionNumber);
}
我怎樣才能獲得textBoxDisplayQuestion顯示問題的實際文本? (代碼我現在已經很明顯是不工作!)
但是這個例子似乎沒有工作:我創建了一個名爲問題類和這裏是代碼:
import Question;
var QuNoLoad:Number;
var Qu1:Question = new Question(1,"how","yes","no","maybe","so","AnsB","AnsA");
trace(Qu1.QuNo, Qu1.Qu, Qu1.AnsA,Qu1.AnsB, Qu1.AnsC, Qu1.AnsD, Qu1.CorAns, Qu1.FaCorAns);
//the following is the code for the button
loadQu.addEventListener(MouseEvent.CLICK, loadQuClick);
function loadQuClick(event:MouseEvent):void
{
//this sets the variable "QuNoLoad" with the contents of the "textBoxQuLoad"
//imagine the user inputed "1"
QuNoLoad=Number(textBoxQuLoad.text);
//this SHOULD!! display the contents of "Qu1.Qu"
textQu.text= this["Qu"+QuNoLoad.toString()+".Qu"]
//and when i traced this statment the value was "undefined"
}
爲什麼? ?
this["Question" + QuestionNumber.toString()]
可在使用此操作符動態設置和對象的屬性檢索值:
謝謝!非常清楚!! – user2434923