Import and Export components for ActionScript

Source: Internet
Author: User

By default, the video clip element instance in the Flash document library cannot be created dynamically (that is, it is created only using ActionScript ). This is because every component exported for use by ActionScript will increase the size of the SWF file, and it is well known that some components may not be suitable for use on the stage. Therefore, to enable the component to be used in ActionScript, you must specify as ActionScript to export the component.

Export components for the ActionScript file:
    1. On the "library" Panel, select the component and open its "component properties" dialog box.

    2. Activate "advanced" settings if necessary.

    3. In the "Link" section, activate the "export as ActionScript" check box.

      This will activate the "class" and "base class" fields.

      By default, the "class" field is filled with the component name with spaces deleted (for example, the component named "Tree House" is changed to "Treehouse "). To specify that the component uses a custom class for its behavior, enter the full name of the class in this field, including its package. If you want to create an instance for this component in ActionScript without adding any other behavior, you can keep the class name unchanged.

      The default value of the "base class" field isFlash. display. movieclip. If you want the component to extend the functionality of another custom class, you can specify the name of this class to replace this value, as long as this class extends the sprite (or movieclip) class.

    4. Click OK to save the changes.

      In this case, if Flash cannot find an external ActionScript file that contains the definition of the specified class (for example, if you do not need to add other actions for the component), a warning is displayed:

      This class definition cannot be found in the class path. Therefore, the corresponding definition is automatically generated in the SWF file during export.

      This warning message can be ignored if the library component does not need a unique feature that exceeds the movieclip class function.

If no class is provided for the component, flash creates a class equivalent to the following class for the component:

Package {import flash. display. movieclip; public class examplemovieclip extends movieclip {public function examplemovieclip (){}}}

If you want to add additional ActionScript functions to the component, go downCodeAdd the corresponding attributes and methods to the structure. For example, assume that there is a video clip element that contains a circle of 50 pixels wide and 50 pixels high, and use the class named circle to specify as ActionScript to export this element. The following code expands the movieclip class after the circle. As file, and provides additional methods for this componentGetarea ()AndGetcircumference ():

Package {import flash. display. movieclip; public class circle extends movieclip {public function circle () {} public function getarea (): number {// The formula is pi times the radius squared. return math. pI * Math. pow (width/2), 2);} public function getcircumference (): number {// The formula is pi times the diameter. return math. pI * width ;}}}

The following code is placed on the 1st key frame of the flash document to create an instance of the component and display the instance on the screen:

 
VaR C: Circle = new circle (); addchild (c); trace (C. width); trace (C. height); trace (C. getarea (); trace (C. getcircumference ());

This Code demonstrates that the ActionScript-based instantiation can be used as an alternative to dragging a single resource to the stage. The circle it creates has all the attributes of the video clip and the custom method defined in the circle class. This is a very simple example-your library component can specify any number of attributes and methods in its class.

the instantiation Function Based on ActionScript is powerful because it allows a large number of instances to be dynamically created. Manual creation is a heavy task. It is also flexible because you can customize the attributes of an instance when creating an instance. You can use multiple cycle instances to dynamically create multiple circle instances. If the above circle components and classes exist in the Flash document library, place the following code on the 1st frame key frame:

Import flash. geom. colortransform; var totalcircles: uint = 10; var I: uint; for (I = 0; I <totalcircles; I ++) {// create a new circle instance. vaR C: Circle = new circle (); // place the new circle at an X coordinate that will space the circles // evenly initialize ss the stage. c. X = (stage. stagewidth/totalcircles) * I; // place the circle instance at the vertical center of the stage. c. y = stage. stageheight/2; // change the circle instance to a random color C. transform. colortransform = getrandomcolor (); // Add the circle instance to the current timeline. addchild (c);} function getrandomcolor (): colortransform {// generate random values for the red, green, and blue color channels. vaR RED: Number = (math. random () * 512)-255; var Green: Number = (math. random () * 512)-255; var Blue: Number = (math. random () * 512)-255; // create and return a colortransform object with the random colors. return new colortransform (1, 1, 1, 1, red, green, blue, 0 );}

This Code demonstrates how to use the code to quickly create and customize multiple instances of components. Each instance is located based on the current count in the loop, each instance obtains a random color by setting the transform attribute (circle inherits this attribute by extending the movieclip class.

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.