教程
欢迎转载,敬请注明来源--闪吧和作者---sxl001---QQ:285510591
在此只探讨用AS语句去控制移动的方法。FLASH中能够移动的物体一般是舞台上的电影实例(为节省篇幅以下简称为MC,并且其实例名为my_mc)。FLASH中物体的移动是在X轴(即水平)方向或Y轴(垂直)方向的运动。因此,通过控制mc属性中的_x与_y的值就可以达到使其运动的目的。
一、匀速运动
1、水平方向上的向右匀速运动
方法一:
主场景第1帧:var mx=5;//设定初始速度为5
主场景第2帧:my_mc._x+=mx;//名称为my_mc的电影实例以步幅5的速度向右移动,
主场景第3帧:gotoAndPlay(2);// 跳转到第二帧,再次激发my_mc电影实例以步幅5的速度向右移动后又会跳转到此帧,并再次激发运动,如此周而得始地激发,就达到了不断向右运动的目的.
(见实例匀速运动1_1_1)
匀速运动1_1_1.swf
(2006-09-30 04:10:19 PM, Size: 2.06 kB, Downloads: 0)
匀速运动1_1_1.fla
(2006-09-30 04:10:19 PM, Size: 13.5 kB, Downloads: 0)
方法二:
主场景第1帧:
var mx = 5;//设定初始速度为5
this.onEnterFrame = function() {
my_mc._x += mx;//不断刷新my_mc的向右运动
};//方法一需要3帧才能运动,而此方法二只需要一帧就可以使其运动,这是主要区别
(见实例匀速运动1_1_2_1)
匀速运动1_1_2_1.swf
(2006-09-30 04:11:23 PM, Size: 2.17 kB, Downloads: 0)
匀速运动1_1_2_1.fla
(2006-09-30 04:11:23 PM, Size: 15.5 kB, Downloads: 0)
或者:
var mx = 5;//初始速度值为5
onEnterFrame = function () {
my_mc._x += mx;
};//此方法三与方法二的区别在于,方法二中this.onEnterFrame中的this是指当前时间轴;而在此方法三中直接用onEnterFrame,并未用this时,就表示暗指当前时间轴
(见实例匀速运动1_1_2_2)
匀速运动1_1_2_2.swf
(2006-09-30 04:12:23 PM, Size: 2.27 kB, Downloads: 0)
匀速运动1_1_2_2.fla
(2006-09-30 04:12:23 PM, Size: 15.5 kB, Downloads: 0)
或者:
var mx = 5;//初始速度值5
my_mc.onEnterFrame = function() {
this._x += mx;
};//此处的my_mc.onEnterFrame指方法是加在my_mc上的.
(见实例匀速运动1_1_2_3)
匀速运动1_1_2_3.swf
(2006-09-30 04:13:13 PM, Size: 2.25 kB, Downloads: 0)
匀速运动1_1_2_3.fla
(2006-09-30 04:13:13 PM, Size: 11 kB, Downloads: 0)
方法三:
主场景my_mc上:
onClipEvent (load) {
var mx = 5;//初始速度值5
}
onClipEvent (enterFrame) {
_x += mx;//触发my_mc实例定义的动作是x轴坐标不断增值5。
}//(见实例匀速运动1_1_3_1)
匀速运动1_1_3_1.swf
(2006-09-30 04:16:26 PM, Size: 2.14 kB, Downloads: 0)
匀速运动1_1_3_1.fla
(2006-09-30 04:16:26 PM, Size: 15 kB, Downloads: 0)
方法四:
主场景第1帧:
function moveToRight(Object, xVar) {//定义函数的方法
var mx = xVar;
onEnterFrame = function () {
Object._x += mx;
};
}
moveToRight(my_mc, 5);
//(见实例匀速运动1_1_4_1)
匀速运动1_1_4_1.swf
(2006-09-30 04:16:26 PM, Size: 2.13 kB, Downloads: 0)
匀速运动1_1_4_1.fla
(2006-09-30 04:16:26 PM, Size: 10.5 kB, Downloads: 0)
方法五:
MovieClip.prototype.mcmove = function(Object, x) {
var mx = x;
onEnterFrame = function () {
Object._x += mx;
};
};
mcmove(my_mc, 5);//(见实例匀速运动1_1_5_1)
匀速运动1_1_5_1.swf
(2006-09-30 04:16:26 PM, Size: 2.19 kB, Downloads: 0)
匀速运动1_1_5_1.fla
(2006-09-30 04:16:26 PM, Size: 10.5 kB, Downloads: 0)
1、垂直方向上的向下匀速运动
以上实例的运动方向都是水平从左向右匀速运动,如果需要垂直方向上的向下匀速运动只需要把MC的_x属性改为_y。
如:
var my = 5;
this.onEnterFrame = function() {
my_mc._y += my;
};//(见实例匀速运动1_2_01)
匀速运动1_2_01.swf
(2006-09-30 04:16:26 PM, Size: 2.16 kB, Downloads: 0)
匀速运动1_2_01.fla
(2006-09-30 04:16:26 PM, Size: 12.5 kB, Downloads: 0)
MovieClip.prototype.mcmove = function(Object, y) {
var my = y;
onEnterFrame = function () {
Object._y += my;
};
};
mcmove(my_mc, 5);
//(见实例匀速运动1_2_02)
匀速运动1_2_02.swf
(2006-09-30 04:16:26 PM, Size: 2.21 kB, Downloads: 0)
匀速运动1_2_02.fla
(2006-09-30 04:16:26 PM, Size: 15 kB, Downloads: 0)
2、水平方向上的向左匀速运动
水平方向上的向左匀速运动,只需要将上述实例1_系列中的变量var mx=5;更改为var mx=-5;或者,将my_mc._x += mx;更改为my_mc._x -= mx;
如:
var mx = -5;
my_mc._x = 524;//初始my_mc的x坐标。
this.onEnterFrame = function() {
my_mc._x += mx;
};//(见实例匀速运动1_3_01)
匀速运动1_3_01.swf
(2006-09-30 04:20:11 PM, Size: 2.29 kB, Downloads: 0)
匀速运动1_3_01.fla
(2006-09-30 04:20:11 PM, Size: 15.5 kB, Downloads: 0)
或者:
var mx = 5;
my_mc._x = 524;//初始my_mc的x坐标。
this.onEnterFrame = function() {
my_mc._x -= mx;
};//(见实例匀速运动1_3_02)
匀速运动1_3_02.swf
(2006-09-30 04:20:11 PM, Size: 2.34 kB, Downloads: 0)
匀速运动1_3_02.fla
(2006-09-30 04:20:11 PM, Size: 18.5 kB, Downloads: 0)
3、垂直方向上的向上匀速运动
垂直方向上的向下匀速运动更改为向上的匀速运动时,方法同“3、水平方向上的向左匀速运动。”
如:
var my = 5;
my_mc._y = 370;
this.onEnterFrame = function() {
my_mc._y -= my;
};//(见实例匀速运动1_4_01)
匀速运动1_4_01.swf
(2006-09-30 04:20:11 PM, Size: 2.12 kB, Downloads: 0)
匀速运动1_4_01.fla
(2006-09-30 04:20:11 PM, Size: 10 kB, Downloads: 0)
4、斜方向上的匀速运动
如:
var mx = 5, my = 3;
my_mc._x = 0;
my_mc._y = 370;
this.onEnterFrame = function() {
my_mc._x += mx;
my_mc._y -= my;
};//(见实例匀速运动1_5_01)
匀速运动1_5_01.swf
(2006-09-30 04:20:11 PM, Size: 2.05 kB, Downloads: 0)
匀速运动1_5_01.fla
(2006-09-30 04:20:11 PM, Size: 15 kB, Downloads: 0)
精确起终点代码版:
var k = 200;
//速率
var startx = my_mc._x=0, starty = my_mc._y=400;
//起点坐标
var endx = 550, endy = 0;
//终点坐标
onEnterFrame = function () {
my_mc._x += (endx-startx)/k;
my_mc._y += (endy-starty)/k;
};//(见实例匀速运动1_5_02)
匀速运动1_5_02.swf
(2006-09-30 04:20:11 PM, Size: 2.2 kB, Downloads: 0)
匀速运动1_5_02.fla
(2006-09-30 04:20:11 PM, Size: 15 kB, Downloads: 0)
5、在一定范围内的来回匀速运动
⑴水平来回:
var startx = my_mc._x=50;
//startx起点位置
var endx = 450;
//endx结束位置
var dis = 100;
//dis速率
var disx = (endx-startx)/dis;
var disy = (endy-starty)/dis;
var k0 = k=1;
//k方向系数
onEnterFrame = function () {
if (my_mc._x>=endx) {
k = -k;
}
if (my_mc._x<=startx) {
k = k0;
}
my_mc._x += disx*k;
my_mc._y += disy*k;
};//(见实例匀速运动1_6_01)注:垂直来回方法相似
匀速运动1_6_01.swf
(2006-09-30 04:21:51 PM, Size: 2.44 kB, Downloads: 0)
匀速运动1_6_01.fla
(2006-09-30 04:21:51 PM, Size: 15.5 kB, Downloads: 0)
⑵斜向来回:
var k0 = k=1;
var dis = 200;
//dis速率
var startx = my_mc._x=50, starty = my_mc._y=300;
//起点坐标
var endx = 450, endy = 50;
//终点坐标
var disx = (endx-startx)/dis;
var disy = (endy-starty)/dis;
onEnterFrame = function () {
if (my_mc._x<=startx) {
k = k0;
}
if (my_mc._x>=endx) {
k = -k;
}
my_mc._x += disx*k;
my_mc._y += disy*k;
};//(见实例匀速运动1_6_02)
匀速运动1_6_02.swf
(2006-09-30 04:21:51 PM, Size: 2.4 kB, Downloads: 0)
匀速运动1_6_02.fla
(2006-09-30 04:21:51 PM, Size: 15.5 kB, Downloads: 0)
二、变速运动
1、水平方向上的变速运动
⑴减速运动
方法一:
主场景第1帧:
var endPosition = 500;
var k = 12;
my_mc._x = 50;
my_mc._y = 200;
主场景第2帧:my_mc._x += (endPosition-my_mc._x)/k;
主场景第3帧:
gotoAndPlay(2);
if (my_mc._x>=(endPosition-0.6)) {
my_mc._x = endPosition;
stop();
}
//(见实例减速运动2_1_1_01)
减速运动2_1_1_01.swf
(2006-09-30 04:39:46 PM, Size: 2.29 kB, Downloads: 0)
减速运动2_1_1_01.fla
(2006-09-30 04:39:46 PM, Size: 15 kB, Downloads: 0)
方法二:
var endPosition = 475;
var k = 12;
my_mc._x = 50;
my_mc._y = 200;
onEnterFrame = function () {
my_mc._x += (endPosition-my_mc._x)/k;
if (my_mc._x>(endPosition-1)) {
my_mc._x = endPosition;
delete onEnterFrame;
}
};//(见实例减速运动2_1_1_02)
减速运动2_1_1_02.swf
(2006-09-30 04:39:46 PM, Size: 2.27 kB, Downloads: 0)
减速运动2_1_1_02.fla
(2006-09-30 04:39:46 PM, Size: 12.5 kB, Downloads: 0)
如果要实现从右向左运动时,只需要稍改上述的实例减速运动2_1_1_02
var endPosition = 50;
var k = 12;
my_mc._x = 550;
my_mc._y = 200;
onEnterFrame = function () {
trace(my_mc._x);
my_mc._x += (endPosition-my_mc._x)/k;
if (my_mc._x<=endPosition) {
my_mc._x = endPosition;
delete onEnterFrame;
}
};(见实例减速运动2_1_1_03)
减速运动2_1_1_03.swf
(2006-09-30 04:39:46 PM, Size: 2.38 kB, Downloads: 0)
减速运动2_1_1_03.fla
(2006-09-30 04:39:46 PM, Size: 12.5 kB, Downloads: 0)
特别说明:上述的减速运动代码可广泛运用于实际中,它不仅可用于向上、下、左、右、斜向各个方向上的运动,产生缓冲效果。也可以运用于透明度和缩放等方面。如下面的两个例子。
另一个减速缓冲效果:
var endPosition = 500;
var k = 0.7;
var c = 0.2;
my_mc._x = 10;
my_mc._y = 200;
onEnterFrame = function () {
temp = temp*k+(endPosition-my_mc._x)*c;//此公式常用
my_mc._x += Math.round(temp);
if (Math.round(temp) == 0) {
my_mc._x = endPosition;
delete onEnterFrame;
}
};
(见实例减速运动2_1_1_04)
减速运动2_1_1_04.swf
(2006-09-30 04:39:46 PM, Size: 2.37 kB, Downloads: 0)
减速运动2_1_1_04.fla
(2006-09-30 04:39:46 PM, Size: 12.5 kB, Downloads: 0)
透明度上的运用:
var endAlpha = 10;
var k = 5;
my_mc._alpha = 100;
onEnterFrame = function () {
my_mc._alpha += (endAlpha-my_mc._alpha)/k;
if (my_mc._alpha<=endAlpha) {
my_mc._alpha = endAlpha;
delete onEnterFrame;
}
};(见实例透明度上的缓冲运用2_1_1_05)
透明度上的缓冲运用2_1_1_05.swf
(2006-09-30 04:43:25 PM, Size: 3.09 kB, Downloads: 0)
透明度上的缓冲运用2_1_1_05.fla
(2006-09-30 04:43:25 PM, Size: 10.5 kB, Downloads: 0)
缩放上的运用:
var endscale = 600;
var k = 6;
my_mc._xscale = my_mc._yscale=1;
onEnterFrame = function () {
trace(my_mc._yscale);
my_mc._xscale = my_mc._yscale += (endscale-my_mc._yscale)/k;
if (my_mc._yscale>=(endscale-0.1)) {
my_mc._yscale = endscale;
delete onEnterFrame;
}
};(见实例缩放上的缓冲运用2_1_1_06)
缩放上的缓冲运用2_1_1_06.swf
(2006-09-30 04:43:25 PM, Size: 3.04 kB, Downloads: 0)
缩放上的缓冲运用2_1_1_06.fla
(2006-09-30 04:43:25 PM, Size: 12.5 kB, Downloads: 0)