创建简单按钮2

package
{
	import flash.display.Sprite;
	
	public class SimpleButtonDemo extends Sprite
	{
		public function SimpleButtonDemo()
		{
			//创建三个不同文字不同大小和位置的矩形按钮
			var button1:RectangleButton = new RectangleButton("Button1",60,100);
			button1.x = 20;
			button1.y = 20;
			var button2:RectangleButton = new RectangleButton("Button2",80,30);
			button1.x = 40;
			button1.y = 20;
			var button3:RectangleButton = new RectangleButton("Button3",100,40);
			button1.x = 100;
			button1.y = 60;
			addChild(button1);
			
			addChild(button3);
			addChild(button2);
		}

	}
}
 
 
package
{
	import flash.display.Shape;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.filters.DropShadowFilter;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;

	public class RectangleButton extends SimpleButton
	{
		//显示在按钮上的文本
		private var _text:String;
		//保存矩形的宽度和高度
		private var _width:Number;
		private var _height:Number;
		public function RectangleButton(text:String,width:Number,height:Number)
		{
			_text = text;
			_width = width;
			_height = height;
			//创建按钮状态
			upState = createUpState();
			overState = createOverState();
			downState = createDownState();
			hitTestState = upState;
		}
		//创建状态对象
		private function createUpState():Sprite{
			var sprite:Sprite = new Sprite();
			var background:Shape = createdColoredRectangle(0x33FF66);
			var textField:TextField = createTextField(false);
			sprite.addChild(background);
			sprite.addChild(textField);
			return sprite;
		}
		private function createOverState():Sprite{
			var sprite:Sprite = new Sprite();
			var background:Shape = createdColoredRectangle(0x70FF94);
			var textField:TextField = createTextField(false);
			sprite.addChild(background);
			sprite.addChild(textField);
			return sprite;
		}
		private function createDownState():Sprite{
			var sprite:Sprite = new Sprite();
			var background:Shape = createdColoredRectangle(0xCCCCCC);
			var textField:TextField = createTextField(true);
			sprite.addChild(background);
			sprite.addChild(textField);
			return sprite;
		}
		private function createdColoredRectangle(color:uint):Shape{
			var rect:Shape = new Shape();
			rect.graphics.lineStyle(1,0x000000);
			rect.graphics.beginFill(color);
			rect.graphics.drawRoundRect(0,0,_width,_height,15);
			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;
		}
		
		
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值