0

I'm trying to get containers within containers that I've stored in a Containers array. Sounds confusing, so here's the code:

First I make individual containers to store BMP and label for People and Animals: (I will make a new container for each instance of a NPC because I want individual control over BMP and associated Label).

peopleContainer.addChild(peopleBMP, peopleLabel);
animalContainer.addChild(animalBMP, animalLabel);   
animalContainer2.addChild(animalBMP, animalLabel);

I then assign them names and IDs:

peopleContainer.name = "peopleContainer1";
peopleContainer.id = 0;

animalContainer.name = "animalContainer1";
animalContainer.id = 1;

animalContainer2.name = "animalContainer2"; 
animalContainer2.id = 2;

I then want to create Containers that store these individual containers:

ContainerOfPeople = new createjs.Container();   
ContainerOfPeople.name = "Container Of People";
ContainerOfPeople.id = 0;
ContainerOfPeople.addChild(peopleContainer);

ContainerOfAnimals = new createjs.Container();  
ContainerOfAnimals.name = "Container Of Animals";
ContainerOfAnimals.id = 1;
ContainerOfAnimals.addChild(animalContainer, animalContainer2);

I then add those big containers to an array

NPC_Array.push(ContainerOfPeople, ContainerOfAnimals);

Now, I'd like to loop through the array of big containers, and get the individual IDs of the containers within the big containers... That way I can check their individual distances to see which is closest to the player...

function checkDistance2() {
   //loop through Containers Array [ContainerOfAnimals, ContainerOfPeople]... 
   for (var index = 0; index < NPC_Array.length; index++) {
        console.log(NPC_Array[index].children[0].name);         
   }            
}

This gives the output:

peopleContainer1 
animalContainer2

It seems to be working, but it's skipping over the first animalContainer (name animalContainer1) within ContainerOfAnimals. Why is that?

Thanks!

user3871
  • 12,432
  • 33
  • 128
  • 268

1 Answers1

1

You're not iterating over the array at NPC_Array[index].children. This fixes that:

function checkDistance2() {
   for (var index = 0; index < NPC_Array.length; index++) {
       for (var j = 0; j < NPC_Array[index].children.length; j++) {
           console.log(NPC_Array[index].children[j].name); 
       }
   }            
}

http://jsfiddle.net/mattball/6HGY9

Matt Ball
  • 354,903
  • 100
  • 647
  • 710
  • Why not? animalContainer's name is `animalContainer1`, but the actual container variable's name is still animalContainer... unless the variable name reference is somehow changed when I change the `name` property? – user3871 Aug 14 '13 at 04:56
  • I figured out why the animalContainer1 wasn't being added... I originally thought I could just create a new copy of animalContainer1 into animalContainer2 just by doing `animalContainer2 = animalContainer;`... Just realized I needed to do `animalContainer2.clone(animalContainer);` to make a copy – user3871 Aug 14 '13 at 05:06
  • Assignment only copies primitives, not objects. Probably downvoted because these are very basic JavaScript questions/mistakes, and unlikely to be useful to others in the future because they're not really searchable or generically posed. http://meta.stackexchange.com/a/149138/133242. – Matt Ball Aug 14 '13 at 05:23