0

所以我有很多显示的图片。对于每张图片,您需要输入图片中显示的正确单词(答案)。输入单词后,它会被推送到数组 aUserAnswers 中。所有正确答案都在 aCorrectAnswers 数组中。当输入错误答案时,会显示错误图像,用户可以输入另一个答案。当输入正确答案时,游戏进入下一个级别并更改图像。这是我设法做的,但它不能正常工作。我需要它来验证 ig 输入的字符串是否在指定的数组中被按下。此外,如果在第一张图像中是一只狗并且数组包含 (dog, cat, cow) 能够通过输入 cow 来进一步移动,我也不想让它成为可能,因为它存在于数组中。

如何更改此代码以适应我的情况?

如果您需要更多详细信息,请告诉我,我是 Flash 新手,也许我没有正确解释自己。谢谢

var aCorrectAnswers:Array = new Array("chicken", "ladybug", "cow", "dog");
var aUserAnswers:Array = new Array();
 wrong.visible = false;

submit_btn.addEventListener(MouseEvent.CLICK, quiz);

var poza:int = 2;

function quiz(e:MouseEvent):void {
     aUserAnswers.splice(0);
     aUserAnswers.push(answers_txt.text);
     trace (aUserAnswers);

 // var len:int = aCorrectAnswers.length;
for(var i:int = 0; i < 4; i++) {
    trace("sunt aici");
    var j:Number=0;
    if (aCorrectAnswers[j].toString() == aUserAnswers.toString())
    {

        trace("aici");
       j++;
       pictures.gotoAndStop(j+1);
    }
    else
    {
         wrong.visible = true;
        wrong.gotoAndPlay(2);
    }
4

2 回答 2

0

我认为在这种情况下,您用于测验类型的数据结构对您不利。我会改为设置一个对象数组。这样您就可以跟踪当前的问题,而不必担心他们在“狗”时回答“牛”。在下面查看:

import flash.display.Loader;
import flash.net.URLRequest;
import flash.events.MouseEvent;
import flash.utils.*;

var currentQuestion:Number = 0;
var quiz:Array = new Array(
    {
        "question": "What is this?",
        "answer": "chicken",
        "image": "chicken.png"
    },
    {
        "question": "What is this?",
        "answer": "dog",
        "image": "dog.png"
    },
    {
        "question": "What is this?",
        "answer": "cat",
        "image": "cat.png"
    },
    {
        "question": "What is this?",
        "answer": "cow",
        "image": "cow.png"
    }
);

function submitAnswer(e:MouseEvent):void
{
    //-- if something is in the text field check the answer
    if (answerText.text.length > 0)
    {
        var userAnswer = answerText.text;
        if (userAnswer == quiz[currentQuestion].answer) //-- if user answer is the same as the "answer" prop in the quiz obj array
        {
            submitBtn.enabled = false;
            wrongText.text = "Correct!";
            //-- set a little delay before the next question
            setTimeout(function()
            {
                submitBtn.enabled = true;
                wrongText.text = "";
                answerText.text = "";
                currentQuestion++;
                loadQuestion(currentQuestion);
            },1500);

        }
        else
        {
            wrongText.text = "Please try again.";
        }
    }
}
submitBtn.addEventListener(MouseEvent.CLICK, submitAnswer);

function loadQuestion(id):void
{
    questionText.text = quiz[id].question;
    //-- if an image is in imageholder, remove it
    if (imageHolder.numChildren) imageHolder.removeChildAt(0);
    var ldr:Loader = new Loader();
    ldr.load(new URLRequest(quiz[id].image));
    imageHolder.addChild(ldr);
}
//-- loadQuestion accepts an id which will be used for referencing the quiz array.
//-- loadQuestion(0) will load the first question, chicken, loadQuestion(1) will load the second question, dog, etc.
loadQuestion(currentQuestion);

查看演示:http ://ronnieswietek.com/_random/quizdemo/quiz.swf

编辑1:我没有编码这个来检查最后一个问题或任何东西。这仅用于概念目的。

编辑 2 显示如何将文本输入转换为大写:

function textChange(e:Event):void
{
    answerText.text = answerText.text.toUpperCase();
}
answerText.addEventListener(Event.CHANGE, textChange);
于 2013-09-26T23:16:28.407 回答
0

要检查一个项目是否在数组中,您可以简单地使用array.indexOf(item). 如果项目在数组中或-1不在数组中,则返回项目的位置(索引)。

if(myArray.indexOf("something") >= 0){
    trace("yes, it's in the array!");
} else {
    trace("no, it's not in the array...");
}
于 2013-09-27T22:20:47.420 回答