0

我试图在我存储在 Containers 数组中的容器中获取容器。听起来很混乱,所以这里是代码:

首先,我制作单独的容器来存储人和动物的 BMP 和标签:(我将为每个 NPC 实例制作一个新容器,因为我希望单独控制 BMP 和相关标签)。

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

然后我为他们分配名称和 ID:

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

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

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

然后我想创建存储这些单独容器的容器:

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

然后我将那些大容器添加到数组中

NPC_Array.push(ContainerOfPeople, ContainerOfAnimals);

现在,我想遍历大容器数组,并获取大容器中容器的各个 ID……这样我就可以检查它们各自的距离,看看哪个离玩家最近……

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

这给出了输出:

peopleContainer1 
animalContainer2

它似乎正在工作,但它跳过了animalContainer1ContainerOfAnimals 中的第一个 animalContainer (name)。这是为什么?

谢谢!

4

1 回答 1

1

你不是在遍历数组NPC_Array[index].children。这解决了:

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

于 2013-08-14T04:52:13.887 回答