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

Back again, need help fixing JS game

本文关键字:JS 游戏 帮助 返回      更新时间:2023-09-26

需要明确的是,这是一个独立的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();
}

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

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

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

var Persons = [];