LoginSignup
5
5

More than 5 years have passed since last update.

【AS3】コンパイル時の-debugオプションをFlash側から無理やり取得。

Last updated at Posted at 2012-07-09

リリースモード(-debug=false)でコンパイルしたか、デバッグモード(-debug=true)でコンパイルしたかを取得しようと思ったけどそんな変数はなさそうだったので、無理やり取得する方法を考えた。

trace( hoge )が無視されることを利用する。

isDebug.as
public function isDebug():Boolean {
    var b:Boolean;
    trace( b = true );
    return b;
}

リリースモードの場合、trace( b = true )がまるまる無視されて、falseがかえってくる。

無駄にtrace( true )しないバージョン

上のコードだと、余分にtrace( true )されるので、別バージョンを考える。

isDebug.as
public function isDebug():Boolean {
    try{trace(b.$)}catch(e:*){var b:*=e}
    return b
}

trace中にわざとエラーを起こして、catchする。

応用例

IDEのデバッグコンソールとブラウザのJavaScriptコンソール(ChromeだとF12キーで出てくるやつ)に同時にtrace。

log.as
public function log( ...rest ):void {
    trace((
        ExternalInterface.available && 
        ExternalInterface.call.apply(0,["console.log"].concat(rest)), 
        rest.join(" ")
    ));
}

-debug=falseにすれば、JavaScriptコンソール側の出力も消えてすっきり。JavaScriptが使えない環境でもエラーは起きないし、本来のデバッグコンソールに出力される内容はtraceと同じ。

5
5
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5