简体   繁体   中英

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. I'm just making a standalone JS console game to get a little more familar with the syntax. 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.)

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.

TL;DR - I need to turn user input into an object and push said object into an array.

//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/

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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