JavaScript로 2D 하향식 젤다 스타일 웹 rpg 싱글 플레이어를 만들고 있습니다.JavaScript - 컨테이너에서 요소를 제거하는 데 매우 혼란 스럽습니다.
플레이어 근처에서 보라색 셔츠를 걷다 보면, "rescue"할 것이다 ... ContainerOfAnimals
에서 animalContainer
을 기본적으로 제거하여 (스테이지에서 animalContainer의 BMP를 제거함) 해당 ID를 추가합니다. rescuedTotal_Array
에 animalContainer ...
이상한 것은 아래의 그림에서, 나는 animalContainer2
다음 구조 animalContainer1
를 구출 할 수 있어요 ...하지만 animalContainer2
에 animalContainer1
에서 이동하는 경우, 그것은
Uncaught TypeError: Cannot read property 'id' of undefined` error.
기본적으로
... :
근무 방법 : ID (22)를 얻을 후 최대 ID 21 - >>> 나는이 함께 통지 한 가지 요소 이름이 ID를 변경하지 않는다는 것입니다 ... 이유가 확실치 않으므로 깨진 방법으로 ... 요소 이름 animalContainer2
을 ID와 연결할 수도 없습니다. 하지만 ID와 이름이 그렇게 연관을 얻을 수있는 방법을 모르는 ..
ID in rescued array...: 22, element name: animalContainer1, rescued array length: 2, elem index pos: 0, index: 0
ID in rescued array...: 21, element name: animalContainer1, rescued array length: 2, elem index pos: 1, index: 0
깨진 방법 - 오류가 발생합니다 : 다음 ID 22
ID in rescued array...: 21, element name: animalContainer1, rescued array length: 1, elem index pos: 0, index: 0
1. Uncaught TypeError: Cannot read property 'id' of undefined
까지, ID (21)를 얻을 수를
코드 조각 :
function grabIt(NPC_id, index) {
//check if NPCid already exists in array before adding...
if ($.inArray(ContainerOfAnimals.children[index].id, rescuedTotal_Array) == -1) {
rescuedTotal_Array.push(ContainerOfAnimals.children[index].id);
}
if (rescuedTotal_Array.length == 2) {
for (var z = 0; z < rescuedTotal_Array.length; z++) {
console.log("ID in rescued array...: " + rescuedTotal_Array[z] + ", \n element name: " + ContainerOfAnimals.children[index].name + ", rescued array length: " + rescuedTotal_Array.length + ",\n elem index pos: " + z + ",\n index: " + index);
}
}
//when I remove the first added element to rescuedTotalArray... the 2nd element's index assumes first added element's index... (goes from 1 to 0)
console.log(index);
console.log("element removed: " + ContainerOfAnimals.children[index]);
stage.update();
ContainerOfAnimals.removeChild(ContainerOfAnimals.children[index]);
updateHUD();
}
,691 363,210
나는 순서가있는 내가 배열의 요소를 저장하는 이유를 전혀 모르는/... 중요한 것 무대에서
편집을 제거 : 내가 요소를 제거하여이 문제를 해결할 수 있다고 생각 요소 ID로 대신 컨테이너 객체에 의해 색인 ...에 의해 ContainerOfAnimals에서 더 getChildID()
기능 ...
그래서 난 해본 제공 :
var childToRemove = document.getElementById(ContainerOfAnimals.children[index]);
console.log(childToRemove);
ContainerOfAnimals.removeChild(childToRemove);
을 console.log을 (치
과 같이 문에 대해 시도 console.log(ContainerOfAnimals.children[index].id);
나에게 올바른 ID입니다 ID 21
을 제공합니다 ...
자, 이제 부모 컨테이너가 두 개 있다고 가정 해 봅시다 ... 'ContainerOfItemA' 및 ContainerOfItemB' ... 그리고 해당 ID를 'ContainerOfItemB'에 대해서'ContainerOfItemA'는'container1','container2' ... 같은 것을 가지고 있습니다. ...''ContainerOfItemB''에 대해서 ...''Children''을'0'과'1'로 설정할 수 있습니까? ID가 다른 상위 컨테이너에서 사용 되더라도 하위 항목입니까? – Growler
또한 '나는 개체 자체에 대한 참조를 유지하겠다'라는 예를들 수 있습니까? – Growler
요소의 ID를 직접 변경해야하는지 확신 할 수 없습니다. 도서관에서 설정 한 것으로 보이지만 (100 % 확신 할 수 없음) 라이브러리를 만지면 라이브러리가 잘 작동합니다. 참조 유지에 관해서는, 예를 들어 [this like] (https://github.com/Canop/SpaceBullet/blob/master/src/Mission.js#L107)와 같이 'var myAnimal;'을 의미합니다 (이 프로그램에서 나는 CreateJS 클래스의 서브 클래스를 만든다.) –