ドキュメントクラスパス
こにゃにゃちは。
相変わらず世の中のASerの流れに完全に取り残された私の覚書。
Tweenerなどの超頭がいい人が作ってくれたライブラリや自分で作ったカスタムクラス(まだまともなものは作ったことなし(笑))をSWFに読み込むにはimportっちゅうのをしないといけないのですが、それらのライブラリがどこにあるかflaファイルに教えてあげないといけませぬ。
AS2で非常にお世話になったFuseKit。にゃあプロジェクトさんやtrick7さんの記事を見て、あれやこれや試してみたものです。
その当時(というか最近まで)グローバルクラスパスという方法でライブラリを読み込んでおりましたが、どうやら最近(というかだいぶ前(汗))からドキュメントクラスパスを設定して、プロジェクトごとに利用するライブラリやカスタムクラスなどをひとつのフォルダ以下にまとめようという方向らしい(trick7さんより)。師匠のいないおれは知らんかったぜい。
なんならWEB Designingの一昨年の8月号に記事を書いてると…。買ってなかったぜ…。amazonにて中古でポチっとしたよ…
てなわけで今後はドキュメントクラスパス方式で行くと心に決めた。
【やり方(というほどのものでもない)】
1.新規で作ったflaファイルのパブリッシュ設定を開く
2.パブリッシュ設定画面で「スクリプトの設定ボタンを押す。」
という感じで設定するそうな。
世の中の潮流でいうと
flaファイルの場所が
/hoge/hogehoge/test.fla
という感じであればライブラリやカスタムクラスは
/hoge/hogehoge/src/
と「src」というディレクトリを作ってぶち込んでいくそうな。
たしかにこのほうが綺麗。
ただここで一つ。
Flashのデフォルトではドキュメントクラスパスの設定画面において「.(ドット)」というパスが通っている。
これはflaファイルと同階層にクラスファイルがあれば自動的にクラスパスが通っているということである。
ではなぜ「/hoge/hogehoge/src/」というクラスパスを通したかというと先程述べた通りまとめるためということですが、単純にまとめるだけなら「src」フォルダを作りそこにライブラリなどをぶち込んで、flaファイルからimportすればいいんじゃない?と思ったりしたんですが、そうではないようです。
どうやら「package」というものが関わっているようです。
「package」に関する説明はFlashの枝さんのサイトでちらっと見ました(笑)
「クラスファイルが入っているディレクトリの事」とあります。
諸所の先人たちが作ってくださったライブラリ達はライブラリ同士が名前のバッティングをしないために、ドメイン名を元にしたフォルダ構成でなどで配布されています。(Tweenerのフォルダ構成の由来はよく知りませんが、大抵「com」や「net」などのドメインのおしりから始まっています。)
このようなフォルダに入ったライブラリをflashで読み込む際にクラスパスをデフォルトの「.(ドット)」のみの場合、
■Tweenerの「caurina」フォルダをflaファイルと同階層に置いて
import caurina.transitions.Tweener;
とflaファイルのタイムラインに記述した場合は正常に動作しますが、
■Tweenerの「caurina」フォルダをflaファイルと同階層の「src」フォルダ内に設置した場合
import caurina.transitions.Tweener;
と記述しても正常には動作しません。
flaファイルがTweenerのライブラリを見つけられないからです。
では「src」フォルダに対してドキュメントクラスパスを通さずに、flaファイルにTweenerのライブラリの場所を認識させるためにはどうすればいいかというと
まずflaファイルのタイムラインに
import src.caurina.transitions.Tweener;
と「src」フォルダまでの階層を指定してあげます。
さらにはTweenerの本体でもある「Tweener.as」のpackage宣言部分を
package caurina.transitions {
から↓
package src.caurina.transitions {
という宣言に変えなければいけません。
せっかく誰でも利用できる状態でライブラリを配布してくれているのに、ライブラリ本体に手を加えちゃうのは嫌だしめんどくさい。
なので自分が使い易い場所にクラスファイルをまとめる場合は、ドキュメントクラスパスを追加で通して、package宣言部分をさわらなくても、flaファイルにライブラリの場所をお知らせしてあげる必要があるということなのだろう。(と勝手に解釈。)
なんでこんなことを書いているかというと
flaファイルと同階層の「src」フォルダにドキュメントクラスパスを通して、さらにその中に「sub」というフォルダを作って、その中にTest.asというカスタムクラスを作ってflaファイルから読み込ませようとしました。
んでカスタムクラス内にはよくある記述例を元に
package {
class Test {
function Test() {
trace("UNCO");
}
}
}
と記述し、flaファイルから
import src.Test;
と記述すればUNCOがトレースされるのかと思いきやトレースされない…
なぜだかちんぷんかんぷんだったのですが、
カスタムクラスのpackage宣言に「package名」というのを記述するとな…
こんな基本的なことも知らんかったぜぃ。巷によくあるカスタムクラスのサンプルは「package名」を省略しているのだなと…
package sub{
class Test {
function Test() {
trace("UNCO");
}
}
}
上記のように「package名(フォルダ名)」を追記してあげることでできました。
そんなこんながあり、クラスパスをとおすということがちらっとわかった気がしました。
長々となりましたが、ドキュメントクラスパスを通す事の意味合いを把握したつもりですが、履き違えてたら誰か教えてください。