with文の名前領域 – ActionScript3.0

AS3を本気で勉強しようと思い過去に何度入門したかわからないAS3を再び始めました。今回は本気です!(ってみんな言うよね。)
とりあえずTextField()から入門してHelloWorld的なことをやろうと思い頑張っていたら、with文がなかなか厄介でつまづきました。べつに使う必要ないんだけどね。

とりあえず、HelloWorld的な。

[as]
//************************************************************
// HelloWorld – TextField()
// とりあえず文字を書き出してみる。
//
package {
import flash.display.*;
import flash.text.*;
public class Main extends Sprite {
//————————————————————
// Mainコンストラクタ
//
public function Main() {

//h1 TextFormat() を定義
var tf_h1:TextFormat = new TextFormat();
tf_h1.color = 0×666666;
tf_h1.size = 30;
tf_h1.font = ‘Times New Roman’;
tf_h1.align = TextFieldAutoSize.CENTER;

//titleを定義
var title:TextField = new TextField();
title.text = ‘HelloWorld!! – TextField()’;
title.width = stage.stageWidth;
title.height = 40;
title.x = 0;
title.y = (stage.stageHeight/2);
title.setTextFormat( tf_h1 );
title.background = true;
title.backgroundColor = 0xEEEEEE;

//stageに要素を追加
stage.addChild( title );
}
}
}
[/as]

▽結果

インスタンスのプロパティをwith文を使ってまとめてみる。

[as]
//titleを定義
var title:TextField = new TextField();
with( title ){
text = ‘HelloWorld!! – TextField()’;
width = stage.stageWidth;
height = 40;
x = 0;
y = (stage.stageHeight/2);
setTextFormat( tf_h1 );
background = true;
backgroundColor = 0xEEEEEE;
}
[/as]

・・・これがですね、実は思わぬ落とし穴があって動いてくれないんです。

with文の中身というのは、親オブジェクトの名前領域の中にいる状態(表現あってる?)です。ここで問題になっているのは、stageオブジェクトです。”stage.stageWidth” の部分。
親が”title”オブジェクトなので、with文の中では、”title.stage.stageWidth” というように見られてしまいます。もちろん、titleインスタンスにはそんな階層のプロパティをもっていません。
それでもwith文を使いたい場合はこのように書きます。

[as]
//titleを定義
var title:TextField = new TextField();
var stageInstance:Object = stage;
with( title ){
text = ‘HelloWorld!! – TextField()’;
width = stageInstance.stageWidth;
height = 40;
x = 0;
y = (stageInstance.stageHeight/2);
setTextFormat( tf_h1 );
background = true;
backgroundColor = 0xEEEEEE;
}
[/as]

一旦、上のスコープ内でstageを参照するオブジェクト”stageInstance” を生成して、”stageInstance” を参照しに行きます。この手間を考えると、そこまでしてwith文使う必要ないんじゃない?という気になります。名前領域に気を使うのはめんどくさ過ぎる気がします。

そんなwith文ちゃんでした。

カテゴリー: ActionScript3, Flash   パーマリンク

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

*

次のHTML タグと属性が使えます: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>