Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
(javascript)插入用户';从输入到对象变量的s名称_Javascript_User Input - Fatal编程技术网

(javascript)插入用户';从输入到对象变量的s名称

(javascript)插入用户';从输入到对象变量的s名称,javascript,user-input,Javascript,User Input,我正在做一个文字冒险游戏。我设法让所有其他的东西都能工作,不过,如果可能的话,我想把代码简化一点。无论如何,我需要我的游戏接受用户的名字并将其输入到故事中。我只知道如何通过使用提示来获取故事中的名字,但使用该选项无法与游戏很好地融合 这是一个纯粹的html和javascript游戏 任何建议都会有帮助 <!DOCTYPE html> <html> <head> <meta charset="utf-8" />

我正在做一个文字冒险游戏。我设法让所有其他的东西都能工作,不过,如果可能的话,我想把代码简化一点。无论如何,我需要我的游戏接受用户的名字并将其输入到故事中。我只知道如何通过使用提示来获取故事中的名字,但使用该选项无法与游戏很好地融合

这是一个纯粹的html和javascript游戏

任何建议都会有帮助

 <!DOCTYPE html>
    <html>

<head>
            <meta charset="utf-8" />
            <title>Nuclear Christmas</title>

            <link href="image/favicon.ico" rel="shortcut icon" type="image/x-icon" />
            <link rel="stylesheet" type="text/css" href="ncTheme.css" /> 
            <link href='http://fonts.googleapis.com/css?family=Covered+By+Your+Grace' rel='stylesheet' type='text/css'>
        </head>
         <body>
            <h1 class="headFont">How Far Would You Go for That Christmas Spirit?</h1>
            <table>
                <tr><!-- text field -->
                    <td id="textCell"> 
                        <p id ="dialogue"><!--the game's dialogue will appear in the paragraph tag--></p>
                    </td>
                    <!-- text field -->

                    <!--this section holds img id: "image", which will be used to display the game's images-->
                    <td id="imageCell" width="400" rowspan="2">
                    <img id="image" src="image/start/png" alt="Image failed to load.">
                    </td>
                    <!--this section holds img id: "image", which will be used to display the game's images-->
                </tr>
                <tr>
                    <!--this section houses the input box and the submit button.-->
                  <td id="inputCell"> 
                    <!--background music-->
                    <audio id="music" autoplay loop>
                        <source src="audio/Silent night-warped.mp3" type="audio/mpeg">
                            Your browser does not support the audio element.
                    </audio>
                      <button onclick="document.getElementById('music').muted=!document.getElementById('music').muted"><img id="mute" src ="image/Mobile-Speaker-icon.png"><!--allows music to stop and play when pressed--></button>
                    <!--background music-->
                    <input id="inputText" type="text"> <!-- text box -->
                    <input id="submitInput" type="submit" value="Submit" onClick="advance()"> <!-- the submit button -->
                    <!--this section houses the input box and the submit button.-->
                  </td>
                </tr>
            </table>

    </body>

    <script type="application/javascript">
    //the options object variable holds the text information.
        var userName;

        var options = new Object();
            options.scene0 = "In the early hours of January 3rd, the United States was plunged in to war against an enemy state. Invading forces made landfall on the U.S east coast and pushed towards the capital. Both sides fought a long fight, each suffering causalities in the thousands, yet neither side were willing to wave the white flag. In the coming months, both countries threatened each other with the possibility of a nuclear strike but neither were willing to pull the trigger. Unfortunately, those threats came to fruition on August 13th when the opposing side launched the first of many strikes against the U.S. The eastern seaboard was lit up like a Christmas tree. And it was unclear if a portion of the population survived. [START]"; //main screen
            //////////////////////////////////////////////////////////
            options.inputName = "[CONTINUE]" //adding name to story
            //////////////////////////////////////////////////////////
            options.scene1 = "Fast forward three years to find that the U.S is being occupied by the enemy. Some people are receiving aid from their new leader while others are joining the resistance. INSERT-NAME, on the other hand, is not remotely interested in fighting. What you care about is the date on the calendar. See, it's Christmas Eve, and you do not have everything set up. You know that your guests will be here by morning and that gives you just enough time to finish decorating. [CONTINUE]"; //introduction of story and objectives
            //////////////////////////////////////////////////////////
            options.branch1 = "Thinking about the things you need. You ponder if getting snow will set off the decorations or maybe a gift will be enough to distract the guests. What should you do first? Find [snow] or get a [TV]"; //choices for users to choose from
            //////////////////////////////////////////////////////////
            options.scene2 = "Though it’s below freezing, you know that the environment is way too dry to produce snow, so you improvise. Searching the surrounding area, you snatch up the most amount of paper you can carry and take it back to camp. You rip each piece and scatter it around your campsite.  [CONTINUE]"; //result of the snow option
            //////////////////////////////////////////////////////////
            options.sceneII = "You head into town and search the deserted buildings. It takes you around 2 hours to locate the television. It takes you some time, but you manage to secure the TV on your person. From that put on, you struggle to get it back to camp. [CONTINUE]"; //result of the TV option
            //////////////////////////////////////////////////////////
            options.scene3 = "Stopping to inspect your work, you realize that more could be done; however, you aren’t quite sure if you’re willing to go through the extra trouble. [CONTINUE]"; //user examines situation in this scene
            //////////////////////////////////////////////////////////
            options.branch2 = "I could use the rest of today to scavenge or sleep on today’s accomplishments. Gosh, I don’t know. Should I do more? [YES] or [NO]"; // second set of choices for user to choose from
            //////////////////////////////////////////////////////////
            options.scene4 = "You head deep into town to find Walmart and get something extra for the home. When you get there, you search the aisles and find several things that could come in use but you don’t see that creepy Santa statue that your grandma use to put out on Christmas. Though, you manage to locate some tree ornaments. Don’t know how you’ll use them but you’ll improvise something. [CONTINUE]"; //results of choosing yes
            //////////////////////////////////////////////////////////
            options.sceneIV = "You decide that the extra stuff isn’t needed, so you retire to your bed, promising yourself that you’ll properly clean tomorrow. The next day, you wake up and read the time on your watch, “DAMN!” You realize that your guests are due to arrive at any moment. This causes you to panic and try to tidy up the place as best you can. Unfortunately, you break a couple of items in the process.  [CONTINUE]"; // results of choosing no
            //////////////////////////////////////////////////////////
            options.scene5 = "You get back home before your self-enforced curfew and set out to arrange the final decorations. It only takes you a couple of minutes to strategically place the Christmas ornaments around your room *satisfied sigh* “, time to let the Christmas spirit take over.” You say.  [CONTINUE]"; //proceeding scene for the yes choice
            //////////////////////////////////////////////////////////
            options.end1 = "You prop up the guests and place Christmas hats on their heads and scarves around their necks.  Sabrina looks upset, Todd looks bored, and Ted is looking the other way. 'Hey, guys. Uh, merry Christmas!' You say in your most cheerful voice. The only reaction you get is the slumping of Ted’s head. DAMN IT, no one appreciates the hard work you put into decorating. THE END"; // user suffers a bad Christmas/BAD ENDING
            //////////////////////////////////////////////////////////
            options.end2 = "The next day, you prop up your guests and dress them in their Christmas attire. Sabrina is relaxing on the couch, Todd is drinking your homemade eggnog, and Ted is trying to sneak a peek at the gifts 'So nice to see you all. Merry Christmas!' You say in a cheery tone. Surprisingly, everyone seems to be enjoying themselves. You think to yourself, 'totally doing this next year.' THE END."; // user experiences a good Christmas/GOOD ENDING

    //the options object variable holds the text information.

    //the visuals object variable holds the images.
        var visuals = new Object();
            visuals.scene0 = "image/start.png";
            visuals.inputName = "image/diary_scene.png";
            visuals.scene1 = "image/cityscape.png";
            visuals.branch1 = "image/list1.jpg";
            visuals.scene2 = "image/snow_scene.png"
            visuals.sceneII = "image/tv_scene.png";
            visuals.scene3 = "image/camp_scene.png";
            visuals.branch2 = "image/more.png";
            visuals.scene4 = "image/ornament_scene.png";
            visuals.sceneIV = "image/alright.png";
            visuals.scene5 = "image/good_scene.png";
            visuals.end1 = "image/end1.png";
            visuals.end2 = "image/end2.png";
    //the visuals object variable holds the images.
    }
    //var state holds the each game state.
        var state = "SC0";
        advance();
    /*--------------------------------------------------------------------*/

        function advance(){
            //Game progression is controlled here.
                 if(state == "SC0") checkDialogue(options.scene0,visuals.scene0,"start",null,"INP",null);
            else if(state == "INP") checkDialogue(options.inputName,visuals.inputName,"continue",null,"SC1",null);
            else if(state == "SC1") checkDialogue(options.scene1,visuals.scene1,"continue",null,"B1",null);
            else if(state == "B1") checkDialogue(options.branch1,visuals.branch1,"snow","tv","SC2","SCII");
            else if(state == "SC2") checkDialogue(options.scene2,visuals.scene2,"continue",null,"SC3",null);
            else if(state == "SCII") checkDialogue(options.sceneII,visuals.sceneII,"continue",null,"SC3",null);
            else if(state == "SC3") checkDialogue(options.scene3,visuals.scene3,"continue",null,"B2",null);
            else if(state == "B2") checkDialogue(options.branch2,visuals.branch2,"yes","no","SC4","SCIV");
            else if(state == "SC4") checkDialogue(options.scene4,visuals.scene4,"continue",null,"SC5",null);
            else if(state == "SCIV") checkDialogue(options.sceneIV,visuals.sceneIV,"continue",null,"E1",null);
            else if(state == "SC5") checkDialogue(options.scene5,visuals.scene5,"continue",null,"E2",null);
            else if(state == "E1") checkDialogue(options.end1,visuals.end1,null,null,null,null);
            else if(state == "E2") checkDialogue(options.end1,visuals.end1,null,null,null,null);
        }
        //

        function checkDialogue(output,images,answer1,answer2,state1,state2,userName){
            outT("dialogue",output);
            document.getElementById("image").src = images;

                 if(analyzeInput(answer1)) progress(state1);
            else if(analyzeInput(answer2)) progress(state2);
            else if(analyzeInput("restart")) progress("SC0"); //Resets the game to the beginning
            else document.getElementById("inputText").value = "";
        }
    //
        function progress(statex){
            state = statex;
            document.getElementById("inputText").value = "";
            advance();
        }
    //used professor's code for letter conversion on 9/26/2014
        function analyzeInput(answer){
            if(answer != null){
                var a = document.getElementById("inputText").value.toUpperCase();
                var b = answer.toUpperCase();
                var c = a.indexOf(b) >= 0;
                /*debugger*/console.log("a = "+a+" b = "+b+" c = "+c);/*debugger*/
                return c == true;
            }else return false;
        }
          function outT(myId,myTEXT){
            document.getElementById(myId).innerHTML = myTEXT;
        }
    </script>

</html>

核圣诞节
你会为圣诞精神付出多少?

您的浏览器不支持音频元素。 //options对象变量保存文本信息。 var用户名; var options=新对象(); options.scene0=1月3日凌晨,美国陷入了对敌国的战争。入侵部队在美国东海岸登陆并向首都推进。双方进行了长期的战斗,每一方都遭受了数千人的伤亡,但双方都不愿意挥舞白旗。在接下来的几个月里,两国以核打击的可能性相互威胁,但都不愿意扣动扳机。不幸的是,8月13日,当对方对美国发动了多次攻击中的第一次攻击时,这些威胁就变成了现实。东海岸就像一棵圣诞树一样被点亮了。目前还不清楚是否有一部分人口存活下来。[开始]“;//主屏幕 ////////////////////////////////////////////////////////// options.inputName=“[CONTINUE]”//将名称添加到故事中 ////////////////////////////////////////////////////////// options.scene1=“快进三年,发现美国正被敌人占领。一些人正在接受新领导人的援助,而另一些人则加入了抵抗运动。另一方面,INSERT-NAME对战斗毫无兴趣。你关心的是日历上的日期。看,今天是平安夜,你还没有把一切都安排好。你知道你的客人早上会来,这给了你足够的时间完成装修。[继续]“;//介绍故事和目标 ////////////////////////////////////////////////////////// options.branch1=“思考您需要的东西。你在想,下雪会不会引起装饰,或者一件礼物就足以分散客人的注意力。你应该先做什么?查找[snow]或获取[TV]“;//供用户选择的选项 ////////////////////////////////////////////////////////// options.scene2=“虽然气温低于零度,但你知道环境太干燥,不会下雪,所以你可以即兴发挥。搜索周围地区,你抓起你能携带的最多的纸张,把它带回营地。你撕开每一块,把它撒在你的营地周围。[继续]“;//雪选项的结果 ////////////////////////////////////////////////////////// options.sceneII=“你进城搜索废弃的建筑。你大约需要2个小时才能找到电视。这需要花费你一些时间,但是你设法把电视安在你的身上。从那一刻起,你就很难把它带回营地。[继续]“;//电视选项的结果 ////////////////////////////////////////////////////////// options.scene3=“停下来检查您的工作,您意识到可以做更多的工作;然而,你不太确定你是否愿意经历额外的麻烦。[继续]“;//用户检查此场景中的情况 ////////////////////////////////////////////////////////// options.branch2=“我可以用今天剩下的时间来清理或是沉睡在今天的成就上。天哪,我不知道。我应该做更多吗?[是]或[否]”;//第二组供用户选择的选项 ////////////////////////////////////////////////////////// options.scene4=“你深入城镇寻找沃尔玛,为家里买些额外的东西。当你到达那里时,你搜索过道,找到一些可以使用的东西,但你没有看到你奶奶在圣诞节用来摆放的令人毛骨悚然的圣诞老人雕像。不过,你还是找到了一些树上的装饰品。我不知道你会如何使用它们,但你会即兴创作一些东西。[继续]“;//选择“是”的结果 ////////////////////////////////////////////////////////// options.sceneIV=“你决定不需要额外的东西,于是就躺在床上,向自己保证明天会好好打扫。第二天,你醒来看手表上的时间,“该死!”你意识到你的客人随时都会来。这会让你惊慌失措,并尽你所能整理好这个地方。不幸的是,您在这个过程中破坏了几个项目。[继续]“;//选择否的结果 ////////////////////////////////////////////////////////// options.scene5=“你在自行实施宵禁前回家,开始安排最后的装饰。它只会