2013-05-30 29 views
0

我有許多以「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()] 

可在使用此操作符動態設置和對象的屬性檢索值:

回答

1

這是編程中非常基本的概念,這將使很多事情做起來難,直到你充分理解它,和它的相當困難沒有一些基礎開始解釋:

這裏發生了什麼是最簡單的談與普通的舊Object而不是類,所以讓一個非常簡單的例子開始:

var question1:Object = new Object(); 
question1.number = 1; 

請注意,Object你不必說number提前存在,它會在您設置時創建。現在,當你說要麼question1.number你顯然是1。這是怎麼回事,不過是第一question1得到你存儲在變量question1(這是{ number: 1 }),那麼.number獲取存儲在財產number存儲在該值的價值:1

爲了節省一些打字,你可以使用一個名爲「對象文本」的簡寫:

var question1 = { 
    number: 1 
}; 

現在讓我們嘗試一個更復雜的對象:

var question1 = { 
    number: 1, 
    text: "How many times does it say A?", 
    answers: { 
     a: 1, 
     b: 2, 
     c: 3, 
     d: 4, 
     correct: "b" 
    } 
}; 

現在question1是有3個對象屬性,其中之一,answers,是一個有5個屬性的對象:a,b,c, dcorrect。這也可以寫成:

var question1 = new Object(); 
question1.number = 1; 
question1.text = "How many times does it say A?"; 
question1.answers = new Object(); 
question1.answers.a = 1; 
question1.answers.b = 2; 
question1.answers.c = 3; 
question1.answers.d = 4; 
question1.answers.correct = "b"; 

它應該很清楚爲什麼字面語法現在存在!

這個時候,如果你說question1.answers.correct"b":第一question1讓你的{ number: 1, ... }值,則.answers得到{ a: 1, b: 2, ... }值,然後最後.correct得到"b"值。

你也應該知道,this是具有ActionScript中的特定含義(和JavaScript,在其基礎)的特殊變量:它廣泛是指當你正在寫的代碼是裏面的對象:對於「全局」代碼(不在function中),var爲此對象添加了屬性:var number = 2;this.number = 2在這裏是一樣的。 (當你在function是這真實,this的行爲方式有所不同,有時很奇怪的方式,所以一定要小心!)

現在,你可能會開始看到發生的事情:當您使用[],例如, question1["number"]而不是question1.number,您傳遞的屬性名稱爲String,這意味着您可以更改在運行時獲得的屬性,而不是在編譯時(「運行時」與「編譯時」 「),但它讓你獲得你不能用.語法引用的名字!

var strange = { 
    "a strange name? That's OK!": 1 
}; 
trace(strange["a strange name? That's OK!"]); 

所以,當你寫this["Qu" + QuNoLoad.toString() + ".QuNo"],您創建一個名稱,如"Qu2.QuNo",例如,你想獲取屬性與確切名稱,.包括在內,這不存在!你試圖做的相當於:Qu2.QuNo可以寫成this["Qu" + QuNoLoad].QuNo

不用說了,雖然我不應該離開這一點,對於這樣的事情,我會用數組,存在這樣您就可以使用一個名稱來存儲值的列表:

var questions:Array = [ // set questions to an array with multiple questions 
    new Question(...), 
    new Question(...), 
    ... 
]; 

for each (var question:Question in questions) { // Look at each question in the array 
    if (question.QuNo == textBoxQuLoad.text) { // If this is the right question 
     loadQuestion(question); 
     break; // Found it, stop looking at each question by "breaking out" of the for each 
    } 
} 

你可以用數組做更多事情,所以當你花時間閱讀它們的時候。

+0

謝謝!非常清楚!! – user2434923

2

可以按名稱使用方括號[]操作者,如引用的變量。

保持的問題編號爲整數,你的函數是:

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?" ; 

function displayQuestion(event:MouseEvent):void 
{ 
    var QuestionNumber:uint = uint(userInputQuestionNumber.text); 

    textBoxDisplayQuestion.text = this["Question" + QuestionNumber.toString()]; 
} 
+0

謝謝 - 它的工作。 – user2434923

+0

但新的例子(上述)不起作用!!? – user2434923

+0

您不能在變量名中使用句點'.'。如果它是一個成員,那麼你想要:'這[[Qu] + QuNoLoad.toString()]。Qu'提出一個新問題,而不是擴展你現有的問題。 –