[Flash][AS3]分享一下很好用的Flash弹窗类

没有找到原文链接只有一个下载链接,如有冒犯,请联系我删除

https://download.csdn.net/download/hfangyu/3699219

自己加了一个小函数,包括BUG的解决方案和使用方法都在下面

//文件名Alert.as
//@@@@使用时请将此文件和fla文件放置同一目录
//@@@静态函数调用方法 
//@@1.在主舞台初始化    Alert.init(stage);(一般在构造函数中)
//@@2.在主舞台显示信息  Alert.show("你的信息!");
//@@3.设置提示框的标题  Alert.SetTitle("Title");  
//@@@@@想要在某帧的动作里使用,首先需要在整个舞台的类里初始化,然后直接在帧里面使用就行
package 
{
	//这部分可能会报错,因为这个是个组件,不是类
	//解决方案,从组件对话框中找到 User Inerface中的button
	//button拖动到舞台,编译,发布,成功后删除button(或者直接隐藏button就好)
    //其实只是让这个组件出现在库中,编译时as自动连接库里的组件
	import fl.controls.Button;
	
	import flash.display.Graphics;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.MouseEvent;
	import flash.filters.DropShadowFilter;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	/**
	 * ALert信息提示类<br>使用时先调用init()方法初始化舞台对象,然后调用show()方法传入需要提示的信息
	 * @author hfy 2011-10-19
	 */
	public class Alert {
		//舞台对象
		private static var stage:Stage;
		//标题
		private static var TipsTitle:String="Tips";
		/**
		 * 第一次使用Alert类的时候,调用该方法,为Alert初始化舞台对象
		 * @param stageReference 舞台对象
		 */ 
		public static function init(stageReference:Stage):void {
			stage = stageReference;
		}
		//@@自己加的 暂不支持中文输入
		public static function SetTitle(text:String):void {
			for(var i:uint=0;i<text.length;i++){
				//大于255则是汉字,汉字占两个字符
				if(text.charCodeAt(i)>255){
					return;
				}
			}
		    TipsTitle=text;
		}
		/**
		 * 调用该方法使用Alert
		 * @param text 需要显示的提示信息
		 */
		public static function show(text:String):void {
			//如果舞台为空,那么退出Alert,不做任何操作
			if (stage == null) return;
			//如果text为null,退出Alert
			if(text==null) return;
			
			//定义ALert的sprite面板
			var myAlert:Sprite = new Sprite();
			//添加遮罩层
			myAlert.addChild(createMask());
			//添加信息提示框主体
			myAlert.addChild(createPrompt(text));
			//为alert面板注册事件
			assignListeners(myAlert,null);
			//加载显示
			stage.addChild(myAlert)
		}
		
		/**
		 * 为Alert注册事件
		 */
		private static function assignListeners(myAlert:Sprite, callback:Function):void {
			var Prompt:Sprite = myAlert.getChildAt(1) as  Sprite;
			var OKbutton:* = Prompt.getChildByName("sure");
			// Cause Alert to call specified function when alert has been closed
			if (callback != null) {
				function myFunction(myEvent:MouseEvent):void {
					OKbutton.removeEventListener(MouseEvent.CLICK, myFunction);
					closeAlert(myEvent);
					callback();
				};
				OKbutton.addEventListener(MouseEvent.CLICK, myFunction);
			} else {
				OKbutton.addEventListener(MouseEvent.CLICK, closeAlert);
			}
		}
		
		/**
		 * 关闭Alert的提示框
		 */
		private static function closeAlert(event:MouseEvent):void {
			var button:* = event.target;
			button.removeEventListener(MouseEvent.CLICK, closeAlert);
			var prompt:* = event.target.parent;
			prompt.removeEventListener(MouseEvent.CLICK, doStartDrag);
			prompt.removeEventListener(MouseEvent.CLICK, doStopDrag);

			var myAlert:Sprite = event.target.parent.parent;
			stage.removeChild(myAlert);
		}
		
		/**
		 * Creates the mask for the Alert
		 */ 
		private static function createMask():Sprite {
			var mask:Sprite=new Sprite;
			mask.graphics.lineStyle(2,0x0099ff);
			mask.graphics.beginFill(0xffffff);
			mask.graphics.drawRoundRect(0,0,stage.width,stage.height,5);
			mask.graphics.endFill();
			mask.alpha=0.6;
			return mask;
		}
		
		/**
		 * 创建一个提示框sprite位于舞台中央。并显示提示信息和确定按钮。
		 * @param text 提示信息
		 */ 
		private static function createPrompt(text:String):Sprite {
			var holder:Sprite = new Sprite;
			holder.name="holder";
			//注册提示框的拖拽事件
			holder.addEventListener(MouseEvent.MOUSE_DOWN, doStartDrag);
			holder.addEventListener(MouseEvent.MOUSE_UP, doStopDrag); 
			
			//创建提示信息文本框和确定按钮
			var textField:TextField=getTextField(text);
			var button:Button=getSureButton();
			
			//创建提示框holder的graphice样式
			GraphicePrompt(holder,textField);
			
			//设置文本框和按钮的坐标位置,并加载到显示界面
			textField.x=(holder.width/2)-(textField.width/2);
			textField.y=26;
			button.x=(holder.width/2)-(button.width/2);
			button.y=holder.height-27;
			holder.addChild(textField);
			holder.addChild(button);
			
			//设置holder的显示坐标位置
			holder.x = (stage.stageWidth/2)-(holder.width/2);
			holder.y = (stage.stageHeight/2)-(holder.height/2);
			return holder;
		}
		
		/**
		 * 绘制信息提示框主体的显示样式
		 * @param holder 提示框的主体对象
		 * @param text 提示信息文本框
		 */
		private static function GraphicePrompt(holder:Sprite,text:TextField):void {
			var myWidth:int = text.width+50;
			var myHeight:int = text.height+60;
			
			if (myWidth < 150) {
				myWidth = 150;
			}
			if (myHeight < 80) {
				myHeight = 80;
			}
			if (myHeight > stage.stageHeight) {
				myHeight = stage.stageHeight - 20;
				text.autoSize = TextFieldAutoSize.NONE;
				text.height = stage.stageHeight-80;
			}
			
			//holder.graphics.lineStyle(1,0,0);
			holder.graphics.beginFill(0xffffff);
			holder.graphics.drawRoundRect(0,0,myWidth,myHeight,20);
			holder.graphics.endFill();
			
			var shap1:Shape=new Shape;
			var shap2:Shape=new Shape;
			shap1.graphics.beginFill(0x768f9a);
			shap1.graphics.drawRoundRectComplex(0,0,myWidth,20,10,10,0,0);
			shap1.graphics.endFill();
			shap2.graphics.beginFill(0x899fa9);
			shap2.graphics.drawRoundRectComplex(0,21,myWidth,myHeight-21,0,0,10,10);
			shap2.graphics.endFill();
			holder.addChild(shap1);
			holder.addChild(shap2);
			
			var tip:TextField=getTextField(TipsTitle,true);
			tip.x=5;
			tip.y=0;
			holder.addChild(tip);
			
			//设置阴影效果
			var shadow:DropShadowFilter = new DropShadowFilter; 
			shadow.distance = 5; 
			shadow.alpha=0.5;
			holder.filters = [shadow];
		}
		
		/**
		 * 生成一个textfield文本框
		 * @param text 提示信息
		 */
		private static function getTextField(Text:String,bold:Boolean=false):TextField {
			var myTextField:TextField = new TextField();
			//计算文本字符的长度
			var length:Number=CalLength(Text);
			if(length>300){
				myTextField.width=300;
			}
			else{
				myTextField.width=length;
			}
			
			//字体显示为白色
			myTextField.textColor = 0xffffff;
			myTextField.name = "textfield";
			//允许多行显示
			myTextField.multiline = true;
			myTextField.wordWrap=true;
			myTextField.selectable = false;
			myTextField.autoSize = TextFieldAutoSize.CENTER;
			
			var format:TextFormat=new TextFormat;
			//format.font="幼圆";
			if (bold) {
				format.bold=true;
			}
			myTextField.defaultTextFormat=format;
			myTextField.text=Text;
			
			return myTextField;
		}
		
		/**
		 * 创建一个button按钮
		 */
		private static function getSureButton():Button{
			var button:Button=new Button;
			button.name="sure";
			button.label="确定";
			button.width=40;
			button.height=20;
			button.textField.height=20;
			return button;
		}
		
		/**
		 * 计算文本字符的长度
		 * @param str 待计算长度的文本字符
		 */
		private static function CalLength(str:String):Number{
			var charNum:uint=0;
			for(var i:uint=0;i<str.length;i++){
				//大于255则是汉字,汉字占两个字符
				if(str.charCodeAt(i)>255){
					charNum+=2;
				}
				else{
					charNum++;
				}
			}
			//返回width
			return charNum*6+5;
		}
		
		/**
		 * 鼠标拖拽事件
		 */
		private static function doStartDrag(event:MouseEvent):void {
			if(event.target.name=="holder"){
				event.target.startDrag();
			}
		}
		private static function doStopDrag(event:MouseEvent):void {
			if(event.target.name=="holder"){
				event.target.stopDrag();
			}
		}
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值