The tutorials Welcome reprint, please indicate the source--Flash bar and author---sxl001---qq:285510591
This only discusses the method of controlling the movement with the as statement. The object that can move in Flash is usually the movie instance on stage (for Save space hereinafter abbreviation is MC, and its instance name is MY_MC). The movement of objects in Flash is in the x-axis (i.e. horizontal) direction or the y-axis (vertical) direction. Therefore, by controlling the value of _x and _y in MC Properties, it can achieve the aim of motion.
One, uniform motion
1. Uniform motion on the right horizontal direction
Method One:
Main scene frame 1th: Var mx=5;//set initial speed to 5
Main Scene Frame 2nd: my_mc._x+=mx;//The movie instance named MY_MC moves to the right at a pace of 5,
Main Scene Frame 3rd: gotoAndPlay (2);///Jump to second frame, excite MY_MC movie instance to move to the right with stride 5 and then jump to this frame again, and excite the movement again, so the week starts to excite, has achieved the goal of continuously right movement.
(see example uniform motion 1_1_1)
Uniform Motion 1_1_1.swf
(2006-09-30 04:10:19 PM, size:2.06 KB, downloads:0)
Uniform Motion 1_1_1.fla
(2006-09-30 04:10:19 PM, size:13.5 KB, downloads:0)
Method Two:
Main scene Frame 1th:
var mx = 5;//Set initial speed to 5
This.onenterframe = function () {
My_mc._x + + mx;//constantly refreshing my_mc to the right movement
The};//method requires 3 frames to move, and this method two takes only one frame to make it move, which is the main difference
(see example uniform motion 1_1_2_1)
Uniform Motion 1_1_2_1.swf
(2006-09-30 04:11:23 PM, size:2.17 KB, downloads:0)
Uniform Motion 1_1_2_1.fla
(2006-09-30 04:11:23 PM, size:15.5 KB, downloads:0)
Or:
var mx = 5;//Initial speed value is 5
Onenterframe = function () {
my_mc._x = mx;
};//the difference between this method and method two is that this is the current time axis in the This.onenterframe in method two, and Onenterframe in this method, which means that the current timeline is implied.
(see example uniform motion 1_1_2_2)
Uniform Motion 1_1_2_2.swf
(2006-09-30 04:12:23 PM, size:2.27 KB, downloads:0)
Uniform Motion 1_1_2_2.fla
(2006-09-30 04:12:23 PM, size:15.5 KB, downloads:0)
Or:
var mx = 5;//Initial speed value 5
My_mc.onenterframe = function () {
this._x = mx;
};//here the my_mc.onenterframe means is added to the MY_MC.
(see example uniform motion 1_1_2_3)
Uniform Motion 1_1_2_3.swf
(2006-09-30 04:13:13 PM, size:2.25 KB, downloads:0)
Uniform Motion 1_1_2_3.fla
(2006-09-30 04:13:13 PM, size:11 KB, downloads:0)
Method Three:
Home View MY_MC:
Onclipevent (load) {
var mx = 5;//Initial speed value 5
}
Onclipevent (enterframe) {
_x + + mx;//triggers a MY_MC instance defined by an x-axis coordinate that continuously adds 5.
}//(see example uniform motion 1_1_3_1)
Uniform Motion 1_1_3_1.swf
(2006-09-30 04:16:26 PM, size:2.14 KB, downloads:0)
Uniform Motion 1_1_3_1.fla
(2006-09-30 04:16:26 PM, size:15 KB, downloads:0)
Method Four:
Main scene Frame 1th:
function Movetoright (Object, Xvar) {///define functions method
var mx = Xvar;
Onenterframe = function () {
object._x = mx;
};
}
Movetoright (MY_MC, 5);
(see example uniform motion 1_1_4_1)
Uniform Motion 1_1_4_1.swf
(2006-09-30 04:16:26 PM, size:2.13 KB, downloads:0)
Uniform Motion 1_1_4_1.fla
(2006-09-30 04:16:26 PM, size:10.5 KB, downloads:0)
Method Five:
MovieClip.prototype.mcmove = function (Object, x) {
var mx = x;
Onenterframe = function () {
object._x = mx;
};
};
Mcmove (MY_MC, 5);//(see Example Uniform motion 1_1_5_1)
Uniform Motion 1_1_5_1.swf
(2006-09-30 04:16:26 PM, size:2.19 KB, downloads:0)
Uniform Motion 1_1_5_1.fla
(2006-09-30 04:16:26 PM, size:10.5 KB, downloads:0)
1. Uniform downward motion in vertical direction
The moving direction of the above example is horizontal from left to right, if the downward motion in vertical direction is required, the _x property of MC must be changed to _y.
Such as:
var my = 5;
This.onenterframe = function () {
My_mc._y = my;
};//(see example uniform motion 1_2_01)
Uniform Motion 1_2_01.swf
(2006-09-30 04:16:26 PM, size:2.16 KB, downloads:0)
Uniform Motion 1_2_01.fla
(2006-09-30 04:16:26 PM, size:12.5 KB, downloads:0)
MovieClip.prototype.mcmove = function (Object, y) {
var i = y;
Onenterframe = function () {
Object._y = my;
};
};
Mcmove (MY_MC, 5);
(see example uniform motion 1_2_02)
Uniform Motion 1_2_02.swf
(2006-09-30 04:16:26 PM, size:2.21 KB, downloads:0)
Uniform Motion 1_2_02.fla
(2006-09-30 04:16:26 PM, size:15 KB, downloads:0)
2, horizontal direction on the left uniform motion
The horizontal direction of uniform motion, only the above instance 1_ series of the variable var mx=5, change to Var mx=-5, or, my_mc._x = = mx, change to my_mc._x-= MX;
Such as:
var mx =-5;
my_mc._x = 524;//The x-coordinate of the initial MY_MC.
This.onenterframe = function () {
my_mc._x = mx;
};//(see example uniform motion 1_3_01)
Uniform Motion 1_3_01.swf
(2006-09-30 04:20:11 PM, size:2.29 KB, downloads:0)
Uniform Motion 1_3_01.fla
(2006-09-30 04:20:11 PM, size:15.5 KB, downloads:0)
Or:
var mx = 5;
my_mc._x = 524;//The x-coordinate of the initial MY_MC.
This.onenterframe = function () {
my_mc._x-= mx;
};//(see example uniform motion 1_3_02)
Uniform Motion 1_3_02.swf
(2006-09-30 04:20:11 PM, size:2.34 KB, downloads:0)
Uniform Motion 1_3_02.fla
(2006-09-30 04:20:11 PM, size:18.5 KB, downloads:0)
3. Upward uniform motion in vertical direction
When the downward motion in the vertical direction is changed to the upward uniform motion, the method is uniform to the left with "3" and horizontal direction. ”
Such as:
var my = 5;
my_mc._y = 370;
This.onenterframe = function () {
My_mc._y = my;
};//(see example uniform motion 1_4_01)
Uniform Motion 1_4_01.swf
(2006-09-30 04:20:11 PM, size:2.12 KB, downloads:0)
Uniform Motion 1_4_01.fla
(2006-09-30 04:20:11 PM, Size:10 KB, downloads:0)
4. Uniform motion in oblique direction
Such as:
var mx = 5, my = 3;
my_mc._x = 0;
my_mc._y = 370;
This.onenterframe = function () {
my_mc._x = mx;
My_mc._y = my;
};//(see example uniform motion 1_5_01)
Uniform Motion 1_5_01.swf
(2006-09-30 04:20:11 PM, size:2.05 KB, downloads:0)
Uniform Motion 1_5_01.fla
(2006-09-30 04:20:11 PM, size:15 KB, downloads:0)
Accurate Finish code version:
var k = 200;
Rate
var startx = my_mc._x=0, starty = my_mc._y=400;
Start coordinates
var endx = Endy = 0;
Endpoint coordinates
Onenterframe = function () {
My_mc._x + = (endx-startx)/k;
My_mc._y + = (endy-starty)/k;
};//(see example uniform motion 1_5_02)
Uniform Motion 1_5_02.swf
(2006-09-30 04:20:11 PM, size:2.2 KB, downloads:0)
Uniform Motion 1_5_02.fla
(2006-09-30 04:20:11 PM, size:15 KB, downloads:0)
5, in a certain range of uniform motion back and forth
⑴ horizontal back and forth:
var startx = my_mc._x=50;
StartX Start position
var endx = 450;
EndX End Position
var dis = 100;
DIS rate
var disx = (endx-startx)/dis;
var disy = (endy-starty)/dis;
var k0 = k=1;
K-directional coefficient
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;
};//(see example uniform motion 1_6_01) Note: Vertical back-and-forth method similar
Uniform Motion 1_6_01.swf
(2006-09-30 04:21:51 PM, size:2.44 KB, downloads:0)
Uniform Motion 1_6_01.fla
(2006-09-30 04:21:51 PM, size:15.5 KB, downloads:0)
⑵ diagonal back and forth:
var k0 = k=1;
var dis = 200;
DIS rate
var startx = my_mc._x=50, starty = my_mc._y=300;
Start coordinates
var endx = 450, Endy = 50;
Endpoint coordinates
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;
};//(see example uniform motion 1_6_02)
Uniform Motion 1_6_02.swf
(2006-09-30 04:21:51 PM, size:2.4 KB, downloads:0)
Uniform Motion 1_6_02.fla
(2006-09-30 04:21:51 PM, size:15.5 KB, downloads:0)
Second, variable motion
1, the horizontal direction of the variable motion
⑴ deceleration movement
Method One:
Main scene Frame 1th:
var endposition = 500;
var k = 12;
my_mc._x = 50;
my_mc._y = 200;
Main Scene Frame 2nd: my_mc._x + = (endposition-my_mc._x)/k;
Main Scene Frame 3rd:
gotoAndPlay (2);
if (my_mc._x>= (endPosition-0.6)) {
my_mc._x = endposition;
Stop ();
}
(see example deceleration movement 2_1_1_01)
Deceleration movement 2_1_1_01.swf
(2006-09-30 04:39:46 PM, size:2.29 KB, downloads:0)
Deceleration movement 2_1_1_01.fla
(2006-09-30 04:39:46 PM, size:15 KB, downloads:0)
Method Two:
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;
}
};//(see example deceleration movement 2_1_1_02)
Deceleration movement 2_1_1_02.swf
(2006-09-30 04:39:46 PM, size:2.27 KB, downloads:0)
Deceleration movement 2_1_1_02.fla
(2006-09-30 04:39:46 PM, size:12.5 KB, downloads:0)
If you want to move from right to left, you only need to slightly change the above example deceleration motion 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;
}
;(See example deceleration movement 2_1_1_03)
Deceleration movement 2_1_1_03.swf
(2006-09-30 04:39:46 PM, size:2.38 KB, downloads:0)
Deceleration movement 2_1_1_03.fla
(2006-09-30 04:39:46 PM, size:12.5 KB, downloads:0)
Special note: The above deceleration movement code can be widely used in practice, it can not only for up, down, left, right, oblique in all directions of the movement, resulting in buffering effect. It can also be used in areas such as transparency and scaling. As in the following two examples.
Another deceleration buffer effect:
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;//This formula is commonly used
My_mc._x + = Math.Round (temp);
if (math.round (temp) = = 0) {
my_mc._x = endposition;
Delete Onenterframe;
}
};
(see example deceleration movement 2_1_1_04)
Deceleration movement 2_1_1_04.swf
(2006-09-30 04:39:46 PM, size:2.37 KB, downloads:0)
Deceleration movement 2_1_1_04.fla
(2006-09-30 04:39:46 PM, size:12.5 KB, downloads:0)
The use of transparency:
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;
}
;(See examples of transparency on the use of buffering 2_1_1_05)
The buffering use of transparency 2_1_1_05.swf
(2006-09-30 04:43:25 PM, size:3.09 KB, downloads:0)
The buffering use of transparency 2_1_1_05.fla
(2006-09-30 04:43:25 PM, size:10.5 KB, downloads:0)
The use of scaling:
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;
}
;(See buffer usage for instance scaling 2_1_1_06)
Buffer use on scaling 2_1_1_06.swf
(2006-09-30 04:43:25 PM, size:3.04 KB, downloads:0)
Buffer use on scaling 2_1_1_06.FLA
(2006-09-30 04:43:25 PM, size:12.5 KB, downloads:0)