1

好的,我使用了 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));
4

1 回答 1

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
    }
于 2012-12-16T16:10:25.880 回答