2015-11-17 134 views
2

對在線RPG進行編碼,並且爲創建了故事路徑case statements。但是,當我運行代碼時,出現以下錯誤:第26行的"Uncaught SyntaxError: Unexpected token ILLEGAL",即case case語句後面的行:case "HotelStay":。我看不出代碼有什麼問題,但是當我將它和所有case語句放在下面(default:除外)代碼工作正常。有任何想法嗎?JavaScript switch語句中的語法錯誤

function onStart() { 
    document.getElementById("storybox").innerHTML = "<p><em>You are Drogon Barre, aka Dragon-Bear.</em></p><p>The date is October 3rd. You are sitting quietly at your father's house when a letter arrives through the door with details of a rape and murder. The letter says that the murder was committed by a member of the infamous cult <strong>The Order of the Mouse</strong>. The writer claims the victim was her sister and that she looked on powerless as the assailant took her sister's life. She claims the perpetrator currently resides in a hotel just outside Plymouth. The letter gives a time and an address but no further details.</p><p>Do you choose to investigate?</p><button type='button' class='btn btn-success' id='yesbtn' onclick='buttonClicked(\"YesStart\")'>Yes</button><button type='button' class='btn btn-danger' id='nobtn' onclick='buttonClicked(\"NoStart\")'>No</button>"; 
}; 

function buttonClicked(buttonid) { 
    switch(buttonid) { 
    case "YesStart": 
     document.getElementById("storybox").innerHTML = "You take the train down to Plymouth and meet at the location specificied. Initially the woman fails to notice you and stares into the distance with the dazed look of someone who has considered the exact point at which the universe might have ended, present within a kind of altered dimensionality that places her materially at a similar point of existence to you, while leaving her utterly absent from it in some other, more absolute sense. Two nuns scurry past her, heading towards a small, fluffy dog, who they pet while making cooing sounds; a stern man in an ill-fitting police officer's uniform eyes them suspiciously. This spectacle seems to rouse her from her trance, and she looks quizzically at them, tilting her head to one side, before spotting you out of the corner her eye and waving you over.</p><button type='button' class='btn btn-success' id='continuebtn' onclick='buttonClicked(\"ContinueStart\")'>Continue</button>"; 
     break; 
    case "NoStart": 
     document.getElementById("storybox").innerHTML = "<p>You try to fall asleep in your bed but you cannot. Your heart is beating so fast you feel like it will fail. All night you are plagued by images of your still conscious body being cut open at autopsy. You wake up often. During the days you are afraid to be left alone. The only solace you have is a lady who calls herself Deer-Wolf, who texts you often and calls you on occasion. You find her voice comforting but you are still afraid, and feel that you will never be fully safe until you solve the case. You ask your friends for help, even offer to pay them to investigate but they either ignore you, decline, or tell you to go away. You are not sure if they believe you and are too scared, or if they think you are crazy. Either way, they won't help.</p><p>Are you sure you don't want to investigate?</p><button type='button' class='btn btn-success' id='yesbtn' onclick='buttonClicked(\"NoStart\")'>No</button><button type='button' class='btn btn-danger' id='nobtn' onclick='buttonClicked(\"YesStart\")'>Yes</button>"; 
     break; 
    case "ContinueStart": 
     document.getElementById("storybox").innerHTML = "<p>She takes you to a hotel. You chat a little on the way and she says her name is Deer-Wolf, which is the name of the lady you have been texting recently. You ask her about it and she tells you that yes, it was her who was sending the text messages. She is acting very unusually, speaking only when directly addressed and seemingly either angry with you or mired in a depression that makes your presence irrelevent. You suspect something of the two. When you arrive the hotel is empty but for two bleary eyed reception staff who stare as you walk past. Deer-Wolf tells you this is the place where the murderer lives. He rents a different room each week, always under different assumed names. He tells people this is because he is married, and likes to take women back unnoticed. The staff have never seen one of these women leave, but the room is always impeccably kept, and the uneasy feeling the hotel staff have about him has never yet been officially corroborated.</p><button type='button' class='btn btn-success' id='investigatebtn' onclick='buttonClicked(\"HotelStart\")'>Investigate</button><button type='button' class='btn btn-warning' id='helpbtn' onclick='buttonClicked(\"HotelHelp\")'>Get Help</button><button type='button' class='btn btn-danger' id='nobtn' onclick='buttonClicked(\"NoStart\")'>Go Home</button>"; 
     break; 
    case "HotelStart": 
     document.getElementById("storybox").innerHTML = "<p>You investigate the hotel, but Deer-Wolf won't tell you the room number and you think you see the faint glimmer of a smile when you are about it. She tells you that you should talk to some of her friends who run a nudist group on the internet. They live in Swindon and she has invited you up so see them. You suspect she is playing a game with you. You could stay in the hotel on more night, or go investigate the group in Swindon Deer-Wolf suggests. Where will you go?.</p><button type='button' class='btn btn-success' id='mapbtn' onclick='buttonClicked(\"Map\")'>Go to Map</button><button type='button' class='btn btn-warning' id='helpbtn' onclick='buttonClicked(\"HotelStay\")'>Stay in Hotel</button>"; 
     break; 
    case "HotelHelp": 
     document.getElementById("storybox").innerHTML = "<p>The only help you have at the moment is Deer-Wolf.</p><button type='button' class='btn btn-success' id='investigatebtn' onclick='buttonClicked(\"HotelStart\")'>Investigate</button><button type='button' class='btn btn-warning' id='helpbtn' onclick='buttonClicked(\"HotelHelp\")'>Get Help</button><button type='button' class='btn btn-danger' id='nobtn' onclick='buttonClicked(\"NoStart\")'>Go Home</button>"; 
     break; 
    case "Map": 
     document.getElementById("storybox").innerHTML = "<p>Map Loads Here</p><button type='button' class='btn btn-success' id='investigatebtn' onclick='buttonClicked(\"HotelStart\")'>Investigate</button><button type='button' class='btn btn-warning' id='helpbtn' onclick='buttonClicked(\"HotelHelp\")'>Get Help</button><button type='button' class='btn btn-danger' id='nobtn' onclick='buttonClicked(\"NoStart\")'>Go Home</button>"; 
     break; 
    case "HotelStay": 
     document.getElementById("storybox").innerHTML = "<p>You stay overnight in the hotel. Deer-Wolf knocks at your door in the night. You go to open it and find her naked. She invites herself into your room.</p><button type='button' class='btn btn-success' id='invitebtn' onclick='buttonClicked(\"Invite\")'>Invite her in</button><button type='button' class='btn btn-warning' id='kissdeerbtn' onclick='buttonClicked(\"KissDeer\")'>Try to kiss her</button><button type='button' class='btn btn-danger' id='nobtn' onclick='buttonClicked(\"Ask\")'>Ask how the Swindon group can help with the murder</button>"; 
     break; 
    case "KissDeer": 
     document.getElementById("storybox").innerHTML = "<p>You try to kiss her but she pushes you away.</p><p>'Did you think because I'm naked I'd be fair game? No thanks. I'll just have a sit down and a gin and tonic if you're offering.'</p><p>Deer-Wolf sits down naked on the sofa of the hotel room she has rented for you.</p><p>'It's a nice place. Double room. I'll be sleeping here too of course. It's got good security and I like the managers here. We get on well.'</p><p>Deer-Wolf gets up and begins to walk the space between you in a semi-circle, as if stalking her prey.<p>'I always sleep naked, but that doesn't mean you 
    can do what you want. Hands to yourself or we forget the whole thing.</p><button type='button' class='btn btn-success' id='invitebtn' onclick='buttonClicked(\"HotelStart\")'>Invite her in</button><button type='button' class='btn btn-warning' id='helpbtn' onclick='buttonClicked(\"HotelHelp\")'>Try to kiss her</button><button type='button' class='btn btn-danger' id='nobtn' onclick='buttonClicked(\"Ask\")'>Ask how the Swindon group can help with the murder</button>"; 
     break; 
    case "Invite": 
     document.getElementById("storybox").innerHTML = "<p>Deer-Wolf thanks you, and walks past your into the hotel room, gazing fixedly into your eyes as she does so.<p>'It's a nice place. Double room. I'll be sleeping here too of course. It's got good security and I like the managers here. We get on well.'</p><p>Deer-Wolf gets up and begins to walk the space between you in a semi-circle, as if stalking her prey.<p>'I always sleep naked, I hope that's not a problem.</p><button type='button' class='btn btn-success' id='invitebtn' onclick='buttonClicked(\"HotelStart\")'>Invite her in</button><button type='button' class='btn btn-warning' id='helpbtn' onclick='buttonClicked(\"HotelHelp\")'>Try to kiss her</button><button type='button' class='btn btn-danger' id='nobtn' onclick='buttonClicked(\"Ask\")'>Ask how the Swindon group can help with the murder</button>"; 
     break; 
    case "Ask": 
     document.getElementById("storybox").innerHTML = "<p>Deer-Wolf thanks you, and walks past your into the hotel room, gazing fixedly into your eyes as she does so.<p>'It's a nice place. Double room. I'll be sleeping here too of course. It's got good security and I like the managers here. We get on well.'</p><p>Deer-Wolf gets up and begins to walk the space between you in a semi-circle, as if stalking her prey.<p>'I always sleep naked, I hope that's not a problem.</p><button type='button' class='btn btn-success' id='invitebtn' onclick='buttonClicked(\"HotelStart\")'>Invite her in</button><button type='button' class='btn btn-warning' id='helpbtn' onclick='buttonClicked(\"HotelHelp\")'>Try to kiss her</button><button type='button' class='btn btn-danger' id='nobtn' onclick='buttonClicked(\"Ask\")'>Ask how the Swindon group can help with the murder</button>"; 
     break; 
    default: 
     console.log("That part of the story hasn't been written yet."); 
    }; 

}; 

onStart(); 
+1

發生錯誤時,通過語句減少代碼語句。當它消失時,你知道它在哪裏。 – zerkms

+4

KissDeer看起來像在「可以做你想做的事情」之前有一個回車符 - 這會是一個問題。 –

+0

由於@LeoFarmer注意到中斷字符串,其他所有內容都是有效的js – kwarunek

回答

3

你應該添加一個\到kissDeer結束條件,請參見下面的代碼。

case "KissDeer": 
 
     document.getElementById("storybox").innerHTML = "<p>You try to kiss her but she pushes you away.</p><p>'Did you think because I'm naked I'd be fair game? No thanks. I'll just have a sit down and a gin and tonic if you're offering.'</p><p>Deer-Wolf sits down naked on the sofa of the hotel room she has rented for you.</p><p>'It's a nice place. Double room. I'll be sleeping here too of course. It's got good security and I like the managers here. We get on well.'</p><p>Deer-Wolf gets up and begins to walk the space between you in a semi-circle, as if stalking her prey.<p>'I always sleep naked, but that's doesn't mean you\ 
 
    can do what you want. Hands to yourself or we forget the whole thing.</p><button type='button' class='btn btn-success' id='invitebtn' onclick='buttonClicked(\"HotelStart\")'>Invite her in</button><button type='button' class='btn btn-warning' id='helpbtn' onclick='buttonClicked(\"HotelHelp\")'>Try to kiss her</button><button type='button' class='btn btn-danger' id='nobtn' onclick='buttonClicked(\"Ask\")'>Ask how the Swindon group can help with the murder</button>"; 
 
     break;

1

有問題在這一行

case "KissDeer": 
     document.getElementById("storybox").innerHTML = "<p>You try to kiss her but she pushes you away.</p><p>'Did you think because I'm naked I'd be fair game? No thanks. I'll just have a sit down and a gin and tonic if you're offering.'</p><p>Deer-Wolf sits down naked on the sofa of the hotel room she has rented for you.</p><p>'It's a nice place. Double room. I'll be sleeping here too of course. It's got good security and I like the managers here. We get on well.'</p><p>Deer-Wolf gets up and begins to walk the space between you in a semi-circle, as if stalking her prey.<p>'I always sleep naked, but that's doesn't mean you 
    can do what you want. Hands to yourself or we forget the whole thing.</p><button type='button' class='btn btn-success' id='invitebtn' onclick='buttonClicked(\"HotelStart\")'>Invite her in</button><button type='button' class='btn btn-warning' id='helpbtn' onclick='buttonClicked(\"HotelHelp\")'>Try to kiss her</button><button type='button' class='btn btn-danger' id='nobtn' onclick='buttonClicked(\"Ask\")'>Ask how the Swindon group can help with the murder</button>"; 

可以做來自新線開工。

與他的絃樂加入它

喜歡這個

document.getElementById("storybox").innerHTML = "<p>Deer-Wolf thanks you, and walks past your into the hotel room, gazing fixedly into your eyes as she does so.<p>'It's a nice place. Double room. I'll be sleeping here too of course. It's got good security and I like the managers here. We get on well.'</p><p>Deer-Wolf gets up and begins to walk the space between you in a semi-circle, as if stalking her prey.<p>'I always sleep naked, I hope that's not a problem.</p><button type='button' class='btn btn-success' id='invitebtn' onclick='buttonClicked(\"HotelStart\")'>Invite her in</button><button type='button' class='btn btn-warning' id='helpbtn' onclick='buttonClicked(\"HotelHelp\")'>Try to kiss her</button><button type='button' class='btn btn-danger' id='nobtn' onclick='buttonClicked(\"Ask\")'>Ask how the Swindon group can help with the murder</button>"; 

JSFIDDLE

1

試試這個:您的字符串未關閉的吻鹿情況下,我只是打破了它一分爲二部分。

case "KissDeer": 
     document.getElementById("storybox").innerHTML = "<p>You try to kiss her but she pushes you away.</p><p>'Did you think because I'm naked I'd be fair game? No thanks. I'll just have a sit down and a gin and tonic if you're offering.'</p><p>Deer-Wolf sits down naked on the sofa of the hotel room she has rented for you.</p><p>'It's a nice place. Double room. I'll be sleeping here too of course. It's got good security and I like the managers here. We get on well.'</p><p>Deer-Wolf gets up and begins to walk the space between you in a semi-circle, as if stalking her prey.<p>'I always sleep naked, but that's doesn't mean you" 
     + "can do what you want. Hands to yourself or we forget the whole thing.</p><button type='button' class='btn btn-success' id='invitebtn' onclick='buttonClicked(\"HotelStart\")'>Invite her in</button><button type='button' class='btn btn-warning' id='helpbtn' onclick='buttonClicked(\"HotelHelp\")'>Try to kiss her</button><button type='button' class='btn btn-danger' id='nobtn' onclick='buttonClicked(\"Ask\")'>Ask how the Swindon group can help with the murder</button>"; 
     break; 
+0

我喜歡你的RPG的方式,它讓我發笑。 –

0

只是通過複製和粘貼代碼昇華會做的伎倆。你有一個新的換行KissDeer的情況。

can do what you want.

使它們成爲一行並且您的代碼已修復。

case "KissDeer": 
    document.getElementById("storybox").innerHTML = "<p>You try to kiss her but she pushes you away.</p><p>'Did you think because I'm naked I'd be fair game? No thanks. I'll just have a sit down and a gin and tonic if you're offering.'</p><p>Deer-Wolf sits down naked on the sofa of the hotel room she has rented for you.</p><p>'It's a nice place. Double room. I'll be sleeping here too of course. It's got good security and I like the managers here. We get on well.'</p><p>Deer-Wolf gets up and begins to walk the space between you in a semi-circle, as if stalking her prey.<p>'I always sleep naked, but that's doesn't mean you can do what you want. Hands to yourself or we forget the whole thing.</p><button type='button' class='btn btn-success' id='invitebtn' onclick='buttonClicked(\"HotelStart\")'>Invite her in</button><button type='button' class='btn btn-warning' id='helpbtn' onclick='buttonClicked(\"HotelHelp\")'>Try to kiss her</button><button type='button' class='btn btn-danger' id='nobtn' onclick='buttonClicked(\"Ask\")'>Ask how the Swindon group can help with the murder</button>"; 
    break;