An easy and quick to program multi-purpose button is very helpful especially when prototyping an application and time is of the essence. The Button and accompanying ButtonClickedEvent class are just the right solution here. These two classes can be programmed as standalone, toggle or menu functions with very little effort.
In the first example below, code is implemented to function as a simple button. The 2 parameters supplied include the button id and the button caption. In this application, the button id is not used by the so that its value is arbitrary.
// import custom classes import com.dtk.Button; import com.dtk.ButtonClickedEvent; // instance of Button class with event listener enabled var sb:Button = new Button( 0, 'Clear' ); sb.x = 10; sb.y = 50; sb.addEventListener( ButtonClickedEvent.BUTTON_CLICKED, turnOff ); addChild( sb ); // function called by event listener when button is pressed function turnOff( e:ButtonClickedEvent ):void { // set rectangle alpha to 0 getChildByName( 'rec' ).alpha = 0; } // draw rectangle var rc:Sprite = new Sprite( ); rc.graphics.beginFill( bgColor ); rc.graphics.drawRect( xPos, yPos, 18, 25 ); rc.graphics.endFill( ); rc.name = 'rec'; addChild( rc ); |
In this next example, the variable btnID is set to 0 and passed to the class constructor. Each time the button is pressed the variable is incremented and the modulus function is used to determine whether the number is odd or even. A ColorTransform is then employed to toggle the color of the rectangle.
import com.dtk.Button; import com.dtk.ButtonClickedEvent; // init var passed to Button class var btnID:uint = 0; var tg:Button = new Button( btnID, 'Toggle' ); tg.x = 10; tg.y = 50; tg.addEventListener( ButtonClickedEvent.BUTTON_CLICKED, toggleColor ); addChild( tg ); // toggle rectangle color function toggleColor( e:ButtonClickedEvent ):void { // increment variable btnID btnID++; // init var defining color to be transformed to var rColor:uint; // modulus function returns either 0 or 1 if ( btnID % 2 == 0 ) // even number { rColor = 0xFF0000; } else // odd number { rColor = 0x00FF00; } // color transform of rectangle var c:ColorTransform = getChildByName( 'rec' ).transform.colorTransform; c.color = rColor; getChildByName( 'rec' ).transform.colorTransform = c; } var rc:Sprite = new Sprite( ); rc.graphics.beginFill( 0xFF0000 ); rc.graphics.drawRect( 80, 50, 18, 25 ); rc.graphics.endFill( ); rc.name = 'rec'; addChild( rc ); |
In the final example below, a simple menu function is programmed. Each instance of the Button class and its matching rectangle are identified by the variable i; in the case of the Button class, the value is dispatched to the function called by the event listener and in the case of the rectangle the value is used to name the instance. A switch statement is then used to turn on the rectangle matching the selected menu tab.
import com.dtk.Button; import com.dtk.ButtonClickedEvent; // array of menu tab captions var titles:Array = [ 'Menu 1', 'Menu 2', 'Menu 3' ]; // starting y postion var yPos:uint = 50; // loop through titles array for ( var i:uint=0; i<titles.length; i++ ) { // use i as parameter btnID and to determine caption to pass to instance of Button var m:Button = new Button( i, titles[i] ); m.x = 10; m.y = yPos; m.addEventListener( ButtonClickedEvent.BUTTON_CLICKED, onMenuClicked ); addChild( m ); var r:Sprite = new Sprite( ); r.graphics.beginFill( 0x00FF00 ); r.graphics.drawRect( 80, yPos, 18, 25 ); r.graphics.endFill( ); // if not first menu tab, hide rectangle if ( i != 0 ) r.alpha = 0; r.name = 'rect_' + i; addChild( r ); // increment vertical position of botton, rectangle yPos += 34; } function onMenuClicked( e:ButtonClickedEvent ):void { // first, set all rectangles to 0 alpha getChildByName( 'rect_0' ).alpha = 0; getChildByName( 'rect_1' ).alpha = 0; getChildByName( 'rect_2' ).alpha = 0; // set selected rectangle to 1 alpha switch ( e.clicked ) { case 0: getChildByName( 'rect_0' ).alpha = 1; break; case 1: getChildByName( 'rect_1' ).alpha = 1; break; case 2: getChildByName( 'rect_2' ).alpha = 1; break; } } |
the Button and ButtonClickedEvent classes are included in the download. Download files: Button.zip (63)
import com.dtk.HTMLText;
You has forgotten include this class.