ButtonTest02: ボタン作成機能をオブジェクトにまとめる

by siouxcitizen forked from ButtonTest01: はじめてのボタン作成 (diff: 61)
参考サイト
↓ん・ぱか工房 ActionScripts 3.0メモ シンプルボタンの利用
http://www.saturn.dti.ne.jp/~npaka/flash/as30/SimpleButton/index.html
♥0 | Line 54 | Modified 2009-07-29 23:26:34 | MIT License
play

ActionScript3 source code

/**
 * Copyright siouxcitizen ( http://wonderfl.net/user/siouxcitizen )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/jfsI
 */

// forked from hacker_7daxapax's ButtonTest01
//参考サイト
//↓ん・ぱか工房 ActionScripts 3.0メモ シンプルボタンの利用
//http://www.saturn.dti.ne.jp/~npaka/flash/as30/SimpleButton/index.html

package {
    import flash.display.*;
    import flash.events.*;
    import flash.text.*;

    public class ButtonTest extends Sprite {
        
        private var button : SimpleButton;//ボタン 
        private var label : TextField;    //ラベル
        
        public function ButtonTest() {
            label = new TextField;
            label.text = "ButtonTest!";
            addChild(label);
            
            button = new CustomButton("button");
            button.y = 20;

            button.addEventListener(MouseEvent.MOUSE_DOWN,onMouseDown);
            button.addEventListener(MouseEvent.MOUSE_UP,onMouseUp);
            addChild(button);
        }
   
        //マウスダウンイベントの処理
        private function onMouseDown(evt:MouseEvent):void {
            label.text = "Mouse Down";
        }

        //マウスアップイベントの処理
        private function onMouseUp(evt:MouseEvent):void {
            label.text = "Mouse Up";
        }        
    }
}

import flash.display.*;
import flash.system.*;
import flash.text.*;

//カスタムボタン
class CustomButton extends SimpleButton { 
        //コンストラクタ    
        public function CustomButton(label:String="") {
            //状態
            upState = makeSprite(label,0x00FF00);
            overState = upState;
            downState = makeSprite(label,0xFF0000);
            hitTestState = upState;
        }

        //ボタン用スプライト作成
        private function makeSprite(text:String,color:uint):Sprite{
            //ボタン用ラベル作成
            var label : TextField = new TextField();
            label.text = text;
            label.autoSize = TextFieldAutoSize.CENTER;
            label.selectable = false;

            //ボタン用スプライト作成
            var sp:Sprite = new Sprite();
            sp.graphics.beginFill(color);
            sp.graphics.drawRoundRect(0, 0, 100, 20, 15);
            sp.graphics.endFill();
            sp.alpha = 0.8;            
            sp.addChild(label);
            
            //ラベル用フォーマット設定
            var format:TextFormat=new TextFormat();
            format.font = "Courier New";
            format.bold = true;
            format.size = 13;
            label.setTextFormat(format);

            return sp;
        }    
}

Forked