Question

I have an array, lets call it _persons. I am populating this array with Value Objects, lets call this object PersonVO

Each PersonVO has a name and a score property.

What I am trying to do is search the array &

//PSEUDO CODE

1 Find any VO's with same name (there should only be at most 2)
2 Do a comparison of the score propertys
3 Keep ONLY the VO with the higher score, and delete remove the other from the _persons array.

I'm having trouble with the code implementation. Any AS3 wizards able to help?

Était-ce utile?

La solution

You'd better use a Dictionary for this task, since you have a designated unique property to query. A dictionary approach is viable in case you only have one key property, in your case name, and you need to have only one object to have this property at any given time. An example:

var highscores:Dictionary;
// load it somehow
function addHighscore(name:String,score:Number):Boolean {
    // returns true if this score is bigger than what was stored, aka personal best
    var prevScore:Number=highscores[name];
    if (isNaN(prevScore) || (prevScore<score)) {
        // either no score, or less score - write a new value
        highscores[name]=score;
        return true;
    }
    // else don't write, the new score is less than what's stored
    return false;
}

The dictionary in this example uses passed strings as name property, that is the "primary key" here, thus all records should have unique name part, passed into the function. The score is the value part of stored record. You can store more than one property in the dictionary as value, you'll need to wrap then into an Object in this case.

Autres conseils

you want to loop though the array and check if there are any two people with the same name.

I have another solution that may help, if not please do say.

                    childrenOnStage = this.numChildren;
                    var aPerson:array = new array;


        for (var c:int = 0; c < childrenOnStage; c++)
        {
            if (getChildAt(c).name == "person1")
            {
              aPerson:array =(getChildAt(c);
            }
        }

        Then trace the array, 
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top