HOME>WEBプログラム覚書>Actionscript3 [基礎]ドキュメントクラス

Actionscript3 [基礎]ドキュメントクラス

ここからがいよいよ本番か?

ドキュメントクラスとは

swfファイルに関連付けられたクラス。 swfがロードされると同時に自動的にインスタンスが生成される。

ドキュメントクラスを利用することで flaファイルのキーフレームはまっさらでOK。

flaファイルは画像管理用に使うようにすると いいのかな?

ドキュメントクラスにするための条件

  • MovieClipまたはSpriteクラスを継承する。
  • 必要なクラスはすべてインポートする。
  • ステージに配置したオブジェクトは変数で定義しない

サンプル

ボタンクリックで円の透明度を変更

ActionScript3.0

  1. package {
  2.     import flash.display.Sprite;
  3.     import flash.display.Shape;
  4.     import flash.display.SimpleButton;
  5.  
  6.     import flash.events.MouseEvent;
  7.  
  8.     public class DocSample extends Sprite {
  9.  
  10.         var circle:Shape;
  11.         var circle_state:Boolean = true;
  12.  
  13.         public function DocSample():void {
  14.  
  15.             // ボタン作成
  16.             var bt:SimpleButton = createButton();
  17.             bt.x = stage.stageWidth / 2;
  18.             bt.y = (stage.stageHeight / 2) + 100;
  19.  
  20.             // 円作成
  21.             this.circle = createCircle();
  22.             this.circle.x = stage.stageWidth / 2;
  23.             this.circle.y = stage.stageHeight / 2;
  24.  
  25.             // イベント追加
  26.             bt.addEventListener(MouseEvent.CLICK, onClick);
  27.  
  28.             // 表示リストに追加
  29.             addChild(bt);
  30.             addChild(this.circle);
  31.         }
  32.  
  33.  
  34.         private function createButton():SimpleButton {
  35.             var bt:SimpleButton = new SimpleButton();
  36.             var sp:Shape = new Shape();
  37.  
  38.             sp.graphics.beginFill(0xCCCCCC);
  39.             sp.graphics.lineStyle(0, 0x000000);
  40.             sp.graphics.drawRect(0, 0, 100, 20);
  41.             sp.graphics.endFill();
  42.  
  43.             bt.upState = sp;
  44.             bt.overState = sp;
  45.             bt.hitTestState = sp;
  46.  
  47.             return bt;
  48.         }
  49.  
  50.  
  51.         private function createCircle():Shape {
  52.             var sp:Shape = new Shape();
  53.  
  54.             sp.graphics.beginFill(0x99CCFF);
  55.             sp.graphics.lineStyle(0, 0x000000);
  56.             sp.graphics.drawCircle(0, 0, 50);
  57.             sp.graphics.endFill();
  58.  
  59.             return sp;
  60.         }
  61.  
  62.         /**
  63.          * ボタンクリックで
  64.          * 表示/非表示を繰り返す
  65.          */
  66.         public function onClick(event:MouseEvent):void {
  67.  
  68.             if (this.circle_state) {
  69.                 this.circle.alpha = 0.2;
  70.                 circle_state = false;
  71.             } else {
  72.                 this.circle.alpha = 1;
  73.                 circle_state = true;
  74.             }
  75.  
  76.         }
  77.     }
  78. }
  79.  

「swfがロードされると同時に自動的にインスタンスが生成される。」って説明だと なんでこれが表示されるのか不思議だった。 「swfがロードされると同時にインスタンスが生成されてstageにaddChild()される」って感じっぽい。

ただ作成されたドキュメントクラスのインスタンスは イベントを検知できないっぽい。

最初はできないのかと思い、それらしきプロパティないか調べたけどどうもできない。 Spriteを継承してるし出来そうなんだけどできない。

マウスイベントを検出するにはSpriteとか作るとできる。

ActionScript3.0

  1. package {
  2.     import flash.display.Sprite;
  3.     import flash.display.Shape;
  4.     import flash.display.SimpleButton;
  5.  
  6.     import flash.text.TextField;
  7.  
  8.     import flash.events.MouseEvent;
  9.  
  10.  
  11.     public class DocSample2 extends Sprite {
  12.  
  13.         var circle:Shape;
  14.         var circle_state:Boolean = true;
  15.  
  16.         public function DocSample2():void {
  17.  
  18.             // ボタン作成
  19.             var bt:Sprite = createSprite();
  20.             bt.x = stage.stageWidth / 2;
  21.             bt.y = (stage.stageHeight / 2) + 100;
  22.  
  23.             // 円作成
  24.             this.circle = createCircle();
  25.             this.circle.x = stage.stageWidth / 2;
  26.             this.circle.y = stage.stageHeight / 2;
  27.  
  28.             // インスタンスに色々と設定
  29.             this.buttonMode = true;
  30.  
  31.             this.graphics.beginFill(0xAAAFFF);
  32.             this.graphics.drawRect(30, 200, 150, 170);
  33.             this.graphics.endFill();
  34.             this.opaqueBackground = 0xCFCFCF;
  35.             this.addEventListener(MouseEvent.CLICK, onClick);
  36.  
  37.             // テキストフィールド作成
  38.             var tf:TextField = new TextField();
  39.  
  40.             tf.autoSize = "left";
  41.             tf.appendText("this.parent : " + this.parent + "\n");
  42.             tf.appendText("this.visible : " + this.visible + "\n");
  43.             tf.appendText("this.alpha : " + this.alpha + "\n");
  44.             tf.appendText("this.mouseEnabled : " + this.mouseEnabled + "\n");
  45.             tf.appendText("this.getRect(this) : " + this.getRect(this) + "\n");
  46.             tf.appendText("this.opaqueBackground : " + this.opaqueBackground + "\n");
  47.             tf.appendText("this.buttonMode : " + this.buttonMode);
  48.  
  49.             addChild(bt);
  50.             addChild(tf);
  51.             addChild(this.circle);
  52.  
  53.         }
  54.  
  55.  
  56.         private function createSprite():Sprite {
  57.             var bt:Sprite = new Sprite();
  58.  
  59.             bt.graphics.beginFill(0xCCCCCC);
  60.             bt.graphics.lineStyle(0, 0x000000);
  61.             bt.graphics.drawRect(0, 0, 100, 20);
  62.             bt.graphics.endFill();
  63.             return bt;
  64.         }
  65.  
  66.  
  67.         private function createCircle():Shape {
  68.             var sp:Shape = new Shape();
  69.  
  70.             sp.graphics.beginFill(0x99CCFF);
  71.             sp.graphics.lineStyle(0, 0x000000);
  72.             sp.graphics.drawCircle(0, 0, 50);
  73.             sp.graphics.endFill();
  74.  
  75.             return sp;
  76.         }
  77.  
  78.         public function onClick(event:MouseEvent):void {
  79.  
  80.             if (this.circle_state) {
  81.                 this.circle.alpha = 0.2;
  82.                 circle_state = false;
  83.             } else {
  84.                 this.circle.alpha = 1;
  85.                 circle_state = true;
  86.             }
  87.  
  88.         }
  89.  
  90.     }
  91. }
  92.  
投稿日 2009年7月17日 03:01
カテゴリ ActionScript
タグ Flash | サンプルコード | 言語仕様
トラックバック URL http://www.kantenna.com/cgi-bin/mt504/mt-tb.cgi/1180

コメント

コメントする
Name
Email Address
URL