We've learned how to write code on the timeline in Flash CS3, how to convert the code on the timeline to an external class, and the document class form of the Flash CS3, and then we'll look at how to write classes and external as files.
Include
If you understand as1.0,as2.0 programming, then it must be no stranger to include, in ActionScript 3.0 we can still use include to import code.
Cases:
1. Open Flash to create a new document. Save As Drag_inclue.fla create a movie clip in the scene, use the ball in this example, convert it to a movie clip, and name it circle_mc in the scene. You do not need to set link properties in the library.
2, add a layer, press F9 Open the Action panel, enter the code as follows:
Code:
//设置当光标移到circle_mc上时显示手形
circle_mc.buttonMode = true;
// 侦听事件
circle_mc.addEventListener(MouseEvent.CLICK,onClick);
circle_mc.addEventListener(MouseEvent.MOUSE_DOWN,onDown);
circle_mc.addEventListener(MouseEvent.MOUSE_UP,onUp);
//定义onClick事件
function onClick(event:MouseEvent):void{
trace("circle clicked");
}
//定义onDown事件
function onDown(event:MouseEvent):void{
circle_mc.startDrag();
}
function onUp(event:MouseEvent):void{
circle_mc.stopDrag();
}
3, the code on this frame all selected, press Ctrl+x cut, create a new ActionScript file, will cut the code affixed. Save the name drag_include.as. With DRAG_INCLUDE.FLA under the same path.
4. Back to Drag_include.fla, enter the following code on the first frame:
Include "Drag_include.as"
When you test a movie, you can see the same results as when you were testing on the timeline. This method is used frequently in AS1.0. If you're still used to this approach, you can still use it in ActionScript 3.0.
Component Class (Symbol Class)
The component class, in fact, specifies a link class name for the symbol in the Flash movie. It differs from the include above in that it uses a strict class structure. Rather than the way we are accustomed to writing the timeline. We're going to encapsulate the drag function of the little ball so that no matter how many small balls you can drag, it will be easy, just create an instance of it and show it.
We still use the example above to explain: Open the Drag_include.fla file, save it as a Symbol_class.fla file, create a new ActionScript file, save it as a symbol_class.as file, and Symbol_ CLASS.FLA files are in the same directory. Now we'll abstract the code in the example above into the following class:
Code:
Package {
import flash.display.MovieClip;
Import flash.events.MouseEvent;
public class Symbol_class extends MovieClip {
Public Function Symbol_class () {
This.buttonmode = true;
This.addeventlistener (Mouseevent.click,onclick);
This.addeventlistener (Mouseevent.mouse_down,ondown);
This.addeventlistener (Mouseevent.mouse_up,onup);
}
Private Function OnClick (event:mouseevent): void{
Trace ("Circle clicked" );
}
Private Function Ondown (event:mouseevent): void{
This.startdrag ();
Private Function Onup (event:mouseevent): void{
This.stopdrag ();
}
}
Because we set the class name to Symbol_class, the class file must be saved as a symbol_class.as file. We also have to do one step work, open the library panel in the source file, right link, class name: Symbol_class that let us class and component related. When you test the movie, you will see the same result as in the previous example. Note: The scene still has to ensure the existence of CIRCLE_MC. Because we don't add CIRCLE_MC dynamically in the code. Also in this example, as in the previous example, we do not use the AS3.0 document class.