如何将Numpool数组中的随机数与matchNum数组匹配?

时间:2012-12-16 06:17:36

标签: actionscript-3

好的,我使用了indexOf,我得到了它与...一起工作....但是我用随机数发布了这个下面的代码并且它不起作用。

movieClip_1.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler);

    function fl_MouseClickHandler(event:MouseEvent):void
    {
        var allreadyPicked:Array = [1,2,3,4,5];
        var pickedNum:Array = [3,5];


        function compareArrays(init_arr:Array, comparing_arr:Array):Array
        {
            var differenceArray_arr:Array = new Array();

            for each (var id_obj:Object in init_arr)
            {
                if (comparing_arr.indexOf(id_obj) >= 0)
                {
                    differenceArray_arr.push(id_obj);
                }
            }
            return differenceArray_arr;
        }

        trace("Matched Number " + compareArrays(allreadyPicked, pickedNum));
    }

但是,当我添加我的随机数字代码时,似乎没有任何想法,而不是????

movieClip_1.addEventListener(MouseEvent.CLICK, clicked);

function clicked(event:MouseEvent):void
{
    //
    var allreadyPicked:Array = [1,5];


    //
    var numPool:Array = [1,2,3,4,5,6];
    var numPicked:Array = [];
    var randomCount:Number = 1;
    var r:Number;


    for (var i = 0; i < randomCount; i++)
    {
        r = Math.floor(Math.random() * numPool.length);
        numPicked[numPicked.length] = numPool.splice(r,1);
    }

    trace("Number Picked " + numPicked);
    ranNum.text = String(numPicked);


    function compareArrays(init_arr:Array, comparing_arr:Array):Array
    {
        var differenceArray_arr:Array = new Array();

        for each (var id_obj:Object in init_arr)
        {
            if (comparing_arr.indexOf(id_obj) >= 0)
            {
                differenceArray_arr.push(id_obj);
            }
        }

        return differenceArray_arr;
    }
    trace("Matched " + compareArrays(allreadyPicked, numPicked));

1 个答案:

答案 0 :(得分:0)

您希望使用Array的indexOf方法来告诉您它是否存在以及它的索引是什么。

示例:

var index:int = matchNum.indexOf(numPicked);

if(index >= 0)
{
  // match exists
  // index is the index of matching element in matchNum array

}
    else
    {
        // no match
    }