【问题标题】:Displaying a large string in sentences one click at a time一次单击在句子中显示一个大字符串
【发布时间】:2016-04-13 14:51:06
【问题描述】:
#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 元素,当点击时,一串文本会以一个大块的形式写入页面。

我的问题是,我怎样才能让它一次只加载一个句子,然后点击时加载下一个句子,然后下一个,直到字符串中没有其他内容?

【问题讨论】:

    标签: javascript string coffeescript adventure


    【解决方案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
    

    【讨论】:

    • 非常感谢。这很有帮助。
    猜你喜欢
    • 2012-12-01
    • 1970-01-01
    • 2012-08-06
    • 1970-01-01
    • 2012-04-08
    • 1970-01-01
    • 2013-04-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多