증분 된 numberOfFaces과 동일한 기능을 호출하려고하지만 이전 값인 5로만로드됩니다. 내가 도대체 뭘 잘못하고있는 겁니까? 내가하려는 것은 왼쪽의 마지막 아이를 클릭 한 후 함수를 반복하는 것입니다. 여기일치하는 게임에서 증분이 작동하지 않음
var numberOfFaces = 5;
function generateFaces() {
var theRightSide = document.getElementById("rightSide");
var theLeftSide = document.getElementById("leftSide");
var theBody = document.getElementsByTagName("body")[0];
while(numberOfFaces > 0) {
// CREATING IMG ELEMENTS AND APPENDING THEM TO THE LEFTSIDE DIV
var img = document.createElement("img");
img.src = "http://home.cse.ust.hk/~rossiter/mooc/matching_game/smile.png" ;
img.style.top = Math.floor(Math.random() * 400) +"px";
img.style.left = Math.floor(Math.random() * 400) +"px";
theLeftSide.appendChild(img);
// CLONING THE LEFT SIDE DIV ELEMENTS, REMOVING THE LAST CHILD AND APPENDING THEM TO THE RIGHT CHILD
var leftSideImages = theLeftSide.cloneNode(true);
leftSideImages.removeChild(leftSideImages.lastChild);
theRightSide.appendChild(leftSideImages);
numberOfFaces--;
}
theLeftSide.lastChild.onclick = function nextLevel(event){
event.stopPropagation();
while (theLeftSide.firstChild) {
theLeftSide.removeChild(theLeftSide.firstChild);
}
while (theRightSide.firstChild) {
theRightSide.removeChild(theRightSide.firstChild);
}
numberOfFaces += 5;
generateFaces();
};
theBody.onclick = function gameOver(event) {
alert("Game Over!");
theBody.onclick = null;
theLeftSide.lastChild.onclick = null;
}
}
여기서 numberOfFaces를 확인/사용하고 있습니까? –
나머지 코드를 게시하십시오 .... –
표시된 코드를 기반으로 알 수있는 방법이 없습니다. 가능한 원인은 변수를 초기화하는 코드를 다시 실행하거나 변수가 로컬이거나 값을 인쇄하는 코드가 잘못되었거나 다른 이유입니다. – JJJ