2016-04-13 33 views
0
#branchID pool 
branch0 = "This is a wall of text. This is a wall of text. This is a wall of text. This is a wall of text. This is a wall of text. This is a wall of text." 
branch1 = "This is a second wall of text." 
branch2 = "This is a third wall of text." 

#classes section 
    #pulls text from pools above. 
branch = (name, branchid)-> 
    stringID = String(branchid) 
    document.write("<h1 id=\'#{stringID}\'>#{name}</h1>") 
    document.getElementById(stringID).onclick = -> 
    document.write(branchid) 

#This is where the game goes. It will be built with conditionals 
window.onload = -> 
    branch('Start', branch0) 

我使用CoffeeScript创建基于浏览器的“选择您自己的冒险游戏”。以上是我迄今为止的代码。它创建一个HTML元素,并在单击时,一个文本字符串被写入一个巨块中的页面。一次单击一次显示句子中的大字符串

我的问题是,我怎么能这样做,所以一次只加载一个句子,然后单击时,下一句加载,然后下一句,直到字符串中没有其他任何内容?

回答

1

虽然我真的不知道,它是什么,你尽力去完成我用你的代码为基础,(希望)你想要做什么做了一个演示给你:

https://jsfiddle.net/Exinferis/qhr7wjmu/

#branchID pool 
firstBranch = [ 
    "This is a wall of text. This is a wall of text. This is a wall of text. This is a wall of text. This is a wall of text. This is a wall of text." 
    "This is a second wall of text." 
    "This is a third wall of text." 
] 

currentStep = 0 

#classes section 
    #pulls text from pools above. 
branch = (event)-> 
    if firstBranch[currentStep]? 
    document.getElementById("text").innerHTML = firstBranch[currentStep] 
    currentStep++ 

#This is where the game goes. It will be built with conditionals 
window.onload = -> 
    document.getElementById("nextbtn").onclick = branch 
+0

非常感谢你。这非常有帮助。 – bryanwillis7