開発環境 Apache Flex SDK 4.12.1
FlashDevelop 4.6.2.5
実行環境 Microsoft Windows 8.1 (64bit)
プロジェクトの種類 ActionScript 3/AS3 Project
プロジェクト名 ButtonTest


Project/Properties
Output
Background color #6495ed

Main.as
package 
{
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.text.TextField;
    import flash.text.TextFormat;
 
    public class Main extends Sprite 
    {
        private var btnArray:Vector.<TextField>;
        private var btnSelect:int;
 
        public function Main():void 
        {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);
        }
 
        private function init(e:Event = null):void 
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);
            // entry point
 
            btnArray = new Vector.<TextField>;
            createButton(10, 10, 200, 25, "こんにちは");
            createButton(10, 45, 200, 25, "世界");
 
            selectButton(0);
        }
 
        private function createButton(x:Number, y:Number, width:Number, height:Number, text:String):void
        {
            var tf:TextField = new TextField;
            tf.background = true;
            tf.border = true;
            tf.defaultTextFormat = new TextFormat(null, 20);
            tf.name = btnArray.length.toString();
            tf.selectable = false;
            tf.x = x;
            tf.y = y;
            tf.width = width;
            tf.height = height;
            tf.text = text;
            tf.addEventListener(MouseEvent.CLICK, onButtonClick);
            addChild(tf);
            btnArray.push(tf);
        }
 
        private function onButtonClick(e:MouseEvent):void 
        {
            var select:int = parseInt((e.target as TextField).name);
            selectButton(select);
        }
 
        private function selectButton(select:int):void 
        {
            for (var i:int = 0; i < btnArray.length; i++) 
            {
                var tf:TextField = btnArray[i];
                tf.backgroundColor = (i == select) ? 0xffff00 : 0x7f7f7f;
            }
            btnSelect = select;
        }
 
    }
 
}
 
最終更新:2014年08月15日 21:43