extremelly basic pressable buttons

by www0z0k
♥0 | Line 56 | Modified 2016-07-06 17:13:52 | MIT License
play

ActionScript3 source code

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

package {
    import flash.text.TextField;
    import flash.display.Sprite;
    public class FlashTest extends Sprite {
        private const LETTERS:Array = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
        private var _result:TextField = new TextField();
        public function FlashTest() {
            var currX:int = 0;
            var currY:int = 0;
            var maxW:int = 200;
            for (var i:int = 0; i < LETTERS.length; i++) {
                addChild(new TextBtn(LETTERS[i], tracer, currX, currY));
                if(currX + 20 >= maxW){
                    currX = 0;
                    currY += 30;
                }else{
                    currX += 20;
                }
            }
            _result.y = 100;
            _result.autoSize = 'left';
            addChild(_result);
        }
        
        private function tracer(str:String):void {
            _result.text = str + ' pressed';
        }
    }
}
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.text.TextField;
    class TextBtn extends Sprite{
        private var _letter:String;
        private var _tf:TextField = new TextField();
        private var _cb:Function;
        public function TextBtn(letter:String, cb:Function, _x:int = 0, _y:int = 0){            
            _tf.selectable = false;
            _tf.background = true;
            _tf.backgroundColor = 0xc0c0c0;
            _letter = letter;
            _cb = cb;
            _tf.text = letter;
            _tf.width = 15;
            _tf.height = 22;
            
            addEventListener(MouseEvent.CLICK, handleClick);
            addChild(_tf);            
            x = _x;
            y = _y;
        }   
        private function handleClick(e:Event):void {
            _cb(_letter);
            _tf.backgroundColor = 0x818181;
            removeEventListener(MouseEvent.CLICK, handleClick);
        }
    }