As3 Summary (5)

Source: Internet
Author: User

Today I am watching as3 game programming and seeing something. It's good. Let's summarize it.

1. When the Int or uint variable type is available, do not use the number type, so the processing speed will be faster. If
If you define hundreds of numbers in the array, you will find that the video runs quite slowly. Besides
Even worse, undefined variables are used, which occupy a large amount of memory overhead during runtime. Also note that when creating
When using Sprite for video editing, do not use movieclip.

2. It is definitely a good habit to process objects correctly after completing flash production. For example, when you create a shooting game, there will be hundreds of bullets in one minute, and they will remain in the memory after they leave the screen. To migrate
In addition to these objects, You need to delete all objects in the variables or arrays and remove the display list using the removechild method.

3. Is the class extension type correct?
As shown in the following code:
Public class myclass extends sprite {
The extension type here is sprite or movieclip. It mainly depends on the number of frames in the main timeline of the video. If it is a single
Frame, Sprite is used, and movieclip is used for multiple frames.

4. Be case sensitive.
When naming variables and functions, be sure to be case sensitive. For example, myvariable and myvariable
Is two completely different variables. Similarly, if you name a myclass class, the software
The constructor named myclass will be automatically called. If you accidentally write this function name as myclass,
It will not be called during initialization.

5. Do you need to disable the shortcut key?
When you use keyboard input to test a video, you may find that some buttons do not respond. This is because the current test
These shortcut keys are required for the test environment. In other words, if you want to publish a video to a webpage, You need to customize the shortcut key.
Select "control"> "Disable shortcut keys.

6. In the flash tag of the release settings, there is a local access security setting, you can choose to access
Access through the network. To ensure the security and reliability of Flash videos, you need to select one of them based on the actual situation.

7. Application of video editing
If you have a video clip in the library and want to add it to the stage, there are two options:

7.1 first drag the video clip from the library to the stage and take an Instance name on the attribute panel, for example
Myclipinstance, then you can use the code to control the attributes of this video clip on the stage. Generation below
Code defines the coordinates of the video clip as (300,200)
Myclipinstance. x = 300
MySQL instance. Y = 200

7.2 operate with pure code. First, select a video clip in the library, right-click it, and choose link> link.
In the dialog box, select "Link"> "export as ActionScript" and set a class name, for example, mascot (note,
The first letter of the class name is generally in upper case ). Now we can add this video clip in the library to the stage in the form of pure code.
In this case, we declare a variable as an instance of this object, and then add it to the display list with addchild. Generation
The Code is as follows:
VaR mymovieclip: mascot = new mascot ()
Addchild (mymovieclip)
Because we have not set any attributes for this video clip, it is displayed at the (0, 0) Position on the stage by default. Me
You can use the X and Y attributes of this instance to set its coordinates, and use the rotation attribute to set the Rotation Degree of this object.
Quantity
VaR mymovieclip: mascot = new mascot ()
Mymovieclip. x = 275
Mymovieclip. Y = 150
Mymovieclip. Rotation = 10
Addchild (mymovieclip)
Adding video clips to the stage with the above Code seems complicated, but the code can be easily generated.
Multiple copies of this video clip and add them to the stage. Next we will generate 10 clips for the mascot video clip.
The object is horizontally arranged at an interval of 50 pixels from left to right and scaled to 50%. The Code is as follows:
For (VAR I = 0; I <10; I ++ ){
VaR mascot: mascot = new mascot ()
Mascot. x = 50 * I + 50
Mascot. y= 300
Mascot. scalex =. 5
Mascot. scaley =. 5
Addchild (MASCOT)
}

8. Button Creation
The following describes how to create a button.

8.1 Method 1: (edit a video into a button)
Make a video clip into a clickable button. First, you need to use the previous knowledge to add the video clip to the dance
And register a listener that accepts mouse events. The following code calls the library class named mascot
Video Clips are placed at (100,150) the stage.
VaR mymovieclip: mascot = new mascot ()
Mymovieclip. x = 100
Mymovieclip. Y = 150
Addchild (mymovieclip)
To register this listener, you need to use the addeventlistener () Listener function, including
Type. This type is a constant. For example, movieevent. Click will respond to a mouse click event.
A function for processing response events
Mymovieclip. addeventlistener (mouseevent. Click, clickmascot)
Function clickmascot (Event: mouseevent)
{
Trace ("You clicked mascot ")
}
When testing the code, the clickmascot function sends an event to the output window. However, in most cases
In this case, we need to make the video clip look more like a button. This is where the buttonmode attribute is used. Set it
If this parameter is set to true, when you move your mouse over the video clip, the arrow of the mouse turns into a hand shape.
Mymovieclip. buttonmode = true

8.2 Method 2: (add the button to the stage)
Of course, you can also create a button instance, just like our operations on video editing. Here
The name of the button link class is librarybutton.
VaR mybutton. librarybutton = new librarybutton ()
Mybutton. x = 450
Mybutton. Y = 100
Addchild (mybutton)
The biggest difference between the form of this button and the previous video clip is that you can double-click the button to see the button on the timeline.
There are four different frames. The first frame is the State displayed before the mouse is crossed; the second frame is the State displayed when the mouse is passed;
Three Frames are displayed when the mouse is pressed but not released. The last frame is the button clicking area, which is
Is invisible. Next, you can add a listener for this button. The Code is as follows:
Mybutton. addeventlistener (mouseevent. Click, clicklibrarybutton)
Function clicllibrarybutton (Event: mouseevent)
{
Trace (you clicked the library button !)
}

8.3 method 3:
Create with simplebutton type. In this case, you need four video clips of the same size. The link class name is
Buttonup, buttonover, buttondown, buttonhit, and then use the simplebutton Construction Method to pass the four video clips to the simplebutton instance. The Code is as follows:
VaR mysimplebutton: simplebutton = new simplebutton (New buttonup (), new buttonover (), new
Buttondown (), new buttonhit ())
Mysimplebutton. x = 450
Mysimplebutton. Y = 250
Addchild (mysimplebutton)
You can also add a listener function.
Mysimplebutton. addeventlistener (mouseevent. Click, clicksimplebutton)
Function clicksimplebutton (Event: mouseevent)
{
Trace (you clicked the simple button !)
}

9. Draw hyperlink text

The simplest method is to use the htmltext attribute to add HTML code, for example
VaR myweblink: textfield = new textfield ()
Myweblink.html text = "click Next to access my blog <a href = 'HTTP: // xiaocui.blogbus.com '> flash
Script learning </a>"
Addchild (myweblink)
Test the above code and we found that the link can be used normally. We want to see the following: What should I do if I want to underline the link and change the font color? Let's look at the following code:
VaR mystylesheet: stylesheet = new stylesheet ()
// Note that the color value format is # ffffff
Mystylesheet. setstyle ("A", {textdecoration: "underline", color: "# 0000ff "})
VaR myweblink: textfield = new textfield ()
Myweblink. width = 300
Myweblink. stylesheet = mystylesheet
Myweblink.html text = "click Next to access my blog <a href = 'HTTP: // xiaocui.blogbus.com
Script learning </a>"
Addchild (myweblink)
In addition, we do not need to link to the webpage window, but can also use the method of listening, for example:
Mylink.html text = "Click <a href = 'event: Testing '> here </a>"
Addeventlistener (textevent. Link, textlinkclick)
Function textlinkclick (Event: textevent)
{
Trace (event. Text)
}

10. Set layer depth

The setchildindex method allows you to move the position of the display object in the display list up or down. You can display the list
Consider it as an array. Its Index position starts from Layer 3. If you create three display objects
The location is layer 0, 1, and 2. The second layer objects are outside, and the second layer objects are in the innermost layer.
You can use
Setchildindex (mymovieclip, 0)
After this statement is executed, the remaining objects will be automatically upgraded to a layer, and their index locations are added with 1.
To 0th layer elements moved to 1st layer, 1st layer elements moved to 2nd layer ......
To move a display object to the top of all objects, the numchildren attribute is used.
Is the number of objects displayed in the container. For example, a container now has three display objects. The value of numchildren is 3, and the Level list is Layer 1 and Layer 2, respectively. The depth of the outermost layer is the numChildren-1 layer.
Setchildrenindex (mymovieclip, numChildren-1)
To better understand the depth of the setup layer, the authors in the book provide us with an instance of settingspritedepth. fla,
Use code to generate three copies of the circle and place them on the stage. You can click any circle to make it
Display on the outermost side (set the layer depth to the highest)

11. Target indicates the Display object of the event, rather than the container. currenttarget indicates the current listener.
Component nodes are often containers. Only when the Display object for adding event listening and the display object for event occurrence are the same,
Currenttarget is equal to the target. In this case, currenttarget may be a non-container Display object. Then, we can use this code. When the mouse clicks on the circular shape, event.tar get indicates the circular display.
And event. currenttarget indicates the container of the circular Display object. Obviously, only containers can
Stores display objects. If target is used, the system reports an error.

12. keyboard input

During the game design process, we don't have to worry about whether the key is pressed or not. More importantly, this key is always pressed. Ratio
For example, how can we control the auto throttling operation through the upward arrow key during the driving game. In this case, you need
We need to set a Boolean value for the key. The value is true when the key is pressed, and false when the key is popped up. At any time
You need to check this Boolean value to see if the keyboard is pressed.

13. text input

The textfield object has one type of input text, which is different from static text and dynamic text.
And you can enter the content in it.

The text created in this way is hard to find in the upper left corner of the screen, and there is no border. We can use the textformat attribute to do this.
Some improvements.
The following code sets the font to 12, with the position (200), height 18, width, and border
VaR inputformat: textformat = new textformat ();
Inputformat. font = 'arial ';
Inputformat. size = 12;
VaR myinput: textfield = new textfield ();

Myinput. type = textfieldtype. input;
Myinput. defaulttextformat = inputformat;
Myinput. x = 10;
Myinput. Y = 10;
Myinput. Height = 18;
Myinput. width = 200;
Myinput. Border = true;
Addchild (myinput );
Stage. Focus = myinput;
The code in the last line sets the text input cursor to be in the text area.

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.