0

我有许多以“问题”开头然后以数字结尾的字符串变量。(“问题 1”)每个变量都有一个问题(“它说 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显示问题的实际文本?(我现在的代码显然不起作用!!)

但是这个例子似乎不起作用:我创建了一个名为Question的类,代码如下:

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"
}

为什么???

4

2 回答 2

2

您可以使用方括号运算符按名称引用变量[],例如:

this["Question" + QuestionNumber.toString()]

您可以使用此运算符动态设置和检索对象属性的值。

将问题编号保持为整数,您的函数将是:

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()];
}
于 2013-05-30T04:09:12.630 回答
1

这是编程中一个非常基本的概念,除非你很好地理解它,否则它会让很多事情变得更难做,而且如果没有一些基础工作就很难解释:

Object这里发生的事情最容易用普通的旧而不是类来谈论,所以让我们从一个非常简单的例子开始:

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

请注意,Object您不必number提前说它存在,它会在您设置它时创建。现在,当你说要么question1.number你得到1,显然。然而,正在发生的事情是首先question1获取您存储在变量中的(即),然后获取存储在存储在该值中的属性中的值:。 question1{ number: 1 }.number number1

为了节省一些输入,您可以使用一种称为“对象文字”的速记:

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

Nowquestion1是一个具有 3 个属性的对象,其中一个answers, 是一个具有 5 个属性的对象:abcdcorrect。这也可以写成:

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)中具有特殊含义:当您编写的代码在其中时,它泛指对象:对于“全局”代码(不是在 a function) 中,var向这个对象添加属性:var number = 2;并且this.number = 2在这里是一样的。(当你在 时,情况并非如此,在那里表现不同,有时以非常奇怪的方式,所以要小心!)functionthis

现在您可能会开始看到正在发生的事情:[]例如,当您使用 时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
    }
}

你可以用数组做更多的事情,所以当你有时间的时候阅读它们。

于 2013-05-31T00:54:18.993 回答