[As3] [Notes] [reprint + collection]

Source: Internet
Author: User

Some less common code writing methods for mutual learning. Program code:
1,

MC. gotoandplay (math. Random () * 10> 0 );

Math. Random () * 10: obtain a random number with a decimal number less than 10.
>>Yes shift symbol> 0 removes the number of decimal places !! (This method is indeed brilliant)
Therefore, math. Random () * 10> 0 indicates obtaining a random integer of less than 10!

-------------------------------------------

Promotion ideas
In the loading progress bar, trace (number of loaded Bytes/total number of bytes} * 100> 0 + "%") is really convenient!

2,

If (I & 1 ){}

I & 1 indicates whether I is an odd or even number;
If it is an odd number of I, the binary representation indicates that the last digit is 1, And I & 1 = 1 is true;
The binary system of even I indicates that the last digit is 0, And I & 1 = 0 is false.

3,

VaR A: array = new array ();
For (var I: Int = 0; I <10; A. Push (I ++ )){
//....
}

After this loop is executed, the values of array a are 0, 1, 2, 3, 4, 5, 6, 8, 9.
There are many ways to simplify a program .. Specific analysis ..

4,

VaR temp: Number = mousex;
Stage. addeventlistener (mouseevent. mouse_move, movehandler );
Function movehandler (E: mouseevent): void {
VaR D: Number = (-Temp + (temp = mousex ));
}

This code function is used to obtain the distance between the mouse and the D. It's really nice to drag the form or the game with the mouse !!! The code can be changed:

Mouse. Hide ();
VaR temp: Number = mousex;
Stage. addeventlistener (mouseevent. mouse_move, movehandler );
Function movehandler (E: mouseevent): void {
VaR D: Number = (-Temp + (temp = mousex ));
Spritemouse. x + = D;
E. updateafterevent ();
}

In this way, the video clip spritemouse becomes a mouse.

5,

Function fn1 (){
Trace ("1 ");
}
Function FN2 (){
Trace ("2 ");
}
([Fn1, FN2] [math. Random () * 2> 0]) ();

This is a random function execution command, which is quite interesting.

For example, a welcome page:

Function fn1 (){
Trace ("you're welcome ");
}
Function FN2 (){
Trace ("don't go here ");
}
Function fn3 (){
Trace ("How did you come ");
}
Function FN4 (){
Trace ("I guess you are coming ");
}
([Fn1, FN2, fn3, FN4] [math. Random () * 4> 0]) ();

6,

VaR A: Int = 3;
If (a> 0 & (trace ("A is positive number"), A & 1 ))
Trace ("A is positive odd number ");

First, execute a> 0, then trace ("A is positive number"), and finally judge whether a is an odd number. Here, the comma expression is executed from left to right.

This usage is mainly used to determine whether a statement is executed somewhere.

7,

VaR A: Int = 13;
VaR B: Int =-12;
If (a ^ B)> 0 | (B * =-1, A> 10 ))
Trace (B );

It is also a comma expression:
A ^ B =-7
B * =-1 B = 12
A> 10
Output B = 12
This usage is inexplicable and I really don't know where to use it.

8,

Mymc. buttonmode = true;

Previously in As2, as long as you specify onrelease or onrolover for MC and so on, when you move the mouse over Mc, the mouse will become a hand shape, no need to worry about it. When this rule is changed in as3, you must first use addeventlistener to add events. After adding events, moving the cursor over Mc remains the default arrow shape. To change this shape, you also need to specify the buttonmode attribute of MC to true.

9,
Shift
A = 1, B = 2, c = 4, D = 8
Answer & 1 = 1; // choose
Answer & 2 = 2; // choose B
Answer & 4 = 4; // Choose C

Answer & 8 = 8; // choose d

 

10,

Access the variables on the main timeline:

Movieclip (Root). Variable

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.