Colossal Caves Adventure - 使用 javascript 访问数据源

发布于 2024-10-20 05:10:44 字数 322 浏览 2 评论 0原文

我一直在考虑创建原始 Colossal Caves Adventure 的 javascript 版本,作为自学语言的一种方式。冒险的原始数据文件可以在这里找到 -

http://jerz.setonhill.edu/if/ crowther/

我希望有经验的 javascripter 可以向我建议存储和访问文本数据文件(只有几百行长)的最佳方法。目前我正在考虑将文本嵌入 html 页面的隐藏元素中并以这种方式访问​​它,但我知道这缺乏优雅。你有什么想法?

I've been playing around with the idea of creating a javascript version of the original Colossal Caves Adventure, as a way to teach myself the language. The original data file for the adventure can be found here -

http://jerz.setonhill.edu/if/crowther/

I'm hoping an experienced javascripter can suggest to me the best way to store and access the text data file (which is only a few hundred lines long). At the moment I'm thinking of embedding the text in a hidden element in the html page and accessing it that way, but I know that lacks elegance. What are your thoughts?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

明明#如月 2024-10-27 05:10:44

您可以深入研究并学习 AJAX。 根据需要获取内容正是 AJAX 的强项。 (如果您正在学习 JS,您最终将学习 AJAX)

如果您确实想加载页面上的所有内容,我建议将内容分解为单独的

<input type="hidden" name="r121" value="You come upon a fissure" />

这可能意味着结果 r 121 是“你遇到了裂缝”

You could dive in the deep end and learn AJAX while you're at it. Grabbing content as needed is exactly what AJAX is great at. (If you're learning JS, you will eventually learn AJAX)

If you really want to load all content on page land, I would recommend breaking the content into individual <input type="hidden"s. JavaScript grabs their contents very easily, and you can give them descriptive ids that will make serving the content more intuitive during programming. e.g.

<input type="hidden" name="r121" value="You come upon a fissure" />

Which could mean result r 121 is "You come upon a fissure"

北城挽邺 2024-10-27 05:10:44

创建一个分配给变量的 javascript 对象,并将这段代码保存为单独的脚本。将数据和控制代码分开。

因此,也许您有一个名为 advdat.js 的文件,其中包含以下内容:

advdat = {  1: "YOU ARE STANDING AT THE END OF A ROAD BEFORE A SMALL BRICK\
                BUILDING . AROUND YOU IS A FOREST. A SMALL\
                STREAM FLOWS OUT OF THE BUILDING AND DOWN A GULLY.",
            2: "YOU HAVE WALKED UP A HILL, STILL IN THE FOREST\
                THE ROAD NOW SLOPES BACK DOWN THE OTHER SIDE OF THE HILL.\
                THERE IS A BUILDING IN THE DISTANCE.",
            3: "YOU ARE INSIDE A BUILDING, A WELL HOUSE FOR A LARGE SPRING.",...
          };

在加载主程序之前将其加载到其自己的脚本标记中。您的数据现在可在对象 advdat 中使用。

Create a javascript object assigned to a variable and save this piece of code as a separate script. Keeps the data and the control code separate.

So, maybe you have a file called advdat.js that contains this:

advdat = {  1: "YOU ARE STANDING AT THE END OF A ROAD BEFORE A SMALL BRICK\
                BUILDING . AROUND YOU IS A FOREST. A SMALL\
                STREAM FLOWS OUT OF THE BUILDING AND DOWN A GULLY.",
            2: "YOU HAVE WALKED UP A HILL, STILL IN THE FOREST\
                THE ROAD NOW SLOPES BACK DOWN THE OTHER SIDE OF THE HILL.\
                THERE IS A BUILDING IN THE DISTANCE.",
            3: "YOU ARE INSIDE A BUILDING, A WELL HOUSE FOR A LARGE SPRING.",...
          };

Load this in its own script tag before loading the main program. Your data is now available in the object advdat.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文