使用Dart for HTML5应用程序,但要从服务器端加载文件 [英] Using Dart for HTML5 app, but want to load a file from the server-side

查看:51
本文介绍了使用Dart for HTML5应用程序,但要从服务器端加载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Dart的新手,并试图创建我的第一个Dart网络游戏.对不起,如果我错过了与此相关的已回答问题.我确实搜索过,但运气不佳.

I'm new to Dart, and trying to create my first Dart web game. Sorry if I missed an answered question related to this. I did search, but wasn't having much luck.

要加载一个关卡,我希望能够读取一个包含关卡数据的文本文件,然后对其进行处理并使用它来构建关卡.不幸的是,如果您要使用File()对象,那么我将遇到无法同时加载dart:io和dart:html的问题.据我所知,dart:html的File()对象是客户端,因此无法打开服务器上的文本文件.

To load a level, I would like to be able to read in a text file with the level data, then process that and use it to build the level. Unfortunately, I am running into the issue where dart:io and dart:html can not both be loaded if you want to use the File() object. From what I can tell, dart:html's File() object is client-side, so that would not be able to open the text-file I will have on the server.

还有另一种方法可以从服务器读取文本文件吗?如果不是,我是否需要建立一个数据库来存储游戏数据,还是我在这里没有考虑一个更好的选择?

Is there another way to read in a text file from the server? If not, do I need to set up a database just to store the game data, or is there a better option I'm not thinking about here?

如果有帮助,我目前使用的游戏数据只是一个文本文件,它提供了关卡外观的地图.例如:

In case it helps, the game data I'm working with currently is just a text file that gives a map of what the level will look like. For example:

~~~~ Z ~~~~

~~~~Z~~~~

P

GGGGLLGGG

GGGGLLGGG

这些字符中的每个字符都表示要放置在关卡中的一种块.这不是存储级别的最佳方法,但是它很容易创建,易于阅读和处理.

Each of those characters would denote a type of block to be placed in the level. It's not the best way to store levels, but it is pretty easy to create and easy to read in and process.

非常感谢您的帮助!

推荐答案

如果要加载的文件是从中加载游戏的 index.html 的同级文件,则只需HTTP请求以获取文件.

If the file you are loading is a sibling of the index.html your game is loaded from, then you can just make an HTTP request to fetch the file.

要下载 web/level1.json ,您可以使用

Future<String> getGameData(String name) {
  var response = await HttpRequest.getString('${name}.json');
  print(response);
  return response;
}

otherMethod() async {
  var data = await getGameData('level1');
}

另请参见 https://api.dartlang.org/stable/1.24.3/dart-html/HttpRequest-class.html

这篇关于使用Dart for HTML5应用程序,但要从服务器端加载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆