2016-04-13 30 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