听起来您正试图在正确回答问题时显示下一个问题。但是,您正在做的是尝试增加一个名为 displayQuestion 的变量,该变量不存在。我指的是你的这部分代码:
function checkAnswer(e:MouseEvent):void{
if(answer_tf.text==answerArray[currentQuestion]){
displayQuestion++;
}
}
您确实有一个名为displayQuestion 的函数:如您的代码中所示:
function displayQuestion():void{
question_tf.text=questionArray[currentQuestion];
}
此外,您声明您删除了验证输入的侦听器,但在您显示的代码中没有对removeEventListener 的函数调用。
在尝试在 Flash CC 2014 中运行您的代码、创建必要的 question_tf 和 answer_tf 文本字段实例和 checkAnswer_btn 按钮实例后,我收到以下编译器错误:
场景 1,Layer 'Layer 1',第 1 帧,第 14 行,第 20 列 1107:增量操作数无效。
场景 1,“Layer 1”层,第 1 帧,第 14 行,第 5 列 1067:将 Function 类型的值隐式强制转换为不相关的 Number 类型。
您是否收到任何这些编译器错误?如果您没有遇到 displayQuestion 数据类型问题,我会感到惊讶。
根据您的描述,我只能假设您正在尝试执行以下操作:
var answerArray:Array=['uncle billy','six','178','hello4'];
var questionArray:Array=["what is the name of the guy?","how many people in the family?","how many planks","what do you call a grown boy4"];
var currentQuestion:int=0;
function displayQuestion():void{
clearAnswerField();
question_tf.text=questionArray[currentQuestion];
}
function clearAnswerField():void {
answer_tf.text = "";
}
checkAnswer_btn.addEventListener(MouseEvent.CLICK,checkAnswer);
function checkAnswer(e:MouseEvent):void{
if(answer_tf.text==answerArray[currentQuestion]){
currentQuestion++;
displayQuestion();
}
}
displayQuestion();
上述代码的作用是纠正对displayQuestion的函数调用问题,增加要显示的问题的变量,如果答案正确,则清除答案字段。
然而,有一些非常重要的事情需要纠正,而您的原始代码没有考虑到这些问题:
-
您的checkAnswer 函数还应确保当索引currentQuestion 的值增加到超过可用问题的数量时,它永远不会被使用;否则,当您尝试访问 answerArray 的元素时,您将得到一个数组越界异常
-
在检查答案是否区分大小写时,您的代码也应该小心。如果有人输入“Uncle billy”,你的代码目前不会接受答案;但是,您可能希望接受该答案。更正此问题的安全方法是执行以下操作:
if(answer_tf.text.toLowerCase() == answerArray[currentQuestion].toLowerCase() ) {
currentQuestion++;
displayQuestion();
}
我为您创建的代码在 Flash CC 2014 中运行没有问题。