如何验证数组中的元素是否不存在于as3中的另一个数组中?

时间:2013-09-26 22:16:56

标签: arrays actionscript-3

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

如何更改此代码以便在我的条件下工作?

请告诉我你是否需要额外的细节我是flash的新手,也许我没有正确解释自己。 THX

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

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分: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...");
}