Skip to main content
Participant
October 2, 2011
Answered

Help: Problem with array

  • October 2, 2011
  • 1 reply
  • 656 views

Hi. I would like to remove objects from my array if they do not meet the condition. But I haven't been able to get the result i want. Please help, thanks a lot.

package

{

          import flash.display.MovieClip;

          public class Test extends MovieClip

          {

                    public var fixedObjects:Array;

                    public function examineLevel()

                    {

                              var fixedObjects = new Array();

                              for (var i:int = 0; i<gamelevel.numChildren; i++)

                              {

                                        var mc = gamelevel.getChildAt(i);

                                        if (mc is Floor)

                                        {

                                                  var floorObject:Object = new Object();

                                                  floorObject.mc = mc;

                                                  floorObject.leftside = mc.x;

 

                                                  fixedObjects.push(floorObject);

                                        }

                              }

                              fixedObjects.sortOn("leftside" , Array.NUMERIC);

                              for each (floorObject in fixedObjects){

                                        trace(floorObject.leftside);

                              }

                              for each (floorObject in fixedObjects){

                                        if(floorObject.leftside <= 260){

                                                  fixedObjects.splice(floorObject,1);

                                        }

                              }

                              trace();

                              for each (floorObject in fixedObjects){

                                        trace(floorObject.leftside);

                              }

                    }

          }

}

OutPut:

153.3

233.3

267.3

233.3

267.3

This topic has been closed for replies.
Correct answer Ned Murphy

Another way to walk thru an array and remove objects is to do it backwards in a loop that counts down...  That way you are sure not to skip an element as you remove them.

for(var i:uint=fixedObjects.length-1; i>-1; i--){ 

        if(fixedObjects.leftside <=300){

                 fixedObjects.splice(i,1);

        }

}

1 reply

Ned Murphy
Legend
October 2, 2011

The Array.splice method argumens when removing objects consist of the start Index and the number of objects to remove.  Your code appears to be using the object itself rather than its index in the array...

    for each (floorObject in fixedObjects){ 

                  if(floorObject.leftside <= 260){

                          fixedObjects.splice(floorObject,1);

                   }

    }

So what you might try instead is to use...

    for each (floorObject in fixedObjects){ 

                  if(floorObject.leftside <= 260){

                          fixedObjects.splice(fixedObjects.indexOf(floorObject),1);

                   }

    }

kavvvkAuthor
Participant
October 2, 2011

Thanks for your help. I changed the code accordingly but the output is still the same. And i can't seem to remove the object at index 1.

Output:

153.3

233.3

267.3

233.3

267.3

And when i changed the this code:

for each (floorObject in fixedObjects){

                                        if(floorObject.leftside <=300){

                                                  fixedObjects.splice(fixedObjects.indexOf(floorObject),1);

 

                                        }

                              }

I get this output:

153.3

233.3

267.3

233.3

Ned Murphy
Ned MurphyCorrect answer
Legend
October 2, 2011

Another way to walk thru an array and remove objects is to do it backwards in a loop that counts down...  That way you are sure not to skip an element as you remove them.

for(var i:uint=fixedObjects.length-1; i>-1; i--){ 

        if(fixedObjects.leftside <=300){

                 fixedObjects.splice(i,1);

        }

}