Posts match “ Haxe ” tag:

For loops in Haxe

for (i in 0...5) {
  trace(i);
}

output: 0,1,2,3,4

如果今天要改成
4,3,2,1,0
通常我會用 while

var i=5;
while (i-->0) {
  trace(i);
}

一定要用 for 的話

for (i in -5...0) {
  trace(-i-1);
}

Inlining Static Variables

AS3:

static public const OFFSET_Y:Int = 100;

Haxe:

static inline public var OFFSET_Y:Int = 100;
static inline var OFFSET_Y:Int = 100;

inline 有下列特性:

  • 宣告時需初始化並給值
  • 不得修改

Tracing output

一般是輸出到螢幕,
如果想改成輸出到 flashlog.txt 有兩種方式。

  • 第一種: 用內建的 trace

    flash.Lib.trace('hello world')
    
  • 第二種: 開啟 fdb

    <haxedef name="fdb" if="flash" />
    

HTTP GET/POST request on iOS

GET

跟 as 用法一樣,基本上沒什麼問題。

POST

記得要指定 request.method = URLRequestMethod.POST
不然會音訊全無..

Debug

開啟 verbose ,真的很好用。
request.verbose = true;

#if cpp
var variables:URLVariables = new URLVariables();
variables.Name = "isobar";
variables.Parameter = Json.stringify(obj); // json

var request:URLRequest = new URLRequest(Config.BASE_URL + __api);
request.verbose = true;
request.method = URLRequestMethod.POST;
request.data = variables;

var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, function(e:Event) {
    trace("e:"+e.target.data.toString());
});
loader.load(request);
#end