Ideas:
1. Use the for...in statement to traverse all objects in the entire main scene (_root);
2. Find them (e.g. _root[k]) and specify different functions for their different methods;
Step 1: Add several movie clips to the home scene without having to name them. Step 2: Add as code:
var f1:function = Function () {
This.startdrag (FALSE);
};
var f2:function = Function () {
This.stopdrag ();
};
var f3:function = Function () {
This._rotation = This.speed;
};
Define three functions:
F1 completes the object to start the drag and drop function;
F2 Finish Object Stop drag and drop function;
The F3 completes the object rotation function, the speed is each object individual speed*/
For (var k in _root) {
_root[k].speed = random (20);
_root[k].onpress = F1;
_root[k].onrelease = F2;
_root[k].onenterframe = F3;
}
Traverse the main scene, set the Speed property and Onpress,onrelease.onenterframe method for each MC.
Flash charge 1: Two ways to define a function
(1) Function statement definition method:
function Testadd (A, b) {
return a B;
}
(2) Function expression Definition method:
var testadd:function = Function (A, b) {
return a B;
};
In peacetime, you should use the Function statement definition [Method 1] As far as possible, this method of definition is more standard and simpler. Difference: Method 2, you need to define, then call, Method 1, you can call first, write the definition.
Flash charge 2:for...in is used to enumerate all the elements in a set, and it is very powerful to iterate over (retrieve) such as XML, array, object, or even _root or MC, etc. In this lesson, we use it to traverse the entire _root.
Example 1:
var car = {brand: "M6", Color: "Red", engine:2000};
For (var k in car) {
Trace (k "=" car[k]);
}
Output results:
Brand=m6
Color=red
engine=2000
Example 2:
First, put four movie clips in the home scene.
For (var k in _root) {
Trace (_root[k]);
}
Output results:
_level0.instance4
_level0.instance3
_level0.instance2
_level0.instance1
We note that even if the MC is not named in the _root, the Flash compiler is automatically named for the MC, and the previous _level10 refers to the depth of the component as 10.
Flash charge 3:. onenterframe
(1) Onenterframe is the soul of as animation and game making.
(2) When we at home scene first frame write onenterframe=function () {...} , in fact the Flash compiler automatically adds _root to the front and turns _root.onenterframe.
(3) Important: Movieclip.onenterframe=function () {...} Form. This form allows the MC to run Onenterframe independently. Cases:
First, there are three movie clips on the main scene, and the instance names are MC1,MC2,MC3. //
Mc1.speed = random (10);
Mc2.speed = random (10);
Mc3.speed = random (10);
Mc1.onenterframe = function () {
Mc1._rotation = Mc1.speed;
};
Mc2.onenterframe = function () {
Mc2._rotation = Mc2.speed;
};
Mc3.onenterframe = function () {
Mc3._rotation = Mc3.speed;
};
Imagine if the scene has 100 such a MC then write the code of the process is not too painful ah ... Oh... Let's take a look at the following wording:
function F1 () {
This._rotation = This.speed;
}
For (var k in _root) {
_root[k].speed = random (10);
_root[k].onenterframe = F1;
}
The complete function is equivalent to:
Mc1.speed = random (10);
Mc1.onenterframe = function () {
This._rotation = This.speed;
};
This refers to the object in the function that calls the function (MC1)