2015-01-16 2 views
-4

분명히하기 위해이 스크립트는 구문에 더 많이 참여할 수 있도록 도와주는 독립 실행 형 자바 스크립트 게임입니다.다시 돌아오고 JS 게임을 수정하는 데 도움이 필요합니다

list 함수를 추가하여이 세션의 배열에 추가 된 모든 것을 나열하는 방법을 알아 내려고 노력 중입니다. 쿠키와 HTML과 함께 사용하도록 구현하는 방법을 아직 배우고 있습니다. .

//We name the functions. 
function Person(name, personAge) { 
    this.name = name; 
    this.personAge = personAge; 
} 

function Animal(animalName, species, breed) { 
    this.animalName = animalName; 
    this.species = species; 
    this.breed = breed; 
} 

function CreateYourOwn(creativeName, species2, power, customAge) { 
    this.creativeName = creativeName; 
    this.species2 = name; 
    this.power = power; 
    this.customAge = customAge; 
} 

//We list the arrays. 
var Persons = [ 


]; 

var Animals = [ 



]; 

var Customs = [ 


]; 

function creator() { 
    //I start the prompt to ask the user which one. 
    var personPrompt = prompt("Welcome to virtual reality! Put in 'person' for person creator, 'animal' for animal creator, and 'custom' for custom creator! Or, if you want to list your creations, type in 'list'! NOTE: All creations will be deleted upon reload.").toLowerCase(); 


    //And this is where I am right now. 
    switch (personPrompt) { 
     case 'person': 
      var name = prompt("Name:").replace(/['"]/g, ''); 
      var age = prompt("Age:").replace(/['"]/g, ''); 
      var person = new Person(name, age); 
      Persons.push(person); 
      break; 
     case 'animal': 
      var animalName = prompt("What is the name of your animal?"); 
      var species = prompt("What species is it?"); 
      var breed = prompt("What breed is it?"); 
      var animal = new Animal(animalName, species, breed); 
      Animals.push(animal); 
      break; 
     case 'custom': 
      var creativeName = prompt("What do you call this thing?"); 
      var species2 = prompt("What do you describe as the species as a whole?"); 
      var power = prompt("What can this thing do?(ex. shoot lasers, change eye color, etc)"); 
      var customAge = prompt("How old is this thing?"); 
      var custom = new CreateYourOwn(creativeName, species2, power, customAge); 
      break; 
     case 'list': 
      var list_var = prompt("Which list would you like to view?").capitalize; 
      switch (list_var) { 
       case 'Persons': 
        for (var i = 0; i < Persons.length; i++) { 
         console.log(Persons[i]); 
        } 
        break; 
       case 'Animals': 
        for (var m = 0; i < Animals.length; i++) { 
         console.log(Animals[i]); 
        } 
        break; 
       case 'Customs': 
        for (var j = 0; i < Customs.length; i++) { 
         console.log(Customs[i]); 
      } 

    } 
} 
} 

while (confirm("Would you like to make another creature ? (If you haven 't already, just click OK.)") === true) { 
    creator(); 
} 

답변

2

"배열"은 실제로 개체입니다.

//We list the arrays. 
var Persons = { 
}; 

객체는 the Array.prototype에 속한 .push 방법을 가지고 있지 않습니다. 푸시를 사용하려면 배열을 실제 배열로 만들어야합니다.

var Persons = []; 
+0

그 부분을 고쳤습니다 (고맙습니다!). 그러나 여전히 작동하지 않습니다! 나는 while 루프를 실행하여 다른 사람 (이론 상으로는 계속 실행하면 여러 가지를 푸시 할 수 있음)을 만들지 만, list_var을 실행할 때 코드를 나열하지 않을지 묻습니다. –

관련 문제