[AS3]as3中创建按钮代码示例
创建SimpleButton 类实例和创建upState,downState,overState和hitTestState等对象。当用户点击按钮时用click事件激活方法
创建SimpleButton 类实例和创建upState,downState,overState和hitTestState等对象。当用户点击按钮时用click事件激活方法
- package {
- import flash.display.Sprite;
- public class Sample1124 extends Sprite
- {
- public function Sample1124()
- {
- var button1:MyButton = new MyButton( "Button 1", 100, 20 );
- button1.x = 20;
- button1.y = 20;
- var button2:MyButton = new MyButton( "Button 2", 100, 30 );
- button2.x = 180;
- button2.y = 20;
- var button3:MyButton = new MyButton( "Button 3", 100, 40 );
- button3.x = 340;
- button3.y = 20;
- addChild( button1 );
- addChild( button2 );
- addChild( button3 );
- }
- }
- }
- import flash.display.SimpleButton;
- import flash.display.Sprite;
- import flash.display.Shape;
- import flash.filters.DropShadowFilter;
- import flash.text.TextField;
- import flash.text.TextFormat;
- import flash.text.TextFormatAlign;
- internal class MyButton extends SimpleButton
- {
- private var _text:String;
- private var _width:Number;
- private var _height:Number;
- public function MyButton(text:String,width:Number,height:Number)
- {
- this._text = text;
- this._width = width;
- this._height = height;
- this.upState = CreateUpState();
- this.downState = CreateDownState();
- this.overState = CreateOverState();
- this.hitTestState = upState;
- }
- private function CreateUpState():Sprite
- {
- var sprite:Sprite = new Sprite();
- var background:Shape = CreateColoredRectangle(0x33FF66);
- var text:TextField = CreateTextField(false);
- sprite.addChild(background);
- sprite.addChild(text);
- return sprite;
- }
- private function CreateDownState():Sprite
- {
- var sprite:Sprite = new Sprite();
- var background:Shape = CreateColoredRectangle(0xCCCCCC);
- var text:TextField = CreateTextField(,true);
- sprite.addChild(background);
- sprite.addChild(text);
- return sprite;
- }
- private function CreateOverState():Sprite
- {
- var sprite:Sprite = new Sprite();
- var background:Shape = CreateColoredRectangle(0x70FF94);
- var text:TextField = CreateTextField(false);
- sprite.addChild(background);
- sprite.addChild(text);
- return sprite;
- }
- private function CreateColoredRectangle(color:uint):Shape
- {
- var rect:Shape = new Shape();
- rect.graphics.lineStyle(1,0x000000);
- rect.graphics.beginFill(color);
- rect.graphics.drawRoundRect(0,0,_width,_height,10)//最后那个参数,值越大,越接近椭圆
- rect.graphics.endFill();
- rect.filters = [new DropShadowFilter(2)]//滤镜
- return rect;
- }
- private function CreateTextField(downState:Boolean):TextField
- {
- var textField:TextField = new TextField();
- textField.text = _text;
- textField.width = _width;
- var format:TextFormat = new TextFormat();
- format.align = TextFormatAlign.CENTER;
- textField.setTextFormat(format);
- //垂直居中
- textField.y = (_height - textField.textHeight)/2;
- textField.y -= 2;//Subtract 2 pixels to adjust for offset
- if(downState)
- {
- textField.x += 1;
- textField.y += 1;
- }
- return textField;
- }
- }
热门文章推荐
- [HLS]做自己的m3u8点播系统使用HTTP Live Streaming(HLS技术)
- [FMS]FMS流媒体服务器配置与使用相关的介绍
- [AS3]什么是M3U8,与HTML5的区别是什么
- AS2.0 让flash自适应全屏,并且不自动缩放
- [AS3]as3.0的sound类常用技巧整理
- [AS3]as3与ByteArray详解、ByteArray介绍、ByteArray用法
- 关于RTMP,RTMPT,RTMPS,RTMPE,RTMPTE协议的介绍
- [JS]分享浏览器弹出窗口不被拦截JS示例
请稍候...