简体   繁体   English

独立JS游戏帮助:如何将用户输入转换为对象?

[英]Standalone JS Game Help: How do I turn user-input into an object?

I'd like to mention that I am not working on pairing JS with HTML and CSS just yet. 我想提一下,我目前还没有将JS与HTML和CSS配对。 I'm just making a standalone JS console game to get a little more familar with the syntax. 我只是在制作一个独立的JS控制台游戏,以使其对语法更加熟悉。 My idea is to have the person insert certain parameters through "prompt" and be able to list them. 我的想法是让人们通过“提示”插入某些参数并能够列出它们。 So far, I've made it to the "handling the prompts" point. 到目前为止,我已经达到了“处理提示”的地步。 I need help figuring out how to have the user give out a "name" field and a "age" field and get it pushed into an array, which I will make a list function to search and list all the persons they have created in that session(I have no idea how to use cookies for something like this.) 我需要帮助弄清楚如何让用户给出一个“名称”字段和一个“年龄”字段并将其推入一个数组,我将使用一个列表函数来搜索和列出他们在其中创建的所有人员会话(我不知道如何将Cookie用于此类操作。)

So far, I've tried directly inputing name, age into the prompt box when asked for a name and age, but it saves name and age to a 'name' item in the array instead of being an object. 到目前为止,我已经尝试在要求输入名称和年龄时直接在提示框中输入name, age和年龄,但是它将名称和年龄保存为数组中的“名称”项,而不是对象。

TL;DR - I need to turn user input into an object and push said object into an array. TL; DR-我需要将用户输入变成一个对象,并将所述对象推入数组。

//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, species, power, customAge) {
    this.creativeName = creativeName
    this.species = name
    this.power = power
    this.customAge = customAge
}

//We list the arrays.
var Persons = [


    ]

var Animals = [



    ]

var Customs = [


    ]

//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!").toLowerCase()


//And this is where I am right now.
switch(personPrompt) {
    case 'person':
        var personCreator = prompt("Put in (name, age) in exactly that form").replace(/['"]/g,'');
        this.name = name
        this.age = age
        personCreator = new Person(name, age)
        break;
}

Example: http://jsfiddle.net/oL9dwbp6/ 示例: http//jsfiddle.net/oL9dwbp6/

case 'person':
    var personPromtResult = prompt("Put in (name, age) in exactly that form").replace(/['"]/g,'');
    var personPromtResultArray = personPromtResult.split(',');
    if(personPromtResultArray.length!=2){
        alert('Incorrect input!');
        break;
    }
    var name = personPromtResultArray[0];
    var age = parseInt(personPromtResultArray[1]);
    if(isNaN(age)){
        alert('Incorrect age!');
        break;
    }
    var person = new Person(name, age)
    console.log(person);
    break;

You have a number issues in your switch, I rewritten that with some comments: 您的交换机中存在一些号码问题,我用一些评论将其重写:

switch(personPrompt) {
    case 'person':
        //collecting name
        var name = prompt("Name:").replace(/['"]/g,'');
        //collecting age
        var age = prompt("Age:").replace(/['"]/g,'');
        //creating an instance of person
        var person = new Person(name, age);
        //adding person to Persons array
        Persons.push(person);
        break;
}

//and then you can access person that you just created like Persons[0]

JsFiddle 的jsfiddle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM