- 2006-07-25 (火) 23:01
- action script
ActionScript3.0の勉強のため、adobeの上條さんのブログをプリントして、読み進めていたのですが
2005/11/21の記事「flash.display パッケージのとりあえずのまとめ」のHelloWorldを表示するサンプルコードを「Flash Professional 9 ActionScript 3.0 Preview」環境でやってみたものの、エラーが出てしまう。
どうも去年の年末から、パッケージ構成等、仕様が変わっているので、一部サンプルコードを書き直さないといけないらしい。
不便だったのは、このPreview版はシンタックスチェックをAS2.0構文でするっぽいので、エラーチェックは当てにならない。(というか2.0チェックなので1行目のpackageからエラー扱い)
結果、ネットを巡りつつ1時間ほど悪戦苦闘してしまいました。スタート地点のHelloWorldすら出せない自分に絶望を感じました。難なく突破された方も多いと思いますが、一応Preview版で動くソースを載せておきます。
オリジナルのサンプルソースは、「akihiro kamijo: flash.display パッケージのとりあえずのまとめ」です。
package{
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.events.Event;
//import flash.events.EventType;を削除
import flash.geom.Point;
import flash.geom.Ractangle;
public class HelloWorld extends Sprite{
private var label:TextField;
private var target:Point=new Point();
public function HelloWorld(){
createLabel();
addEventListener(Event.ENTER_FRAME,enterFrameListener);
}
private function createLabel():void{
label=new TextField();
label.text="Hello World";
var format:TextFormat=new TextFormat();
format.font="Verdana";
format.color="0xff0000";
format.size=16;
label.setTextFormat(format);
addChild(label);
}
public function enterFrameListener(event:Event):void{
var current:Point=label.getBounds(this).topLeft;
if(Point.distance(target,current)<1){
resetTarget();
}
label.x+=(target.x-label.x)/8;
label.y+=(target.y-label.y)/8;
}
private function resetTarget():void{
target.x=Math.random()*stage.stageWidth-100;
target.y=Math.random()*stage.stageHeight-10 ;
}
}
}
赤字がサンプルソースからの修正箇所です。ゼロから手打ちしたので改行とかスペースとかなくてすいません。
要は
- TextFieldはdisplayパッケージからtextパッケージに移動。(livedocs: Class TextField)
- EventTypeはEventクラスに統合され、ENTER_FRAMEもそっちに移された。(livedocs: Class Event)
- Voidはvoidに。(クラスじゃないからかな?)
あとはflaファイルのプロパティパネルのDocument classをHelloWorldにしてパブリッシュ。これで動きます。
ルートがSpriteクラスのインスタンスになってるのが不思議な気分ですが、これで一応動いてます。
- Newer: Experience Color | Panasonic Global
- Older: VAIO
Comments:0
Trackbacks:0
- Trackback URL for this entry
- http://www.trick7.com/blog/2006/07/25-230128.php/trackback
- Listed below are links to weblogs that reference
- akihiro kamijoさんのHelloWorldのサンプル from trick7






![Flash Math & Physics Design:ActionScript 3.0による数学・物理学表現[入門編]](http://ecx.images-amazon.com/images/I/617sNhK9P9L._SL75_.jpg)


![FLASH ActionScript 2.0入門完全ガイド+実践サンプル集 [CD-ROM付]](http://ecx.images-amazon.com/images/I/51915J1BG6L._SL75_.jpg)


