2014-12-04 76 views

回答

1

简单的答案是使用“资源”。您添加路径和标识您hxml

-resource [email protected] 

你用它在你的代码是这样的:

var welcome = haxe.Resource.getString("welcome"); 

注意,操作是在编译时进行的,所以没有运行时开销。它基本上等同于将文件内容嵌入到带引号的字符串中。

复杂的答案是使用宏。有了它们,您可以加载,解析,处理和执行您可能需要的所有操作。通常情况下,您可以看到宏载入一个配置文件(比如JSON或YAML),并将其用作应用程序的一部分(同样在编译时,而不是在运行时)。

0

只要您将它们放在公共位置(如果您将其放置在线)并可供脚本访问,就可以使用XMLHttpRequest获取文件。

这里是从位置资产/ test.txt的

抓住一个文本文件,这是诸如此类的事情,我通常做的JS比赛,我做的一个简单的例子,我觉得比只是多一点灵活用 - 资源嵌入它们。

如果这不是你正在寻找的东西,那么佛朗哥的答案应该会看到你通过。

package ; 

import js.html.XMLHttpRequest; 
import js.html.Event; 

class Start { 
    static function main() { 
     var request = new XMLHttpRequest(); 

     // using the GET method, get the file at this location, asynchronously 
     request.open("GET", "assets/test.txt", true); 

     // when loaded, get the response and trace it out 
     request.onload = function(e:Event){ 
      trace(request.response); 
     }; 

     // if there's an error, handle it 
     request.onerror = function(e:Event) { 
      trace("error :("); 
     }; 

     // send the actual request to the server 
     request.send(); 
    } 
} 
+1

您还可以使用标准的haxe.Http类来加载内容。这也将使它在其他平台上也能够工作。 – 2014-12-20 09:38:24