ActionScript 3.0 basic tutorial 2-display list and display objects

Source: Internet
Author: User
Author: egoldy nature: original reads: 14486 published at: 17:22:36

One of the biggest changes in ActionScript 3.0 is the rendering and display of visual content by flash. now we call it display list and Display object in ActionScript 3.0. you may be used to the Flash Rendering and display content method before flash 8, but if you want to transfer to actionscrpt 3.0 programming, it is necessary to understand the display list and Display object. We will discuss this content below.

What are display list and Display object? 

Display list refers to the hierarchical relationship of the displayed content in flash. A display object is a content element in a hierarchy. As shown in:

In addition to the display objcet we see, we also see the Display object container. Literally, we can understand that it is the container of the display object. The root of any flash program is a display object container. Next we will analyze the display list.

Stage ---- it contains all the content elements in flash. Equivalent to the main timeline
Instance of the main class of the SWF file --------- the instance of the main class. Once the main class is instantiated, the display object is immediately displayed on the stage (main timeline. It is equivalent to the main class we entered in the document class input box on the flash9 property panel.
Display object --- various types of visual content in flash.
Display object container-Display object container.
From the relationship, you can add the Display object or other Display object container as its sub-content in the display object container. However, the display object cannot be added with other content as sub-content. At the same time, you can display the Display object, or disable it, or move it in different Display object container. You do not need to worry that the New Display object will be replaced. This is very similar to the depth management we used earlier.
Note that both the Display object and Display object container are abstract classes in Flash 9. You cannot instantiate them directly, such as new displayobject (). Otherwise, an error will be thrown in flash.
So far, you may want to ask where are the well-known texts, graphics, and moiveclip? Let's analyze the Display object in detail.

The content referred to in the display object is described above. From top to bottom.
Avm1movie ---- a video file generated in versions earlier than flash9.
Bitmap ------ our commonly used bitmap
Interactiveobject --- interactive object. For example, samplebutton, textfield. And displayobjectcontainer.
Morphshape ---- animation. Note that deformation animation can only be created in Flash IDE.
Shape ----- graph.
Statictext-static text, like morphshape, can only be created in Flash IDE.
Video-video

Interactiveobject in the display object. It refers to the interactive object. It can be a button, interactive text can also be displayobjectcontainer. For example, loader, Sprite, And the last layer of stage is our most familiar moiveclip.
Sprite ---- it is very similar to movieclip, it is equivalent to a movieclip without a timeline, and moiveclip is as well known as, you can control the content of it through code for playing. Finally, moiveclip is the most powerful.

Create a display object 

In actionscrpt 2.0, when we create a movieclip object in the scenario, we usually use the following method.
Createemptymovieclip () ---- create an empty video clip.
Createtextfield () ------ create a text field.
In ActionScript 3.0, we create instances by constructing them. As follows.
New moiveclip () ---- creates an empty video clip.
New sprite () ---- create a new empty Sprite.
New textfield () ---- create a new empty text field.
New shape () ------ create a canvas
Processing Methods for existing components in the library are as follows:
In ActionScript 2.0, we usually name the component in the library as a link ID, and then use the attachmovie () method to add it to the scenario. In ActionScript 3.0, we still need to specify other IDs, but this ID is used as the name of a class, as shown in:

Note that the title is class rather than identifier. when you specify a class name in ActionScript 3, this class can or can not be used. If you have not compiled a class named Star. when the as class is compiled, flash will automatically create one for you. Next, you still need to create an instance using new star () in the code. This method is actually attachmovie () in actionscrpt 2.0 ().
After a display object is created, it is not displayed. You need to add it to the display list before it is displayed. Use the addchild () method in ActionScript 3.0.

Display object 

After a display object is created, add it to the display list to display it. Use the addchild method to display it. Of course, you can also display the displayed object, remove it from the display list, for example, removechild. A simple example is provided.
Open flash9, create an ActionScript. As file, and save it as circleshape.. Write the following code:

Package {import flash. display. sprite; import flash. display. shape; public class circleshape extends sprite {public function circleshape () {// create the shape object var mycircle: Shape = new shape (); mycircle. graphics. beginfill (0xff0000, 1); mycircle. graphics. drawcircle (200,150,100 );}}}

Create a new flash file and save it as circleshape. fla. then, enter the class name circleshape in the document class input box of the main scenario attribute and perform the test. After the test is complete, you may not see any content. The reason is very simple. It does not add the created shape object to the display list, so we only need to add a line of code addchild (mycircle. As follows.

package {    import flash.display.Sprite;    import flash.display.Shape;    public class CircleShape extends Sprite    {        public function CircleShape()        {            var myCircle:Shape = new Shape();            myCircle.graphics.beginFill(0xFFcc00, 1);            myCircle.graphics.drawCircle(200, 150, 100);            addChild(myCircle);                    }    }} 

Save your changes and test the circleshape. FLA file again. Then you can see the effect. As follows.

Next, we plan to use the attachmovie method similar to as2.0 in ActionScript 3.0, which is described earlier. In the following example, We will test whether sub-content can be added to the Display object.
On the basis of the previous file, save it as circleshape2.fla. in the home scene of flash, modify the document class to circleshape2 on the attribute panel to open the original circleshape. as file, save it as circleshape2.as, we just need to modify circleshape2.as. Next, we will draw a star in the scene, and the class name specified in the library is star.
As shown in:

We calculated to add this star sticker to the plot that has been drawn. Open the circleshape2.as file and add some code.

Package {import flash. display. sprite; import flash. display. shape; public class circleshape2 extends sprite {public function circleshape2 () {var mycircle: Shape = new shape (); mycircle. graphics. beginfill (0xffcc00, 1); mycircle. graphics. drawcircle (200,150,100); addchild (mycircle); // Add code to construct the star instance var mystar = New Star (); mystar. X = 150; mystar. y = 100; // Add star to mycircle. addchild (mystar );}}}

Save the file and return to the scenario circleshape2.fla. Test the film. You will see an error message on the output panel.

Referenceerror: Error #1069: The addchild attribute cannot be found on flash. display. shape, and there is no default value.
At circleshape2 $ iinit ()
There is no addchild attribute on flash. display. shape, which means you cannot add sub-content to the shape. As mentioned in the analysis illustration above, the display object and the display object container cannot be added as sub-content of the display object. The Display object container can be used. Then we will try to replace it with the Display object container. We will use sprite to change the code to the following.

Package {import flash. display. sprite; import flash. display. shape; public class circleshape2 extends sprite {public function circleshape2 () {var mycircle: SPRITE = new sprite (); mycircle. graphics. beginfill (0xffcc00, 1); mycircle. graphics. drawcircle (200,150,100); addchild (mycircle); // display the star in the library on the screen. VaR mystar = New Star (); mystar. x = 150; mystar. Y = 100; mycircle. addchild (mystar );}}}

We replace var mycircle: Shape = new shape () in the Code with VAR mycircle: SPRITE = new sprite (). Then we test the film again and you will find it succeeded. The star has been successfully added to mycircle as its sub-content. As follows.

Here, we also need to pay attention to the method of displaying stars on the screen from the library. We use New Star () to construct an instance, and then add it to the display list using addchild.

PS by egoldy: In this tutorial, we will focus on understanding the relationship between display list. Display object and Display object container through diagrams. In addition, when you create a display object in ActionScript 3.0, you can directly construct an instance, such as new movieclip (), new shape (), new sprite (), note that both the Display object and displayobjectcontainer are abstract classes.
Displayobject () to instantiate it. When a component in the library is displayed in a scenario, the first is to specify the link class name for it. In actual operations, if there is no link to the external class, the system will generate it dynamically, second, when the screen is pasted, the new star () method is still used to construct the instance, and addchild is used to add it to the display list.

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.