はじめに

お仕事でちょっとした swf を作った方がよさげな状況な今日この頃,さて,どうしたものか...

以前,WCAN mini ActionScript vol. 4 の懇親会で (一部で) 話題に挙がった haXe (「ハックス」 とか 「エックス」 とか読むようです),これを使ってみることにしました.

haXe のインストール

※ 以下,Debian さん 4.0 で実行しています.

apt では見つからないっぽいので,本家サイトのダウンロードページ から任意の場所へダウンロードして展開.make します.


% wget "http://haxe.org/_media/haxe-1.17-linux.tar.gz?id=download&cache=cache"
% tar zxf haxe-1.17-linux.tar.gz
% cd haxe-1.17-linux
% make
make: *** ターゲットが指定されておらず, makefile も見つかりません.  中止.
% ls
CHANGES.txt  LICENSE.txt  doc  haxe  haxedoc  haxelib  std
% ./haxe
Haxe Compiler 1.17 - (c)2005-2007 Motion-Twin
 Usage : haxe.exe [options] <class names...>
 Options :
  -cp <path> : add a directory to find source files
  -js <file> : compile code to JavaScript file
...
(ry
...
  -help  Display this list of options
  --help  Display this list of options
%

...と思ったら,もう実行可能になってるみたいですね.

では,これをパスの通っているところへ移します.私の場合,簡単のためにシンボリックリンクだけ張っています.


% [sudo] ln -s /path/to/download/haxe-1.17-linux/haxe /usr/local/bin/haxe

nekoVM のインストール

とありますが,

ぽいので,今回はスルーの方向で.

ソースコードのコンパイル

haXe な言語で書かれたソースコード (以下,hxファイル) は,haxe コマンドを使ってコンパイルを行い,オプションをいろいろ付加することで目的のファイルを生成します.(ここでは swf ですね.)

引数なしで haxe コマンドを実行するとオプションのリストが得られます.


% ./haxe
Haxe Compiler 1.17 - (c)2005-2007 Motion-Twin
 Usage : haxe.exe [options] <class names...>
 Options :
  -cp <path> : add a directory to find source files
  -js <file> : compile code to JavaScript file
  -as3 <directory> : generate AS3 code into target directory
  -swf <file> : compile code to Flash SWF file
  -swf-version <version> : change the SWF version (6,7,8,9)
  -swf-header <header> : define SWF header (width:height:fps:color)
  -swf-lib <file> : add the SWF library to the compiled SWF
  -neko <file> : compile code to Neko Binary
  -x <file> : shortcut for compiling and executing a neko file
  -xml <file> : generate XML types description
  -main <class> : select startup class
  -lib <library[:version]> : use an haxelib library
  -D <var> : define a conditional compilation flag
  -resource <file@name> : add a named resource file
  -exclude <filename> : don't generate code for classes listed in this file
  -v : turn on verbose mode
  -debug : add debug informations to the compiled code
  -prompt : prompt on error
  -cmd : run the specified command after successful compilation
  --flash-strict : more type strict flash API
  --override : ensure that overriden methods are declared with 'override'
  --no-traces : don't compile trace calls in the program
  --flash-use-stage : place objects found on the stage of the SWF lib
  --neko-source : keep generated neko source
  --gen-hx-classes <file> : generate hx headers from SWF9 file
  --next : separate several haxe compilations
  --altfmt : use alternative error output format
  --auto-xml : automatically create an XML for each target
  --display : display code tips
  --no-output : compiles but does not generate any file
  --times : mesure compilation times
  -help  Display this list of options
  --help  Display this list of options
%

swf を生成する場合は,次のようなオプションをつけてコマンドを実行します.


% haxe -main Hoge -swf-header 100:100:30:ffffff -swf hoge.swf

本エントリのサンプルとして,次のようなソースコードを用意してみました.


class Post451 {
	public function new() {
		trace( 'hogehoge' );
		trace( 'ほげほげ' );
	}
	public static function main() {
		var  post451: Post451 = new Post451();
	}
}

これをコンパイルしてみます.


% ls
Post451.hx
% haxe -main Post451 -swf-header 200:200:30:cccccc -swf post451.swf
% ls
Post451.hx  post451.swf
%

無事に通ったっぽいです.

swfの埋め込み

HTML に swf を埋め込む場合,現在では,object タグや embed タグを使わない,SWFObject を使った埋め込み和訳) が主流になっているようですので,これを使ってみることにします.

まず,先に挙げたサイトから swfobject.js (2008-02-10 時点で ver. 1.5) を入手して,これをインポートします.

<script type="text/javascript" src=".../swfobject.js"></script>

あとは,次のような JavaScript を書けばOKです.


var so = new SWFObject( 'swfのURL', 'swfのオブジェクト名(?)', '幅px', '高さpx', 'プレイヤのバージョン', '背景色' );
so.write( '埋め込み対象の要素のID' );

先でコンパイルしたswfの場合は,例えば,次のように書きます.(これは,実際に冒頭の swf を埋め込むたのものです.)


var so451 = new SWFObject( './swf/post451.swf', 'post451', '200', '200', '7', '#cccccc' );
so451.write( 'swf-451' );

結果

冒頭の swf がそれです.

おわりに

haXe を使って簡単な swf を作ることができました.

haXe は swf の他,JavaScript コードや nekoVM バイナリなんかも生成することができるようなので,ひとつのソースコードでクライアント側もサーバ側もカバーしてくれてしまいます.(nekoVM ってそういう役割なんですよね,きっと...)

まぁこの辺りはおいおい勉強するとして,しばらくの間は swf の生成に絞っていろいろ試してみたいと思います.

関連エントリ

このエントリへのトラックバック

このエントリへのトラックバックはまだありません.


このエントリへのコメント »

このエントリへのコメントはまだありません.