要使用对话框的时候可能会用flash自带的组件,不过有时候只想做个简单的效果,并且使用自己设计的样式,使用组件再更改skin未必太麻烦
这时候就想到自己做个mc代替,当需要对话框出现后背后的所有按钮不能点击时 通常会画一个很大的mc然后给个空的onRelease
如果用as动态创建的话那就更加灵活了,今天尝试写了一个,其中用到了DepthManager 类中的一些方法,通过阅读DepthManager 类的代码后会发现其中有好几个有用的函数,可以很方便地进行深度管理。
自制modal window参考文章 我是通过阅读这篇文章来学习的,强烈建议一看
Ex.
[kml_flashembed movie="http://harryxu.cn/down/modalWin.swf" height="200" width="200" /]
下载
这个例子中我将创建背景mc的方法写到了一个class中
[as]
import mx.managers.DepthManager;
class ModalWin extends MovieClip
{
private var modal_mc : MovieClip;
private var parent_mc : MovieClip;
private var dm : DepthManager;
function ModalWin()
{
parent_mc = this._parent;
modal_mc = parent_mc.createEmptyMovieClip("ModalWinClass_mc"+getTimer(), parent_mc.findNextAvailableDepth(0,parent_mc.buildDepthTable(),"up"));
modal_mc.setDepthBelow(this);
modal_mc.onRelease = undefined;
modal_mc.useHandCursor = false;
}
/**
*
* @param col 背景颜色[默认白色]
* @param alp 背景透明度[默认50]
**/
function createModal( col:Number, alp:Number) :Void
{
var _alp:Number = alp ? alp : 50;
var _col:Number = col ? col : 0xFFFFFF;
drawRect(_alp, _col);
}
function del() :Void
{
modal_mc.swapDepths(parent_mc.findNextAvailableDepth(0,parent_mc.buildDepthTable(),"up"));
modal_mc.removeMovieClip();
this.swapDepths(parent_mc.findNextAvailableDepth(0,parent_mc.buildDepthTable(),"up"));
this.removeMovieClip();
}
/**
* setter
* set _x, _y, _width, _height
*/
function set modal_x(n:Number) :Void
{
modal_mc._x = n;
}
function set modal_y(n:Number) :Void
{
modal_mc._y = n;
}
function set modal_width(n:Number) :Void
{
modal_mc._width = n;
}
function set modal_height(n:Number) :Void
{
modal_mc._height = n;
}
private function drawRect(alp:Number, col:Number) :Void
{
with(modal_mc)
{
clear();
beginFill(col, alp);
moveTo(0,0);
lineTo(Stage.width, 0);
lineTo(Stage.width, Stage.height);
lineTo(0, Stage.height);
lineTo(0, 0);
endFill();
}
}
}
[/as]