/ Published in: ActionScript 3
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
package { import flash.display.*; import flash.events.*; import flash.text.*; public class CheckBox extends Sprite { private var _label:FormattedTextField; private var _icon:CheckBoxIcon; private var _isChecked:Boolean = false; public function CheckBox( name:String, value:* ) { build( ); setupEventListeners( ); layout( ); } private function build():void { _icon = new CheckBoxIcon( ); _label = new TextField( ); _label.autoSize = TextFieldAutoSize.LEFT; _label.text = value.label; _label.selectable = false; addChild( _icon ); addChild( _label ); } private function layout():void { _label.x = _icon.x + _icon.width + 5; _label.y = _icon.x + (_icon.height - _label.height) * 0.5; } private function setupEventListeners():void { addEventListener( MouseEvent.CLICK , clickListener ); } public function set selected( pValue:Boolean ):void { _isChecked = pValue; } public function get selected():Boolean { return _isChecked; } private function clickListener(e:MouseEvent):void { if(selected) { _icon.uncheck( ); selected = false; } else { _icon.check( ); selected = true; } } }