从 playn 中的文件读取
这是一个愚蠢的问题。
如何在玩游戏时读取文件?我尝试像通常在标准 java 程序中那样使用 File 和 Scanner:
void readFromFile(){
int x;
int y;
int pixel;
int[][] board;
try{
Scanner scan = new Scanner(new File(in));
x = scan.nextInt();
y = scan.nextInt();
pixel = scan.nextInt();
Point start = new Point(scan.nextInt(), scan.nextInt());
Point dir = new Point(scan.nextInt(), scan.nextInt());
Point end = new Point(scan.nextInt(), scan.nextInt());
int antRoads = scan.nextInt();
board = new int[x][y];
for (int i = 0; i < y; i++){
for (int j = 0; j < x; j++){
board[i][j] = scan.nextInt();
}
}
lev = new Level(board, start, dir, end, antRoads, pixel, x, y);
} catch(FileNotFoundException e){
System.out.println(e);
}
}
我测试了 File.canRead()、canWrite() 和 can Execute(),它们都返回 false。
我应该使用 assetMannager().getText() 还是其他东西?如果是这样的话,有人可以告诉我它是如何工作的吗? (或者 ResourceCallback 是什么以及如何工作?)
我的目标是有一个名为“Maps”的文件夹,其中填充了常规文本格式的地图,就像标准图像文件夹一样。
问候, 托盖尔
Here's a stupid question.
How do you read files in a playn game? I tried using File and Scanner like I usually do in a standard java program:
void readFromFile(){
int x;
int y;
int pixel;
int[][] board;
try{
Scanner scan = new Scanner(new File(in));
x = scan.nextInt();
y = scan.nextInt();
pixel = scan.nextInt();
Point start = new Point(scan.nextInt(), scan.nextInt());
Point dir = new Point(scan.nextInt(), scan.nextInt());
Point end = new Point(scan.nextInt(), scan.nextInt());
int antRoads = scan.nextInt();
board = new int[x][y];
for (int i = 0; i < y; i++){
for (int j = 0; j < x; j++){
board[i][j] = scan.nextInt();
}
}
lev = new Level(board, start, dir, end, antRoads, pixel, x, y);
} catch(FileNotFoundException e){
System.out.println(e);
}
}
I tested File.canRead(), canWrite() and can Execute() and they all returned false.
Am I supposed to use assetMannager().getText() or something? If that's the case can someone tell me how it works? (or what is and how ResourceCallback works?)
My goal is to have a folder named "Maps" filled with maps in regular text-format just like the standard Image folder.
Regards,
Torgeir
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

你不能在PlayN游戏中进行普通的文件I/O,因为游戏被编译成JavaScript并在浏览器中运行(使用HTML5后端时),而浏览器不支持文件I/O(至少不支持通用文件)用于这些目的所需的 I/O)。
浏览器甚至不支持字节流或任何类型的二进制 I/O 的概念(这可能最终会实现,但要得到所有浏览器的支持还需要很长时间)。
所以你必须使用AssetManager.getText来读取数据文件。如果您愿意,可以将它们编码为 JSON,并使用
PlayN.json()
对其进行解码,或者您也可以使用您自己的自定义基于字符串的格式。如果您不打算使用 HTML5 后端进行部署,则可以创建一个“LevelReader”接口并在 Android 或 iOS 后端中实现该接口,并利用这些平台上的本机文件 I/O 功能。
You cannot do normal file I/O in a PlayN game, because the games are compiled into JavaScript and run in the browser (when using the HTML5 backend), and the browser supports no file I/O (at least not the general purpose file I/O you would need for these purposes).
Browsers also do not even support the idea of a byte stream, or any sort of binary I/O (this may eventually arrive, but it will be ages before it's supported for all browsers).
So you have to use
AssetManager.getText
to read data files. You can encode them in JSON if you like and usePlayN.json()
to decode them, or you can use your own custom string-based format.If you don't plan to deploy using the HTML5 backend, you can create a "LevelReader" interface and implement that interface in your Android or iOS backend and make use of the native file I/O capabilities on those platforms.