2009年07月17日 03:01

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

| http://www.kantenna.com/cgi-bin/mt/mt-tb.cgi/478

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

ドキュメントクラスとは

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

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

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

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

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

サンプル

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


package {
    import flash.display.Sprite;
    import flash.display.Shape;
    import flash.display.SimpleButton;
    
    import flash.events.MouseEvent;
    
    public class DocSample extends Sprite {

        var circle:Shape;
        var circle_state:Boolean = true;
        
        public function DocSample():void {
            
            // ボタン作成
            var bt:SimpleButton = createButton();
            bt.x = stage.stageWidth / 2;
            bt.y = (stage.stageHeight / 2) + 100;
            
            // 円作成
            this.circle = createCircle();
            this.circle.x = stage.stageWidth / 2;
            this.circle.y = stage.stageHeight / 2;
            
            // イベント追加
            bt.addEventListener(MouseEvent.CLICK, onClick);
            
            // 表示リストに追加
            addChild(bt);
            addChild(this.circle);
        }
        
        
        private function createButton():SimpleButton {
            var bt:SimpleButton = new SimpleButton();
            var sp:Shape = new Shape();
            
            sp.graphics.beginFill(0xCCCCCC);
            sp.graphics.lineStyle(0, 0x000000);
            sp.graphics.drawRect(0, 0, 100, 20);
            sp.graphics.endFill();
            
            bt.upState = sp;
            bt.overState = sp;
            bt.hitTestState = sp;
            
            return bt;
        }
        
        
        private function createCircle():Shape {
            var sp:Shape = new Shape();
            
            sp.graphics.beginFill(0x99CCFF);
            sp.graphics.lineStyle(0, 0x000000);
            sp.graphics.drawCircle(0, 0, 50);
            sp.graphics.endFill();

            return sp;
        }
        
        /**
         * ボタンクリックで
         * 表示/非表示を繰り返す
         */
        public function onClick(event:MouseEvent):void {
            
            if (this.circle_state) {
                this.circle.alpha = 0.2;
                circle_state = false;
            } else {
                this.circle.alpha = 1;
                circle_state = true;
            }
            
        }
    }
}

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

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

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

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


package {
    import flash.display.Sprite;
    import flash.display.Shape;
    import flash.display.SimpleButton;
    
    import flash.text.TextField;
    
    import flash.events.MouseEvent;
    
    
    public class DocSample2 extends Sprite {

        var circle:Shape;
        var circle_state:Boolean = true;
        
        public function DocSample2():void {
            
            // ボタン作成
            var bt:Sprite = createSprite();
            bt.x = stage.stageWidth / 2;
            bt.y = (stage.stageHeight / 2) + 100;
            
            // 円作成
            this.circle = createCircle();
            this.circle.x = stage.stageWidth / 2;
            this.circle.y = stage.stageHeight / 2;
            
            // インスタンスに色々と設定
            this.buttonMode = true;
            
            this.graphics.beginFill(0xAAAFFF);
            this.graphics.drawRect(30, 200, 150, 170);
            this.graphics.endFill();
            this.opaqueBackground = 0xCFCFCF;           
            this.addEventListener(MouseEvent.CLICK, onClick);
            
            // テキストフィールド作成
            var tf:TextField = new TextField();
            
            tf.autoSize = "left";
            tf.appendText("this.parent : " + this.parent + "\n");
            tf.appendText("this.visible : " + this.visible + "\n");
            tf.appendText("this.alpha : " + this.alpha + "\n");
            tf.appendText("this.mouseEnabled : " + this.mouseEnabled + "\n");
            tf.appendText("this.getRect(this) : " + this.getRect(this) + "\n");
            tf.appendText("this.opaqueBackground : " + this.opaqueBackground + "\n");
            tf.appendText("this.buttonMode : " + this.buttonMode);
            
            addChild(bt);
            addChild(tf);
            addChild(this.circle);

        }
        
        
        private function createSprite():Sprite {
            var bt:Sprite = new Sprite();
            
            bt.graphics.beginFill(0xCCCCCC);
            bt.graphics.lineStyle(0, 0x000000);
            bt.graphics.drawRect(0, 0, 100, 20);
            bt.graphics.endFill();
            return bt;
        }
        
        
        private function createCircle():Shape {
            var sp:Shape = new Shape();
            
            sp.graphics.beginFill(0x99CCFF);
            sp.graphics.lineStyle(0, 0x000000);
            sp.graphics.drawCircle(0, 0, 50);
            sp.graphics.endFill();

            return sp;
        }

        public function onClick(event:MouseEvent):void {
            
            if (this.circle_state) {
                this.circle.alpha = 0.2;
                circle_state = false;
            } else {
                this.circle.alpha = 1;
                circle_state = true;
            }
            
        }

    }
}

ActionScript 3.0 アニメーション

著者 : Keith Peters / 金額 : ¥ 7,350

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

トラックバック

  • http://www.kantenna.com/cgi-bin/mt/mt-tb.cgi/478
[情報備忘録]2010年02月16日 02:36
ActionScript3.0 コメントとかコーディング規約とか
ActionScript3.0 コメントとかコーディング規約とか
[WEBプログラム覚書]2010年02月16日 03:55
ActionScript3.0 [練習] XMLとナビゲーション
ActionScript3.0を利用してXMLを読み込んでダサいナビゲーションを作ってみる
[WEBプログラム覚書]2010年02月24日 05:02
ActionScript3.0 [基礎] Tweenとeasing
ActionScript3.0で最も簡単にアニメーションをさせることができるTweenと加速と減速が簡単に設定できるeasingを使ってみる。
コメント (0)
コメントを投稿

(いままで、ここでコメントしたことがないときは、コメントを表示する前にこのブログのオーナーの承認が必要になることがあります。承認されるまではコメントは表示されません。そのときはしばらく待ってください。)





この情報を登録しますか?


先月アクセスが多かったページ