图片转场效果
这是我们经常会在浏览网页和论坛时看到的图片转场效果
制作这些效果我们很自然会想到用setMask函数来完成
在本节中我们将介绍七种基本转场效果
思路:
1.注意共三张图片,一张是底版(pic_old)放在最底层,一张是切换来的新图
(pic_new)放在中间层,最后还有一张用来做新图的遮罩层(mask);
2.新图和底版的深度不要颠倒,新图要比底版的深度高,否则会发生错误;
3.我们只在mask上面作文章,让mask去加载遮罩物,这样就可以完成许许多多不同的效果了。
步骤1:
(1)绘制一个正方形(50*50),注册点在中心,保存为影片剪辑
连接—>导出—>标志符 Rec;
(2)绘制一个圆形(50*50),注册点在中心,保存为影片剪辑
连接—>导出—>标志符 Cir;
(3)导入图片n张,均保存为影片剪辑,注册点在左上(0,0)
这些图片的连接—>导出—>标志符 分别从 pic1, pic2, pic3 ... picn
步骤2:
加入AS代码
var old_p:String;
//临时变量用于存储当前图片
var n:Number = 0;
//变量n为当前所在图片
var Num:Number = 5;
//Num图片数量
//=======================================================
//鼠标点击后设置底版图、新图和遮罩层,并调用(PassEffect)
_root.onMouseDown = function() {
n = n < Num ? ++n : 1 ;
_root.createEmptyMovieClip("pic_old", -3);
pic_old.attachMovie(old_p, old_p, 1);
old_p = "pic"+n;
_root.createEmptyMovieClip("pic_new", -2);
pic_new.attachMovie("pic"+n, pic, 1);
_root.createEmptyMovieClip("mask", -1);
pic_new[pic].setMask(mask);
//在测试时,可以把setMask这句注释掉,可更好地观察遮罩层的情况
PassEffect();
};
//============================================================
function PassEffect() {
switch (1+Math.floor(Math.random()*7)) {
//***************效果1[见附录]***************
//复制若干个圆,分布在每行每列,并使每个圆的大小不断增加至覆盖整张图
case 1 :
for (i=0; i<7; i++) {
for (j=0; j<6; j++) {
var p:MovieClip = mask.attachMovie("Cir", "Cir"+i*10+j,
i*10+j);
//注意这里是mask.attachMovie(),加载来的图形都属于遮罩层(mask)
p._width = 20;
p._height = 20;
p._x = 20+i*60;
p._y = 20+j*60;
p.onEnterFrame = function() {
if (this._width<180) {
this._width = this._height += 8;
} else {
delete this.onEnterFrame;
}
};
}
}
break;
//=================================================================
//***************效果2[见附录]***************
//复制一个正方形,放入舞台中心,设置他其初始大小为10*10,并放大
case 2 :
var p:MovieClip = mask.attachMovie("Rec", Rec, 1);
p._width = 10;
p._height = 10;
p._x = Stage.width/2;
p._y = Stage.height/2;
p.onEnterFrame = function() {
if (this._width
this._width += 40;
this._height += 40;
} else {
delete this.onEnterFrame;
}
};
break;
//=================================================================
//***************效果3[见附录]****************
//复制一个正方形,其初始大小比舞台的尺寸多一些,使其移动到舞台中心
case 3 :
var p:MovieClip = mask.attachMovie("Rec", "Rec"+1, 1);
p._width = Stage.width+10;
p._height = Stage.height+10;
p._x = 0-p._width;
p._y = 0-p._height;
p.onEnterFrame = function() {
this._x += (Stage.width/2-this._x)*0.3;
this._y += (Stage.height/2-this._y)*0.3;
};
break;
//=================================================================
//***************效果4[见附录]***************
//复制二个正方形,放到舞台外的左右各一个,使它们都向舞台中心移动
case 4 :
var p:MovieClip = mask.attachMovie("Rec", "Rec"+1, 1);
p._width = Stage.width;
p._height = Stage.height;
p._x = -p._width/2;
p._y = Stage.height/2;
p.onEnterFrame = function() {
if (this._x
this._x += 15;
} else {
delete this.onEnterFrame;
}
};
var p:MovieClip = mask.attachMovie("Rec", "Rec"+2, 2);
p._width = Stage.width;
p._height = Stage.height;
p._x = Stage.width+p._width/2;
p._y = Stage.height/2;
p.onEnterFrame = function() {
if (this._x>Stage.width/2) {
this._x -= 15;
} else {
delete this.onEnterFrame;
}
};
break;
//=================================================================
//***************效果5[见附录]***************
//随机产生两种倾斜角度,用一个递增变量d_time来控制每个遮罩物的开始时间
case 5 :
if (Math.random()<=0.5) {
var rotation = 45;
} else {
var rotation = -45;
}
for (i=-10, d_time=0; i<30; i++, d_time++) {
var p:MovieClip = mask.attachMovie("Rec", "Rec"+d_time, d_time);
p._width = 0;
p._height = Stage.height*2;
p._x = p._width/2+i*20;
p._y = Stage.height/2;
p._rotation = rotation;
p.delay = d_time;
p.start_time = 0;
p.onEnterFrame = function() {
if (this.start_time
this.start_time += 3;
} else if (this._xscale<30) {
this._xscale += 2;
} else {
delete this.onEnterFrame;
}
};
}
break;
//=================================================================
//***************效果6[见附录]***************
//与效果5相近,但在遮罩物的尺寸及位置上要略加改动
case 6 :
if (Math.random()<=0.5) {
for (i=0; i<45; i++) {
var p:MovieClip = mask.attachMovie("Rec", "Rec"+i, i);
p._width = 0;
p._height = Stage.height+10;
p._x = i*10;
p._y = Stage.height/2;
p.delay = i;
p.start_time = 0;
p.onEnterFrame = function() {
if (this.start_time
this.start_time += 3;
} else if (this._xscale<30) {
this._xscale += 2;
} else {
delete this.onEnterFrame;
}
};
}
} else {
for (i=0; i<35; i++) {
var p:MovieClip = mask.attachMovie("Rec", "Rec"+i, i);
p._width = Stage.width+10;
p._height = 0;
p._x = Stage.width/2;
p._y = i*10;
p.delay = i;
p.start_time = 0;
p.onEnterFrame = function() {
if (this.start_time
this.start_time += 3;
} else if (this._yscale<30) {
this._yscale += 2;
} else {
delete this.onEnterFrame;
}
};
}
}
break;
//=================================================================
//***************效果7[见附录]***************
//复制长条,均放在舞台外的上方,并使Y坐标有所差异,向下运动
case 7 :
for (i=0; i<40; i++) {
var p:MovieClip = mask.attachMovie("Rec", "Rec"+i, i);
p._width = 10;
p._height = Stage.height+50;
p._x = p._width/2+i*p._width;
p._y = -p._height+Math.random()*50;
p.onEnterFrame = function() {
if (this._y
this._y += 20;
} else {
delete this.onEnterFrame;
}
};
}
break;
}
}
Flash充电1: MovieClip.setMask 方法
mc.setMask(mask:Object): 使参数 mask 中的影片剪辑成为 mc 的遮罩层。
Flash充电2: 条件运算符(三目运算) ? :
格式:expression1 ? expression2 : expression3
例如:
var x:Number = 5;
var y:Number = 10;
var z = (x < 6) ? x: y;
trace (z);
// returns 5
补充:flash教程,Action