As3 calls external resource files (swf files and image files)

Source: Internet
Author: User

I. Objectives

1. Use bb.swf in aa.swf.

2. After bb.swf has a dynamic text txt1,aa.swf called, change the display content of this text

 

 

Step 2

Generate the bb.swf File

1. Create bb. fla and add a dynamic text on it. The name is "txt1 ". The initial display of txt1 is "hello"

2. Press Ctrl + Enter to run bb. fla and the bb.swf file will be generated.

 

Call bb.swf

1. Create aa. fla and add the code as shown in the timeline.

Ch ();

Function ch (){
// Call the swf File
Var lr: LoadResource = new LoadResource('bb.swf ', 120,200 );
LR. addeventlistener (event. Complete, CC );
Addchild (LR );

// Call the image part
// Var LR: loadresource = new loadresource ('HTTP: // img1.gtimg.com/gamezone/pics/21519/21519683.jpg',-1,-1 );
// LR. addeventlistener (event. Complete, CC );
// Addchild (LR );
}
Function CC (E: Event ){
// Change the attributes of the MC object in the swf File
Var mc: MovieClip = movi((e.tar get. content );
Mc.txt 1. text = 'bbbbb ';

// Change the display attribute of the image
// E.tar get. content. height = 100;
// E.tar get. content. width = 100;
// E.tar get. content. alpha =. 5;
}

 

Note: The call part of the image is commented out in the code above. If you need to test the call of the image, you can open it on your own.

 

 

Iii. General call Resources

Package {
Import flash. display. Bitmap;
Import flash. display. BitmapData;
Import flash. display. Loader;
Import flash. display. MovieClip;
Import flash. display. DisplayObject;
Import flash. events .*;
Import flash.net. URLRequest;

// Load resources (swf jpg png gif)
Public class LoadResource extends MovieClip {
Private VaR _ URL: string; // URL to be requested
Private VaR _ Height: int; // Mc height
Private VaR _ width: int; // Mc width

// Content loaded this time (Note: 1. to throw an event. after the complete event, 2. when the SWF file is loaded, this variable is movieclip; when the image is loaded, this variable is Bitmap)
Public var content: displayobject;

Public Function loadresource (URL: String, H: int, W: INT ){
_ Url = url;
_ Height = h;
_ Width = w;
ConfigureAssets ();
}

Private function configureAssets (): void {
Var loader: Loader = new Loader (); // flash. display. Loader is used here, not URLLoader.
Loader. contentLoaderInfo. addEventListener (Event. COMPLETE, completeHandler );
Loader. contentLoaderInfo. addEventListener (IOErrorEvent. IO_ERROR, ioErrorHandler );

Var request: URLRequest = new URLRequest (_ url );
Loader. load (request );

AddChild (loader );
}

Private function completehandler (E: Event ){
Content = e.tar get. content;

// Set the width and height of the returned object
If (_ height! =-1 ){
Content. Height = _ height;
}
If (_ width! =-1 ){
Content. width = _ width;
}

Dispatchevent (E); // throw an "event. Complete" event so that the call layer can perform some additional processing.
}

Private function ioerrorhandler (Event: ioerrorevent): void {
Trace ("unable to load image:" + event. Text );
}
}
}

 

 

4. Others

Data Types loaded by Loader:
Loader is used to replace the loadMovie function of the original MovieClip and load external image files and SWF files.

* If an image file (jpg, gif, or png) is loaded, the Data Type of Loader. content is a Bitmap object;
* If the SWF file (flash 9) is loaded, the Data Type obtained by Loader. content is MovieClip;
* If the SWF file (earlier than flash 9) is loaded, the Data Type of Loader. content is AVM1Movie;

 

 

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.