This course covers: function definition,
For... in statement,. onEnterFrame three key content
The function is used to realize code reuse. With the for... in statement, you can batch assign functions.
Hope beginners can read it carefully
Idea: 1. Use the for... in statement to traverse all objects in the main scenario (_ root;
2. Find them (such as _ root [k]) and specify different functions for their different methods;
Step 1:
You do not need to create an instance name for multiple video clips in the main scenario.
Step 2:
Add the AS code:
Var F1: Function =
Function (){
This. startDrag (true );
};
Var F2: Function = function (){
This. stopDrag ();
};
Var F3: Function = function (){
This. _ rotation + = this. speed;
};
// Define three functions:
// F1 completes the object dragging function;
// F2 completes the object stop dragging function;
// F3 completes the object rotation function. The speed is the speed of each object */
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 scenario and set the speed attribute and onPress and onRelease. onEnterFrame methods for each MC.
Flash charging 1: two methods to define functions
(1) function statement definition method: for example:
Function Testadd (a, B ){
Return a + B;
}
(2) function expression definition: for example
Var Testadd: Function = function (a, B ){
Return a + B;
};
At ordinary times, we should try to use function statements to define [Method 1]. This definition method is more standard and simpler.
Difference: Method 2, which must be defined and called first;
Method 1, which can be called first and then written to the definition.
Flash Charging 2: for... in
Used to enumerate all elements in a set
It is mainly used for traversing (retrieval), such as XML, arrays, objects, and even _ root or MC. It has very powerful functions.
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 result:
// Brand = M6
// Color = red
// Engine = 2000
Example 2:
// First, put four video clips in the home scene
For (var k in _ root ){
Trace (_ root [k]);
}
// Output result:
// _ Level0.instance4
// _ Level0.instanpushed
// _ Level0.instance2
// _ Level0.instance1
We noticed that even if the MC name in _ root is not given, the FLASH compiler automatically named MC. The previous _ level10 indicates that the depth of the component is 10.
Flash Charging 3:
. OnEnterFrame
(1) onEnterFrame is the soul of AS animation and game production.
(2) when we write onEnterFrame = function () {...} in the first frame of the main scenario,
In fact, the Flash compiler automatically adds _ root to _ root. onEnterFrame.
(3) important: MovieClip. onEnterFrame = function () {...} form.
This form allows MC to run onEnterFrame independently.
Example:
// First, there are three video clips in the home scene. The instance names are MC1, MC2, and MC3 respectively. //
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 there are 100 such MC in the scenario, isn't the process of writing code too painful.
Let's look at the following method:
Function F1 (){
This. _ rotation + =
This. speed;
}
For (var k in _ root ){
_ Root [k]. speed =
Random (10 );
_ Root [k]. onEnterFrame = F1;
}
The completed functions are equivalent:
MC1.speed =
Random (10 );
MC1.onEnterFrame =
Function (){
This. _ rotation + =
This. speed;
};
In a function, this indicates the object that calls the function (MC1)