2014-12-04 27 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

相關問題