再回来,需要帮助修复JS游戏

时间:2015-01-16 00:30:11

标签: javascript

要清楚,这是一个独立的JavaScript游戏,可以帮助我更多地参与语法。

此时此刻我正在尝试找出一种方法来添加list函数来列出该会话中数组中添加的所有内容(我仍在学习如何实现此功能以便与Cookie一起使用和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();
}

1 个答案:

答案 0 :(得分:2)

问题是你的“数组”实际上是对象。

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

对象没有属于the Array.prototype.push方法。如果你想使用push,你需要使你的数组​​成为实际的数组。

var Persons = [];