We Recommend

Essential ActionScript 3.0 Essential ActionScript 3.0
The book focuses on the core language and object-oriented programming, but also adds a deep look at the centerpiece of Flash Player's new API: display programming. Enjoy hundreds of brand new pages covering exciting new language features, such as the DOM-based event architecture, E4X, and namespaces--all brimming with real-world sample code.


Posted By

sine on 06/20/08


Tagged


Versions (?)


Who likes this?

2 people have marked this snippet as a favorite

visuallyspun
taboularasa


rect button


Published in: ActionScript 3 


URL: http://safari.oreilly.com:80/0596526954/ID-I_0596526954_CHP_6_SECT_6

  1. package {
  2. import flash.display.*
  3. import flash.text.*;
  4. import flash.filters.DropShadowFilter;
  5.  
  6. public class RectangleButton extends SimpleButton {
  7. // The text to appear on the button
  8. private var _text:String;
  9. // Save the width and height of the rectangle
  10. private var _width:Number;
  11. private var _height:Number;
  12.  
  13. public function RectangleButton( text:String, width:Number, height:Number ) {
  14. // Save the values to use them to create the button states
  15. _text = text;
  16. _width = width;
  17. _height = height;
  18.  
  19. // Create the button states based on width, height, and text value
  20. upState = createUpState( );
  21. overState = createOverState( );
  22. downState = createDownState( );
  23. hitTestState = upState;
  24. }
  25.  
  26. // Create the display object for the button's up state
  27. private function createUpState( ):Sprite {
  28. var sprite:Sprite = new Sprite( );
  29.  
  30. var background:Shape = createdColoredRectangle( 0x33FF66 );
  31. var textField:TextField = createTextField( false );
  32.  
  33. sprite.addChild( background );
  34. sprite.addChild( textField );
  35.  
  36. return sprite;
  37. }
  38.  
  39. // Create the display object for the button's up state
  40. private function createOverState( ):Sprite {
  41. var sprite:Sprite = new Sprite( );
  42.  
  43. var background:Shape = createdColoredRectangle( 0x70FF94 );
  44. var textField:TextField = createTextField( false );
  45.  
  46. sprite.addChild( background );
  47. sprite.addChild( textField );
  48.  
  49. return sprite;
  50. }
  51.  
  52. // Create the display object for the button's down state
  53. private function createDownState( ):Sprite {
  54. var sprite:Sprite = new Sprite( );
  55.  
  56. var background:Shape = createdColoredRectangle( 0xCCCCCC );
  57. var textField:TextField = createTextField( true );
  58.  
  59. sprite.addChild( background );
  60. sprite.addChild( textField );
  61.  
  62. return sprite;
  63. }
  64.  
  65. // Create a rounded rectangle with a specific fill color
  66. private function createdColoredRectangle( color:uint ):Shape {
  67. var rect:Shape = new Shape( );
  68. rect.graphics.lineStyle( 1, 0x000000 );
  69. rect.graphics.beginFill( color );
  70. rect.graphics.drawRoundRect( 0, 0, _width, _height, 15 );
  71. rect.graphics.endFill( );
  72. rect.filters = [ new DropShadowFilter( 2 ) ];
  73. return rect;
  74. }
  75.  
  76. // Create the text field to display the text of the button
  77. private function createTextField( downState:Boolean ):TextField {
  78. var textField:TextField = new TextField( );
  79. textField.text = _text;
  80. textField.width = _width;
  81.  
  82. // Center the text horizontally
  83. var format:TextFormat = new TextFormat( );
  84. format.align = TextFormatAlign.CENTER;
  85. textField.setTextFormat( format );
  86.  
  87. // Center the text vertically
  88. textField.y = ( _height - textField.textHeight ) / 2;
  89. textField.y -= 2; // Subtract 2 pixels to adjust for offset
  90.  
  91. // The down state places the text down and to the right
  92. // further than the other states
  93. if ( downState ) {
  94. textField.x = 1;
  95. textField.y = 1;
  96. }
  97.  
  98. return textField;
  99. }
  100. }
  101. }

Report this snippet 

You need to login to post a comment.