Similarities and differences between onenterframe and onclipevent (enterframe) of as2 and as3
In the flashas manual, enterframe is interpreted as a frame entry.
_ MC. onenterframe {} indicates that an event is triggered when the video is played to the current frame.
When I first started learning as, I naturally thought that if the current frame uses the stop statement, the video will stop playing and the onenterframe event will no longer be triggered. But interestingly, in fact, enterframe is most useful in combination with stop.
In other programming languages, when a variable changes, loop statements are generally used to wait. These loop statements are generally if... else, do... loop, for... next, and so on. These are also available in as, but you can try it yourself. Once these loops are used, the film can easily pause or even enter an endless loop once the number of loops is large.
In as, loop statements are generally not used to monitor variables. It is implemented with the combination of enterframe and stop.
There is no declaration in the as help Article: When the video stops at the current frame, it does not actually stop playing, but repeatedly plays the current frame. In addition, the video is repeatedly played based on the frame frequency set by the video, repeatedly triggering the enterframe event.
I have been confused for a long time.
However, many examples of as use this pattern for loop:
Stop ();
_ MC. onenterframe = function {
Program processing statement
};
To understand this, we can control the loop speed by setting the frame frequency of video playback. For example, when it takes a long time, you can even cycle every second.
The following describes the differences between onenterframe and onclipevent (enterframe. Because many beginners may be confused.
Onenterframe onclipevent (enterframe)
Place of use written in the frame script editing box written in the movieclip (video editing) script editing box
Format _ MC. oneenterframe = function () {} onclipevent (enterframe ){}
Type event processing function events
Before Execution
"Execution order" is the conclusion of my own experiment.
In one frame, the script of the frame includes:
_ MC. oneenterframe = function (){
Trace ("oneenterframe ")
}
Then write the following in the _ Mc script:
Onclipevent (enterframe ){
Trace ("onclipevent (enterframe )")
}
At that time, my abnormal psychology was to look at onenterframe and onclipevent (enterframe). Which of the following is more powerful? What is the result?
The result is:
Onclipevent (enterframe)
Onenterframe
Onclipevent (enterframe)
Onenterframe
Onclipevent (enterframe)
Onenterframe
Onclipevent (enterframe)
Onenterframe
Slave --------------------------------------------------------------------------------------------------------------------------------
The onenterframe function is awesome in the as2 era, and no one is familiar with this event. How can I use as3? I have studied it carefully.
The addeventlistener function specifies a listener for an event. Enter the name of the event to be listened on and the name of the function to be processed. Let's look at an example:
Addeventlistener ("enterframe", onenterframe );
When you add an event listener, you can use an optional parameter. The event name "enterframe" is a string type, and someone calls it a magic string ). Why? If you mistakenly enter "entorframe", the compiler will compile and execute the event without the event name, but it will find that the event handler function is not executed. In addition to the magic string, you can also use the attribute of the event class. For example:
Addeventlistener (event. enter_frame, onenterframe );
In fact, the value of event. enter_frame is the string "enterframe. Therefore, we recommend that you use this method.
In addition, the event processing function here is onenterframe, which can be named at will in as 3. For example, it can be written as follows:
Addeventlistener (event. enter_frame, click snow next );
But we are used to this name in as2. Everyone can understand it at a glance. If you want to stop the event listening, you can use the removeeventlistener method to remove the Member, that is, to tell the object to delete the listener from the listener list, so that it will no longer receive information.
Itch. Come and try it:
Steps:
1. Create a snowflake falling video clip in the library, and select export as ActionScript in component properties. In the previous flash version, you can freely give an identifier or enter a class name. In Flash CS3, the identifier column is unavailable, and the class column is automatically filled with the default value. The base class column is also displayed. The default value is flash. display. movieclip. You can also enter the custom class inherited from movieclip or Sprite. Enter a class named "XiaXue" without worrying about this class, and click "OK. This is interesting. If Flash cannot find this class, it will automatically generate a class and compile it. It does not mean that flash will create an ActionScript class file, but it will generate a string of byte code in SWF to represent a class inherited from sprite or movieclip. Apart from inheriting the base class, it does not do anything, but it is connected to the component in the library. I learned this from Kang Laoshi and applied it to me.
2. Create a file class called frame rate event.. Write code:
Package {
Import flash. display. Sprite;
Import flash. display. movieclip;
Import flash. Events. event;
Public class Frame Rate event extends sprite {
VaR I: Number = 1;
Public Function Frame Rate event (){
Snow ();
}
Private function snow (): void {
Addeventlistener (event. enter_frame, light snow );
}
Function light snow (Event: Event): void {
VaR Snowflake: XiaXue = new XiaXue ();
Addchild (snowflake );
Snowflake. x = math. Random () * 550;
Snowflake. scalex = 0.2 + math. Random ();
Snowflake. scaley = 0.2 + math. Random ();
I ++;
When (I> 100 ){
This. removechildat (1 );
I = 100;
}
}
}
}
Is it snowing? OK
Experiences
1. Kang once said that it is very useful to subclass A sprite or movieclip class. It replaces the attachmovieclip function in as 2. to extract the video clip element from the library and put it on the stage, we use a class inherited from movieclip or Sprite. Apart from inheriting the base class, it does nothing, but it is connected to the component in the library. Then write:
VaR Snowflake: XiaXue = new XiaXue ();
Addchild (snowflake );
In this way, a component in the library is created on the stage, just like the attachmovie method of as 2. If we can give a custom real class name and path, we can add many functions to the component.
Of course, this is not enough. In as3, attachmovie has disappeared. In as3, use new to create any visual element and add it to the stage as needed, remove it when you don't need it. When you use it again, you can put it again.
This statement is: addchild (snowflake );
// Literally, the child is added to the stage through the document class. Haha, he used to rest in the background, so it is very convenient. Due to this feature, the components in different SWF can be used with each other. For example, if you create an icon in the swf1 library, the bound class is "Class 1 ", you can load swf1 to swf2, create an icon through New Class 1 (), and add it to the stage of swf2 through addchild. In this way, you do not need to use the shared library function.
Professionally, this is called "display list". In as 2, the usage of the display list is:
To create a video clip using attach or createemptymovieclip, you must specify the location of the video clip in the tree. In this way, the video clip must be placed in the specified position of the list. When you delete a video, you cannot change its position in the list or remove it from the list.
Some sprite films created in as 3 are not automatically added to the display list. It does not involve parent videos or depth, so that you can operate on it before it is added to the visual list.
Of course, the removechild method is used to delete an object from the display list and take the object name as a parameter. The object is not destroyed, but is temporarily removed to the "background". When it is added to the display list again, the object remains in the original state.
2. duplicatemovieclip () has been deleted. Use the new operator to create a new instance in ActionScript 3.0. This is not a long question.