Notes
First say hello, trace out "Hello world!" in the output panel. Review the AS2 Class code:
Class net.eidiot.learnas3.helloas2{
Public Function HelloAs2 () {
Trace ("Hello world!");
}
}
In order for this code to work, you need to enter the code for the first frame of the scene in the Flash IDE:
Import Net.eidiot.learnAS3.HelloAs2;
var hello:helloas2 = new HelloAs2 ();
Let's look at the AS3 class:
Package Net.eidiot.learnAS3
{
Import Flash.display.Sprite;
public class HelloAs3 extends Sprite
{
Public Function HelloAs3 () {
Trace ("Hello world!");
}
}
}
To open Flash 9, enter the package name and class name in document class for the documentation properties, as shown in the figure:
Let's look at the difference here. In the AS3 class, there is a package keyword, followed by the package name of the class (if the FLA and class are in the same folder, nothing can be followed). This way, when declaring a class, you don't have to bring the package name.
public class HelloAs3 extends Sprite
The class keyword has one more public before it. In AS3, classes can also be internal. If the declaring class is internal, only the other classes in the same package can reference the import, and no class outside the package will be able to access it. This class inherits the Sprite. AS3 class If you want to use the event or method of MC, you must let it inherit MovieClip or Sprite. Sprite can be understood as MovieClip without a timeline.
The final step is to set it to the FLA's document class, so that the class HelloAs3 is bound to the document HELLOAS3.FLA.
Are you feeling AS3 too much trouble? It takes a bit more effort to beat the mosquitoes with anti-aircraft guns.
Try to make a simple counter with AS3. Let's take a look at the final effect:
Public Function Showtimer () {
Stage.scalemode = Stagescalemode.no_scale;
Stage.align = Stagealign.top_left;
INITMC ();
}
A lot of new constants are added to the AS3 instead of strings. This brings us a lot of convenience. For example, to limit the movie's zoom mode to a fixed size, the AS2 code is
Stage.scalemode = "Noscale";
A value is a string that, when entered, is not prompted by code, and is easily lost (I often copy the string in the Help document). And the code in the AS3 is:
Stage.scalemode = Stagescalemode.no_scale;
The original string "Noscale" is replaced by a constant stagescalemode.no_scale. This can be done automatically using code hints, effectively avoiding bug (and very convenient) bugs caused by errors. The same string constants also have some event types such as Mouseevent.click instead of "click" and so on.
Private Function INITMC (): void{
Showtxt = new TextField ();
Addshow (showtxt,10,10,310,20);
AddLabel (setdelaylabel,10,40, "delay:");
.. other code
}
Add text boxes and buttons. Note that the text box that needs to be referenced again must be explicitly initialized, otherwise referencing this variable elsewhere will return null.
Private Function AddLabel (txt:textfield,x:uint,y:uint,text:string): void{
txt = new TextField ();
Txt.x = x;
Txt.y = y;
Txt.text = text;
AddChild (TXT);
}
All the things in AS3 are new. Only new is not available, you must use AddChild () to add it to the display list.
Private Function addbtn (mc:sprite,..., clickhanlder:function): void{
Mc.mousechildren = false;
Mc.graphics.beginFill (0x000000,0.3);
Mc.graphics.drawRect (0,0,W,H);
Mc.buttonmode = true;
Mc.addeventlistener (Mouseevent.click,clickhanlder);
AddChild (MC);
//
txt = new TextField ();
Txt.name = "Btntext";
Mc.addchild (TXT);
}
In AS3 want MC to be a button must be set:
Mc.buttonmode = true;
At this time to see the mouse after the MC did not become a hand shape, the reason in the last line, the TXT added to the MC used to display button text, so that the object of the mouse event is txt rather than the expected MC. In order to solve this problem, we need to add one sentence:
Mc.mousechildren = false;
To ensure that the MC is the target object of the mouse event (target objects).
All the visible objects in the AS3 are subclasses of the Displayobject, and Displayobject is a subclass of Eventdispatcher.
Sprite→displayobjectcontainer→interactiveobject→displayobject→eventdispatcher→object
That is to say, all visible objects can be addeventlistener directly.
Mc.addeventlistener (Mouseevent.click,clickhanlder);
This replaces the event type "click" with a constant mouseevent.click. Such constants are not to be repeat later.
Mc.graphics.beginFill (0x000000,0.3);
Mc.graphics.drawRect (0,0,W,H);
All the drawing methods in the AS3 are in the Graphics. The graphics attribute of sprite is a graphics. In addition to the basic Beginfill, Beginbitmapfill, and so on, and added a new drawcircle, DrawEllipse, DrawRect and other methods, no longer endless moveTo, lineTo.
Public Function Starttimer (event:mouseevent): void{
... code here
}
Here's the main stuff: Timer.
var delay:uint = Setdelaytxt.text;
var repeatcount:uint = Setrepeatcounttxt.text;
if (timer = = null) {
Timer = new timer (delay,repeatcount);
}
UINT is the AS3 new data type, representing a 32-bit positive integer (int represents a 32-bit signed integer). The timer constructor accepts two parameters, delay is the number of milliseconds that the timer event is delayed, RepeatCount is the number of loops, and the default is 0, which loops until stop or reset.
Timer.addeventlistener (Timerevent.timer,timerhandler);
Timer.addeventlistener (Timerevent.timer_complete,timercompletehandler);
The timer broadcasts two events, broadcasts a "timer" event every delay the specified millisecond, and broadcasts the "Timercomplete" event after looping repeatcount times.
Timer.start ();
Startbtn.getchildbyname ("Btntext"). Text = "Stop";
The timer begins execution after start, at which point the running property is true. Set the startbtn to "stop" and note that AS3 is not able to get the STARTBTN child because Sprite is not a dynamic class and cannot declare its child. When you want to get the text box inside the STARTBTN, you need to use Getchildbyname method. Of course, first give the child a name:
function addbtn
Txt.name = "Btntext";
The last is the difference between stop and reset: Reset after the stop to set the Currentcount property to 0. It can be realized by the last SWF compiled.
When you do something, you find that AS3 's Eventdispatcher class doesn't seem to pass parameters. Ask bogey, answer Yue, write a class inheritance Event, put the parameters in the structure. Try it, it really works.
The page Generation section is not covered, the only noteworthy is that the TextField class adds a AppendText method. of the previous
Mytxt.text + + "your text";
should be written:
Mytxt.appendtext ("Your text");
If you use the old method compiler will prompt: This trick is too slow, try a new bar. (appending text to a TextField using + is many times slower than using the Textfield.appendtext () method.)
The Dispatchevent method of the Eventdispatcher class accepts only one parameter: event:event. To pass arguments at the same time as broadcast events, write a class that inherits the event: TestEvent
Internal class TestEvent extends event{
code here
}
To declare an event type as a string constant:
public static Const Trace_inof:string = "Traceinfo";
The arguments and event types that will be passed are placed in the constructor
private Var _who:string;
private Var _info:string;
Public Function TestEvent (type:string,who:string,info:string) {
Super (type);//Call the constructor of the parent class Event
_who = who;
_info = info;
}
Code to broadcast events:
Public Function Dispatch (who:string,info:string): void{
Dispatchevent (New TestEvent (Testevent.trace_inof,who,info));
}
Registering Listeners:
Dispatcher.addeventlistener (Testevent.trace_inof,ontraceinfo);
To receive events and parameters:
Public Function Ontraceinfo (event:testevent): void{
var Tracetxt:textfield =
Getchildbyname ("Tracetxt") as TextField;
Tracetxt.appendtext (event.who+ "Dispatch:" +event.info);
}
What needs to be noted here is as, the new type conversion operator, which converts the Displayobject returned by Getchildbyname () to the type TextField previously declared. If the conversion fails, NULL is returned. Examples of official giving:
public var myarray:array = ["One", "two", "three"];
Trace (myarray as Array); One,two,three
Trace (myarray as number); Null
Trace (myarray as int); Null
First, the parameters of the function in AS3 can have a default value.
Public Function TestFunc () {
MyFunc ();
}
Private Function MyFunc (para1:int=10,para2:string= "str"): void{
Trace (Para1 + "," + para2); Ten, str
}
No extra parameters can be given in the AS3.
Public Function TestFunc () {
MyFunc (2, "3", 4);
}
Private Function MyFunc (para1:int,para2:string): void{
Trace (Para1 + "," + para2);
}
The compiler gave a parameter mismatch error: Argumenterror:error #1063: Argument count mismatch on Testfunc/testfunc::myfunc (). Expected 2, got 3.
So the previous method of using arguments to get the invariant parameters can not be used. AS3 gives a new keyword: ... (rest) parameter
Public Function TestFunc () {
MyFunc (2, "3", 4, "5", true);
}
Private Function MyFunc (para1:int,para2:string,... more): void{
Trace (Para1 + "," + para2); 2, 3
Trace (more); 4,5,true
}
After a fixed parameter followed by a "..." and an expression (such as "more" in the example), "..." All subsequent arguments are placed in the array named by the expression. Note "..." Must be the last parameter.
If you use the "..." arguments, you will not be able to get Arguments.callee (a reference to the currently executing function), so you can use "..." when you are certain that you are not using callee.
Referring to arguments, Arguments.caller has been "removed". To get caller, you need to pass the call function's callee as an argument to the called function. The official example:
Package {
Import Flash.display.Sprite;
public class Argumentsexample extends Sprite {
private var count:int = 1;
Public Function Argumentsexample () {
Firstfunction (TRUE);
}
Public Function Firstfunction (Callsecond:boolean) {
Trace (Count + ": firstfunction");
if (Callsecond) {
Secondfunction (Arguments.callee);
}
else {
Trace ("CALLS STOPPED");
}
}
Public Function Secondfunction (caller:function) {
Trace (Count + ": secondfunction\n");
count++;
Caller (false);
}
}
}
AS3 has a button class: SimpleButton, you can specify different displayobject for four states respectively. But SimpleButton does not inherit the Displayobjectcontainer class, that is, you cannot add other child to it. What if you want to create a Button with a text? Two scenarios:
- Plan One: add words to each state. Because Shape does not inherit the Displayobjectcontainer class, it is necessary to add a text state with Sprite. The advantage is that each state can have different text color, size, location, and so on. The disadvantage is that it is not convenient to change the text content.
- Programme two: put SimpleButton and TextField together in a Sprite. This SimpleButton state can use Shape to conserve memory space. The pros and cons are the opposite of the scenario.
Creating a button is very simple, for its four states to specify a separate displayobject on it:
BTN = new SimpleButton ();
Btn.name = "BTN";
Btn.downstate = new BtnStatusShape2 (downcolor,w,h);
Btn.overstate = new BtnStatusShape2 (overcolor,w,h);
Btn.upstate = new BtnStatusShape2 (upcolor,w,h);
Btn.hitteststate = btn.upstate;
AddChild (BTN);
Note You must specify Hitteststate, which is the hit frame in the Flash IDE that creates the button, and the area that responds to the mouse event, and the button loses its effect without it. Generally set it to be the same as upState.
Each state of the second scenario is a Shape (→displayobject→eventdispatcher→object):
Internal class BtnStatusShape2 extends shape{
Public Function BtnStatusShape2 (bgcolor:uint,w:uint,h:uint) {
Graphics.linestyle (1,0x000000,0.8)
Graphics.beginfill (bgcolor,0.8);
Graphics.drawroundrect (0,0,w,h,8);
Graphics.endfill ();
}
}
Plan one has nothing to say. Scenario II If you want BTN to respond to mouse events, you can override the AddEventListener method that loads its Sprite:
public override function AddEventListener (type:string, listener:function, Usecapture:boolean = false, Priority:int = 0, u Seweakreference:boolean = False): void{
Btn.addeventlistener (Type,listener);
}
To override a method inherited from a parent class, you must use the Override keyword. And the overridden method must have the exact same parameter name, number, and type of the parent class method.
Of course, program II can also be monitored mouse_over, mouse_out, CLICK and other mouseevent to change the text display of different states. Other content Reference class code .
AS3 support label, jump out of multi-layer loop can write this:
Outerloop:for (var i:int = 0; i < i++) {
for (var j:int = 0; J < J + +) {
if ((i = = 8) && (j = = 0)) break Outerloop;
Trace (Ten * i + j);
}
}
When you AS2, you can only add a variable to judge:
var Needbreak:boolean = false;
for (var i:number = 0; i < i++) {
for (var j:number = 0; J < J + +) {
if ((i = = 8) && (j = = 0)) {
Needbreak = true;
Break
}
if (needbreak) break;
Trace (Ten * i + j);
}
}