演示效果:
//-------------------------------------------------------------------
//as代码如下;被遮罩图片mc名称为img-mc;
//特点自动适应图片大小,自动判断子遮罩个数,可随意修改子遮照大小;
//-------------------------------------------------------------------
//
Stage.showMenu = false;
Stage.scaleMode = "noScale";
fscommand(allowscale, true);
createEmptyMovieClip("mask", -1);
mask.createEmptyMovieClip("mask0", -2);
with (mask.mask0) {
beginFill(0xcccccc, 100);
moveTo(0, 0);
lineTo(0, 100);
lineTo(100, 100);
lineTo(100, 0);
lineTo(0, 0);
endFill();
_width = 1;
_height = 1;
}
//以上为创建mask元件和mask0子元件,这两个元件就是用于遮罩图片的。
mask._x = img_mc._x=0;
mask._y = img_mc._y=0;
img_mc.setMask(mask);
//以上为设置遮罩和遮罩层的位置;
maskNum = 0;
//初始化遮罩子元素的个数;
box = 40;
//初始化子遮罩正方形的边长;
lNum = Math.ceil(img_mc._width/box)+1;
//获得被遮罩图片的一行需要多少个子遮罩;
cNum = Math.ceil(img_mc._height/box)+1;
//获得被遮罩图片的一列需要多少个子遮罩;
maxNum = lNum*cNum;
//得到子遮罩元件的总数;
var myArray = new Array();
//定义数组;
for (i=0; i<cNum; i++) {
myArray[i] = [];
for (j=0; j<lNum; j++) {
myArray[i].push(i*lNum+j+1);
//该两个循环生成一个二维数组用于存储子遮罩的行和列的编号。
}
}
for (i=0; i<cNum; i++) {
if (i%2 == 0) {
myArray[i];
} else {
myArray[i] = myArray[i].sort(order);
//当遇到奇数行的时候,倒排子遮罩的编号;该方法是为了让子遮罩从右到左产生;
}
for (j=0; j<lNum; j++) {
with (mask) {
masks = mask0.duplicateMovieClip("mask"+(myArray[i][j]), myArray[i][j]);
//根据元件mask0复制出子遮罩,并将名称存入masks数组;
masks._x = j*(box-1);
//设置单个子遮罩的x和y坐标值,将其平铺在整个被遮罩图片上;
masks._y = i*(box-1);
//
masks.xs = i;
//设置自遮罩在二为数组myArray中的编号;
masks.ys = j;
masks._width = 0;
//初始化子遮罩不可见;
masks._height = 0;
//
masks.onEnterFrame = function() {
if (_root.myArray[this.xs][this.ys] == _root.maskNum) {
this.mcZoom();
//执行子遮罩缩放动画;
}
};
}
}
}
_root.onEnterFrame = function() {
if (maskNum<=maxNum) {
_root.maskNum++;
//递加子遮罩个数;
}
};
function order(a, b) {
//该函数用于降幂排序数组;
if (a>b) {
return -1;
} else if (a<b) {
return 1;
} else {
return 0;
}
}
MovieClip.prototype.mcZoom = function() {
//子遮罩动画部分;
this.onEnterFrame = function() {
this._width += (box-this._width)/10;
this._height += (box-this._height)/10;
};
};