How to compare 2 arrays?

十年热恋 提交于 2019-12-13 02:45:30

问题


I have two arrays, namely combo and truecombo. The user fills the combo with MovieClips by clicking on various buttons on the stage, truecombo is the correct combination.

At any given point (enterFrame) Flash is checking whether the two are the same, if yes, then do some stuff. For the time being this is my code (altered several times, like with Typecasting the indices, adding .parent at the end of combo[o] etc. 2 things will happen, either one or the other.

Either the statement will not be satisfied, at which point the adding and chopping of the combo array will continue, or the condition will be instantly met when combo.length = 6. Check my code.

UPDATE: I have a dropbox file with my current code. Click this for FLA link and here is the SWF link stripped down as always for ease and security.

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

This is how I push my new elements to the combo array.

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

And this is how I have my truecombo and its contents created.

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];

Sorry for the lack of comments, I'd guess it's pretty self-explanatory. Thanks in advance.


回答1:


I believe combo[o] & truecombo[o] are two instances of the same class & you want them to be matched. If that is the case you may consider :

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

To match the way you did, you must ensure the objects lying inside truecombo be referring to the same ones on stage & not new instances.


EDIT:

It seems you do not break the loop when the match is a success. Use this instead :

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



回答2:


Here's a really simple loop:

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
}

This assumes both are equal, until we find out otherwise. So if the lengths are different, they are not equal. If the ith element is different, they are not equal.

In the end, you check if your flag equal is true and do whatever you want to do.



来源:https://stackoverflow.com/questions/13755936/how-to-compare-2-arrays

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!