Frame loop
The concept of frame loop exists in the earliest flash versions. At that time, the ActionScript was not as strong as today.
Large. Write the code to the key frame, and write a statement like gotoandplay in the next frame to enable the playhead)
Return to the previous frame. In this way, an infinite loop is formed between the two frames. Every time the playback header reaches the code frame, it will execute
The code. For example, there is a video clip named ball on the stage.
The code for the first frame is as follows:
Ball. x ++;
The code for the second frame is as follows:
Gotoandplay (1 );
In fact, there is no need to do anything for the second frame, but the timeline is automatically returned to the first frame. Another version is
Three frames. The first frame is initialized, and only code executed once is written without repeating. The second frame is the main execution.
Code, the third frame only writes gotoandplay (2); this method is often used in early flash versions, although a bit too much
But can also complete the task well. Now we will learn more flexible and powerful settings, but in the future you
The principle is the same.
Video events
The film event completely disappears in as 3, which is really a good thing. But I also need to mention it in the example of Flash 5.
In the era of video editing, there are only two options: frame loop and video editing events. A video event indicates that the code is directly written in a video clip, instead
Is the frame. How to implement a video event, first select a video clip on the stage, then open the action panel and write the code
The above code is only valid for this video clip. All code must be written in the event block, for example:
Onclipevent (eventname ){
// Code goes here
}
For onclipevent (eventname), it acts on eventname (an event ). For "on" type events
You must specify mouse or Keyboard Events, such as press and release ).
The event name (eventname) is one of many Flash video events.
Events. There are two types of events: system events and user events. A system event occurs on a computer, Flash, or video.
Events, such as data retrieval, information retrieval, or playback frames. User events refer to some of the things the user has done, basically
There are two types: mouse and keyboard. The most used video events are load and enterframe. Load
The video is executed only once when it appears on the stage for the first time. Therefore, it is very suitable for writing
Initial code. You just need to write the code between braces:
Onclipevent (load ){
// Initialization code
}
We can put video clips with the following code on the timeline (Note: This is written as 1 ):
Onclipevent (load ){
This. _ x = 100;
This. _ y = 100;
}
Onclipevent (enterframe ){
This. _ x + = 5;
}
The code in the example in this book does not use this method (because it is no longer a language ),
Method, initialization, repeating actions, and screen refresh)
Is very important.
Event handling
The actionscrpt of Flash MX has undergone important changes, and these changes and innovations have become true for flash.
Rich Client Program (RIA) lays the foundation. One of them is a brand new event structure.
It is much easier than the previous version. For versions earlier than Flash MX, you can only put the code in a video or a button.
In the onclipevent (eventname) or on (eventname) event processing methods. This means that
You need to put the video clip on the stage and write the code into the video clip. The event structure of MX is not perfect,
However, we have made great strides with the previous version, allowing us to access any event at any time or stop
How powerful and flexible it can be imagined to handle any event or dynamically change the behavior of an event.
To understand events, you must understand the following concepts: lintener and handler.
The two names are very relevant. The Listener is the object that listens to events. The Handler function is used to process the events to be generated.
Function. The process of listening and processing has evolved many times in the Development of ActionScript, and there are many in as 2.
Different implementation methods. To avoid confusion, I highly recommend as 3 because it simplifies this process and changes event processing
More convenient and consistent.
Event Listeners and processing functions
As mentioned earlier, a listener is an object used to listen on events. We can design a class by calling
The addeventlistener function specifies a listener for an event. Enter the name of the event to be listened on and process it.
Function name. Let's look at an example:
Addeventlistener ("enterframe", onenterframe );
When you add an event listener, you can use optional parameters, which are not used in this book. For most applications,
This method is enough. Please note that the event name "enterframe" is a string type, and it is called "magic word ".
String "(magic string ). Why? If you entered "entorframe" by mistake, even though this is not the case
File name. The compiler will compile and execute it, and it will find that the event processing function is not executed. However, as 3 will still Process
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. This attribute can also be used.
An error can be input, just like event. entor_frame. However, if the input is incorrect, the program rejects compilation,
The system prompts you that this attribute does not exist in the event class. The compiler will prompt the row with errors and the exact characters. Therefore, it is best
This method is used unless the compiler helps us correct errors or write code.
In addition, there are other event types such as: mouseevent. mouse_down, keyboardevent. key_down,
Timerevent. Timer. These are simple strings such as "mousedown", "keydown", and "timer ".
If you cannot remember these strings, you 'd better use the properties of the event class.
Another important point is to use the addeventlistener function to directly call functions in the class. Sometimes, you need to listen to another
An event generated by an object. For example, a Sprite movie named myspritebutton: a video
Or button to complete the button action. When you click it, a mousedown event is triggered. Detection
To listen to the mousedown event of the sprite video, you must call the addeventlistener method of the object as follows:
Myspritebutton. addeventlistener (mouseevent. mouse_down, onspritepress );
Finally, there must be an event handler function such as onenterframe. In as 3, it can be any event
The name of the processing function. In the enterframe example, use
Onenterframe is used as an event handler because we are used to this name. As 3,
In Onenterframe
It is no longer a keyword. You can also name this handler as move, run, or dosomethingcool.
However, we are used to using "on" to indicate the start of an event, followed by some descriptive words such as onstartbuttonclick,
Onconfigxmlload or onroketcrash. Some friends like to add "handler" after the event name
Fix, such as enterframehandler, which is just a matter of personal preferences.
The listener is used to listen on events, but for a listener, it may listen on many events at the same time. Inside the system,
An event object has a list of all objects and its own listeners.If an object can generate multiple
Different types of events, such as mousedown, mouseup, and mousemove, have a list of listeners,
It includes all types of events it involves. No matter what events are triggered, the list is retrieved and the list is
Every object in is aware of the event.
Another description of the event is,Think of it as a listener member that joins the event ranks. The object that generates the event
Events generated are published to all members,When you no longer need this object to listen, you can stop it or
The removeeventlistener method is used to remove the Member, that is, to tell the object to delete the listener from the listener list,
In this way, he will no longer receive information.
Let's take a look at this piece of code. Below is a Sprite film created on the stage, drawing it, and then
The code for adding a listener:
Package {
Import flash. display. Sprite;
Import flash. Events. mouseevent;
Public class eventdemo extends sprite {
Private var eventsprite: SPRITE;
Public Function eventdemo (){
Init ();
}
Private function Init (): void {
Eventsprite = new sprite ();
Addchild (eventsprite );
Eventsprite. Graphics. beginfill (0xff0000 );
Eventsprite. Graphics. drawcircle (0, 0,100 );
Eventsprite. Graphics. endfill ();
Eventsprite. x = stage. stagewidth/2;
Eventsprite. Y = stage. stageheight/2;
Eventsprite. addeventlistener (mouseevent. mouse_down, onmousedown );
Eventsprite. addeventlistener (mouseevent. mouse_up, onmouseup );
}
Private function onmousedown (Event: mouseevent): void {
Trace ("mouse down ");
}
Private function onmouseup (Event: mouseevent): void {
Trace ("Mouse up ");
}
}
}
Create a Sprite film in the initialization function (init), draw a circle in it, place it in the center of the stage, and the last two
Add two listeners to the listener to listen for the following events: mouse down (mouse_down) and mouse up (mouse_up.
They are two attributes of the mouseevent class, which must be imported. Finally, two processing functions are defined.
Onmousedown and onmouseup.
When an event object calls an event processing function, it usually includes some event information. When processing mouse events
Information about the mouse position when the event is triggered, such as: click on the button. For Keyboard Events, it is necessary to include when you press the key
Such As ctrl, ALT, and shift. Save the preceding example as an eventdemo. As file, and select
The compilation method mentioned above. When running SWF, you will see that each click or image will output pressed or
Released.
Animation events
We hope that we can use code to make objects move and allow repeated screen refreshes. I have seen a usage
An example of an enterframe video event. To apply this method to as 3, you only need to add one
The listener for the enterframe event can be:
Addeventlistener (event. enter_frame, onenterframe );
Do not forget to import the event class and create a method named onenterframe. People are often confused, only
How can I execute an enterframe event? In fact, the stream play header does not actually enter the next frame,
It only stays on the first frame,It is not to move the playback header to the next frame to form an enterframe event, but to use another
One way: Flash tells the stream play header when to move, you can regard enterframe as a timer, but there are
Some are inaccurate.
Next let's take a look at the first as 3 Animation:
Package {
Import flash. display. Sprite;
Import flash. Events. event;
Public class firstanimation extends sprite {
Private var ball: SPRITE;
Public Function firstanimation (){
Init ();
}
Private function Init (): void {
Ball = new sprite ();
Addchild (ball );
Ball. Graphics. beginfill (0xff0000 );
Ball. Graphics. drawcircle (0, 0, 40 );