如何比较2个数组?(How to compare 2 arrays?)

2019-10-30 18:49发布

我有两个阵列,即组合和truecombo。 用户填写通过点击不同的按钮在舞台上与影片剪辑组合,truecombo是正确的组合。

在任何给定的点(enterFrame事件)闪光灯正在检查这两个是否相同,如果是的话,做一些东西。 暂且这是我的代码(改变几次,像在组合的末端铸字的索引,加入.parent [O] 2周的事情将会发生,无论是一个或另一个。

此语句也不会满意,此时添加和组合阵列的斩波会继续,或者状态将被立即满足时combo.length = 6.检查我的代码。

更新:我有我当前的代码Dropbox的文件。 单击此为FLA 链接这里是SWF 的链接 ,便于和安全剥离下来一如既往。

/*stage.*/addEventListener(Event.ENTER_FRAME, checkthis);
function checkthis(e:Event)
{
    for(var o:int=0;o<= combo.length; o++) 
    {
        if((combo[o] == truecombo[o]) && (combo.length==truecombo.length))
        {
            equal=true;
        }
    }
    if (equal==true)
    {

        stage.removeEventListener(Event.ENTER_FRAME, checkthis);
        endSeq();
    }
}
function endSeq():void
{
    bravo.play();
    for (var i:int = 0; i < combo.length; i++)
    {
        var element:DisplayObject = combo[i];
        element.parent.removeChild(element);
    }
    firebb.gotoAndPlay(2);
    windbb.gotoAndPlay(2);
    spiritbb.gotoAndPlay(2);
    earthbb.gotoAndPlay(2);
}

这是我怎么把我的新元素组合阵列。

function add(element:DisplayObject)
{
    twist.gotoAndPlay(2);

    element.width = WIDTH;
    element.height = HEIGHT;

    if (this.combo.length >= MAX_ELEMENTS)
    {
        removeChild(this.combo.shift());
    }

    this.combo.push(element as DisplayObject);
    this.addChild(element);
    this.reorder();
}

function reorder()
{
    for (var i:int = 0; i < combo.length; i++)
    {
        var element:DisplayObject = combo[i];
        element.x = OFFSET_X + (i * SEP_X);
        element.y = OFFSET_Y;
    }
}

这是我有我的truecombo和创建内容。

var fireb:firebtn = new firebtn();
var spiritb:spiritbtn = new spiritbtn();
var earthb:earthbtn = new earthbtn();
var windb:windbtn = new windbtn();
var combo:Array=new Array();

const truecombo:Array = [fireb,windb,spiritb,windb,earthb,fireb];

对不起,缺乏意见,我想这是不言自明。 提前致谢。

Answer 1:

我相信combo[o] truecombo[o]是同一类的两个实例:你希望他们相匹配。 如果是这样的话,你可以考虑:

getQualifiedClassName(combo[o]) == getQualifiedClassName(truecombo[o])

要匹配你的方式,你必须确保躺在里面的物体truecombo被提及在舞台上和没有新的情况下的相同。


编辑:

看来当比赛是成功的,你不破环。 使用这个来代替:

function checkthis(e:Event)
{
    for(var o:int=0;o<= combo.length; o++) 

      if((combo[o] == truecombo[o]) && (combo.length==truecombo.length)) {

        equal=true;

        break;
      }     

      if (equal) {

        stage.removeEventListener(Event.ENTER_FRAME, checkthis);

        endSeq();
      }
}


Answer 2:

这里是一个非常简单的循环:

var equal:Boolean=true
if(combo.length == truecombo.length) {
    for(var i:int=0; i<combo.length; i++) {
        if(combo[i] != truecombo[i]) {
            equal=false;
            break;
        }
    }
} else {
    equal=false;
}

if(equal) {
    //do whatever
}

这是假定两者相等,等到我们找到答案。 因此,如果长度是不同的,它们是不相等的。 如果 i 元素是不同的,它们不相等。

最后,你检查你的标志equal是真实的,做任何你想做的事情。



文章来源: How to compare 2 arrays?