AS3 Alert

借鉴了key以及jiajia的写法,具体代码如下:

package com.DNight 
{
import flash.display.Shape;
import flash.display.Sprite;
import flash.display.Stage;
import flash.text.TextFieldAutoSize;
import flash.display.MovieClip;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.events.MouseEvent;

/**
* ...
* @author DN
*/
public class AlertRect extends MovieClip
{

private var box:Sprite

private static var _alertTitle:String;

private static var _alertContent:String;

private static var _alertMode:String;

private static var _alertFun:Function;

/////////////////////////////////

private var window:AlertWindow

private var _titleText:TextField;

private var _txtFormat:TextFormat;

private var _contentText:TextField;

private var _enterBtn:EnterBtn;

private var _cancelBtn:CancelBtn;


public function AlertRect() 
{

box = new Sprite();

this.addChild(box);

   }

public static function show(_title:String, _content:String, _fun:Function, stage:Stage,_mode:String = "ok") {

_alertTitle = _title;

_alertContent = _content;

_alertMode = _mode;

_alertFun = _fun;

var alert:AlertRect = new AlertRect();

stage.addChild(alert);

alert.initApp(stage);

}
private function initApp(_stage:Stage) {

var bg:Shape = new Shape();

bg.graphics.beginFill(0x000000, 0.5);

bg.graphics.drawRect(0, 0, _stage.stageWidth, _stage.stageHeight);

bg.graphics.endFill();

box.addChild(bg);

window = new AlertWindow();

window.x = _stage.stageWidth / 2 - window.width / 2;

window.y = _stage.stageHeight / 2 - window.height / 2;

box.addChild(window);

_txtFormat = new TextFormat();

_txtFormat.size = 12;

_txtFormat.color = 0x000000;

_titleText = new TextField();

_titleText.autoSize = TextFieldAutoSize.LEFT;

_titleText.mouseEnabled = false;

_titleText.text = _alertTitle;

_titleText.x = 10;

_titleText.y = 3;

window.addChild(_titleText);

_titleText.defaultTextFormat = _txtFormat;

_contentText = new TextField();

_contentText.autoSize = TextFieldAutoSize.LEFT;

_contentText.mouseEnabled = false;

_contentText.text = _alertContent;

_contentText.x = 15;

_contentText.y = 26;

window.addChild(_contentText);

_contentText.defaultTextFormat = _txtFormat;

switch(_alertMode) {

case "ok":

initEnterBtn();

_enterBtn.x = window.width / 2 - _enterBtn.width / 2;

_enterBtn.y = 106

window.addChild(_enterBtn);

break

case "okAndCancel":

initEnterBtn();

initCancelBtn();

_enterBtn.x = 39;

_cancelBtn.x = 173;

_enterBtn.y = _cancelBtn.y = 106;

window.addChild(_enterBtn);

window.addChild(_cancelBtn);

break

}

}

private function initEnterBtn() {

_enterBtn = new EnterBtn();

_enterBtn.textField.text = "确定";

_enterBtn.textField.x = _enterBtn.width / 2 - _enterBtn.textField.width / 2;

_enterBtn.textField.y = _enterBtn.height / 2 - _enterBtn.textField.height / 2;

_enterBtn.addEventListener(MouseEvent.CLICK,onEnterClickHandle);

}

private function initCancelBtn() {

_cancelBtn = new CancelBtn();

_cancelBtn.textField.text = "取消";

_cancelBtn.textField.x = _enterBtn.width / 2 - _cancelBtn.textField.width / 2;

_cancelBtn.textField.y = _enterBtn.height / 2 - _cancelBtn.textField.height / 2;

_cancelBtn.addEventListener(MouseEvent.CLICK,onCancelClickHandle);

}

private function onEnterClickHandle(event:MouseEvent) {

if (_alertFun!=null) {

_alertFun();

}
parent.removeChild(this);

}

private function onCancelClickHandle(event:MouseEvent) {

parent.removeChild(this);
}

}

}

//////////////////////////////////////

使用:

package {
import com.DNight.AlertRect;
import com.DNight.AlertRectMode;
import flash.display.MovieClip
import com.DNight.ShortAlertRect;

public class Main extends MovieClip{

private var shortAlert:ShortAlertRect;

public function Main (){

initView();
}
private function initView() {

AlertRect.show("DNight", "HHHHH", moveHande, stage, AlertRectMode.OK_CANCEL);

}
private function moveHande() {

trace("move");

}
}
}

posted @ 2011-12-31 01:25  rob_2010  阅读(296)  评论(0编辑  收藏  举报