画像のボタン

画像ファイルをクリックするとイベントが実行されるボタンを作って見る。

package {
    import flash.display.*;
    import flash.events.*;
    import flash.system.*;
    import flash.text.*;
        
	public class MyInit{

        public function MyInit(){
	}
        public static function addButtonImg(doc:DisplayObjectContainer,
            image:Bitmap,handler:Function,x:int,y:int):Sprite {
            var button:Sprite = new Sprite;
            button.addChild(image);
            doc.addChild(button);
            button.addEventListener(MouseEvent.CLICK,handler);
            button.x=x;
            button.y=y;
            return button;
        }
        
	}
}	

使用方法

クラス定義で画像を読み込み

	[Embed(source='button.png')] private var Img_button : Class;

コンストラクタで追加

    	MyInit.addButtonImg(stage,new Img_button(),testEvent,300,20);

イベントを定義

        private function testEvent(evt:MouseEvent):void {
             trace("Push Button!!");
        }