【问题标题】:Input text questionnaire in as3在 as3 中输入文本问卷
【发布时间】:2015-07-12 05:24:17
【问题描述】:

我正在尝试创建一系列由输入文本回答的问题。用户可以选择跳过问题,当他们回答正确时,他们会前进到下一帧和新问题。代码很简单,但是无论出于何种原因,当我删除验证输入的侦听器并调用新的侦听器时,什么都没有发生,也没有引发错误。我不在我的电脑旁,但如果这没有意义,我会发布代码。

我确信这一切都可以在单帧上完成,但超出了我的技能,因此进入下一帧。

已编辑:我已经确定了我们的数组片段,现在将它放在一帧上,但是 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{
question_tf.text=questionArray[currentQuestion];
}

checkAnswer_btn.addEventListener(MouseEvent.CLICK,checkAnswer);

function checkAnswer(e:MouseEvent):void{
if(answer_tf.text==answerArray[currentQuestion]){
    displayQuestion++;
}
}

【问题讨论】:

    标签: actionscript-3 flash text input


    【解决方案1】:

    听起来您正试图在正确回答问题时显示下一个问题。但是,您正在做的是尝试增加一个名为 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_tfanswer_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的函数调用问题,增加要显示的问题的变量,如果答案正确,则清除答案字段。

    然而,有一些非常重要的事情需要纠正,而您的原始代码没有考虑到这些问题:

    1. 您的checkAnswer 函数还应确保当索引currentQuestion 的值增加到超过可用问题的数量时,它永远不会被使用;否则,当您尝试访问 answerArray 的元素时,您将得到一个数组越界异常

    2. 在检查答案是否区分大小写时,您的代码也应该小心。如果有人输入“Uncle billy”,你的代码目前不会接受答案;但是,您可能希望接受该答案。更正此问题的安全方法是执行以下操作:

      if(answer_tf.text.toLowerCase() == answerArray[currentQuestion].toLowerCase() ) {
          currentQuestion++;
          displayQuestion();
      }
      

    我为您创建的代码在 Flash CC 2014 中运行没有问题。

    【讨论】:

    • 太完美了!感谢您的帮助,我将开始研究确定数组的长度,以便进入最终的场景序列。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-27
    • 1970-01-01
    • 2018-06-03
    • 2014-04-07
    • 1970-01-01
    相关资源
    最近更新 更多