Use flash as to load external image code

Source: Internet
Author: User
Tags addchild imageclip require

Let's take a look at how to use as to load external images. Let's take a look at the tutorial below.

The improvement in AS3 is how you load external content to your application. Some new changes make it easier for you to download progress-things are always A bit challenging A S2 world.

In this article, I will describe how to load an external image to the application Flash release instructions for video editing. Here is an example. You will create this tutorial near the end:

[The image was taken from the city and previously called kirupaVille! ]

In the above example, when you run the image, the SWF stored in the same folder is loaded and displayed. I know this is hard to see, but don't worry, because you will be able to show this very fast on your own computer.

Let's get started.
The following steps help you create a small application to load an image file from an external location:

First, create a new Flash release description application and save the application loadimage.

For this article, the size of the stage, background color, and so on are not important, so that you can leave everything at any time in the default settings, if you want.
Let's add a video clip to pack your host image. To insert

Once everything is set, close this dialog box by pressing OK and create a new ImageClip video clip icon.

Now, you will edit the newly created ImageClip. We really don't want to do anything here, so we exit ImageClip and return to scenario 1. Click the link navigation bar in the first session.

You should go back to your main scenario and browse it once. Now we have created a new icon of ImageClip, but we have not really joined our site. From the library, drag and drop your ImageClip video clip to your scenario:

Once you insert ImageClip to your scene, the small circle outside of the things you should not see represents your empty video clip:

[An empty video clip consists of a hollow circle]

Make sure that the blank circle indicates that ImageClip is selected, and from the property panel, let your selection ImageClip, such as the name imageArea

In the past, you said that the name of an empty video clip instance is imageArea to your site. This video clip will be responsible for displaying your image load. Talking about pictures...

Find an image
Be sure to save the image to the same location your SWF publishing location-by default, your f la in the same folder. If you cannot find an image quickly, save the following image at any time. I use my example kirupaVille:

In the past, you said that the name of an empty video clip instance is imageArea to your site. This video clip will be responsible for displaying your image load. Talking about pictures...

Find an image
I saved the image pixelhouses.jpg. You can find its right next to my loadimage FLA file:

You will see why I emphasize the saved image in the same location as your FLA and output when I tell you the code.

Add Code
The next step is to add some code. Your time should only have one layer called layer 1st and layer 1st that will have a key frame. Right-click the key frame and select the action from the displayed menu:



[Right-click your key frame and select Action]

The operation window appears. In this window, copy and paste the following code:
Var imageLoader: Loader;
 
Function loadImage (url: String): void {
// Set properties on my Loader object
ImageLoader = new Loader ();
ImageLoader. load (new URLRequest (url ));
ImageLoader. contentLoaderInfo. addEventListener (ProgressEvent. PROGRESS, imageLoading );
ImageLoader. contentLoaderInfo. addEventListener (Event. COMPLETE, imageLoaded );
}
LoadImage ("pixelHouses.jpg ");
 
Function imageLoaded (e: Event): void {
// Load Image
ImageArea. addChild (imageLoader );
}
 
Function imageLoading (e: ProgressEvent): void {
// Use it to get current download SS
// Hint: You cocould tie the values to a preloader :)
}

In the above code, please note that the name image file that I walk through, I want to load:

Var imageLoader: Loader;
 
Function loadImage (url: String): void {
// Set properties on my Loader object
ImageLoader = new Loader ();
ImageLoader. load (new URLRequest (url ));
ImageLoader. contentLoaderInfo. addEventListener (ProgressEvent. PROGRESS, imageLoading );
ImageLoader. contentLoaderInfo. addEventListener (Event. COMPLETE, imageLoaded );
}
LoadImage("PixelHouses.jpg");
 
Function imageLoaded (e: Event): void {
// Load Image
ImageArea. addChild (imageLoader );
}
 
Function imageLoading (e: ProgressEvent): void {
// Use it to get current download SS
// Hint: You cocould tie the values to a preloader :)
}

Make sure to change the name of the image file, one to load. Once you do this, press Ctrl + Enter

Var imageLoader: Loader;

In this line, I announce a new object, its so-called imageLoader loader. First-class loaders allow you to load image-based content, such as images, Papua New Guinea, and GIF, and provide many convenient methods so that you can easily achieve this. You will see some people in our code very quickly.

// Set properties on my Loader object
ImageLoader = new Loader ();
ImageLoader. load (new URLRequest (url ));
ImageLoader. contentLoaderInfo. addEventListener (ProgressEvent. PROGRESS, imageLoading );
ImageLoader. contentLoaderInfo. addEventListener (Event. COMPLETE, imageLoaded );
}

In the next line, I start our loadImage method. This method requires the value of a URL to be encoded as a string, and its return type is invalid. In other words, it does not return a value.

Function loadImage (url: http://www.111cn.net/flash_a/112/String): void {
// Set properties on my Loader object
ImageLoader = new Loader ();
ImageLoader. load (new URLRequest (url ));
ImageLoader. contentLoaderInfo. addEventListener (ProgressEvent. PROGRESS, imageLoading );
ImageLoader. contentLoaderInfo. addEventListener (Event. COMPLETE, imageLoaded );
}

The next two lines are interesting! I have created two event listeners that require proper event handler when launching. These two events are ProgressEvent. PROGRESS and Event. COMPLETE.

Because your image is downloaded, you can download more images each time. This allows you to have an accurate snapshot of how much your data has been downloaded and how much is left. Once all your data has been downloaded, your complete event will let you know this.

When an event is initiated, an event handler is notified. An event handler is a fantasy name method. The requirement for this event handler is improved by the imageLoading event, and the requirement for the event handler to get the complete event is imageLoaded. The bundled activity and event handler are taken care of by the addEventListener.

Note that I did not require the addEventListener method to register the relationship between your activities and event handlers in your imageLoader object itself. Instead, I add a property named contentLoaderInfo. The reason is that contentLoaderInfo returns the LoaderInfo object, providing you with everything you need to register events. This is your default loader object, imageLoader. Do not do this.

In this way, we are working with our loadImage method. We can start with this application in the most important code line-a practical start.


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

LoadImage ("pixelHouses.jpg ");
In this line, I call it the loadImage method and use the file that it looks like to our image. Since the image is in the same directory as my FLA and SWF files, I just reference the file, but it is actually a relative path image. If your image is stored in an image folder, your argument loadImage will be:

LoadImage ("image/pixelHouses.jpg ");
In addition to the details, this call sets out that you need to attach your image to everything in the motion. This requires loadImage to create two events for your loader object and listener to handle the progress and completion events.


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

Function imageLoaded (event): Invalid (
// Load the image
ImageArea. addChild (imageLoader );
)
This method is the complete event handled by our event you once. Please note that it arguments for object type events. You don't have to worry about passing event objects. However, in China, this value is the complete event you live in.

In any case, the important line of code is to participate in where I call the addChild method on our video clip, imageArea. Please note that the addChild method passed to us is our loader object imageLoader. This is the source code of this line. It is responsible for displaying the video clips loaded into your imageArea and processing the complete events in it, we must ensure that our images are completely downloaded only after they are displayed.


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

Function imageLoading (English: ProgressEvent): Invalid (
// Use it to get the current download progress
// Tip: preloader for values that you can work :)
)
Earlier on, we covered the imageLoaded method, which is a complete event for Event Processing. What you see now is the event processing program for our progressive activities. This method is required by imageLoading for every Progress event. If you want to create a preloader, you will specify it here. Since this is a big question, I will save the discussion later.

All actions surrounding it
It indicates that it is not fast! This is a massive workload of about 10 lines of code. In general, as you can see, this is very simple to load external images to your Flash application. The main thing is to create your own loader object and fill its load method with the path image of the URLRequest to load.

Your loader object is only responsible for downloading images to the memory. You can register an event in its contentLoaderInfo property to find out how many images have been loaded, or when the image has been downloaded. Once your image has been downloaded, you just need to call the addChild method of your video clip and it is passed to your loader object.

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.