Package SWF files as resources

Source: Internet
Author: User
Tags export class

Developing web games with flash will inevitably deal with various art resources. For static resources, there are various images. For dynamic resources, you can consider making SWF directly. The art resources produced into SWF can be divided into two types: one is to directly list the key frames on the main timeline, after the program uses the loader class to load the SWF file, addchild can be directly added to the display list. The other is to encapsulate the art resources into a movieclip and export the class as3, in this way, art resources can be created in a program similar to new XXX.

However, this method of using SWF has a major drawback, that is, it creates bitmaps repeatedly, resulting in a waste of memory. For example, you use flash cs5 to create a SWF file and place an image on the main timeline. Therefore, each time you use loader to load this SWF file, many bitmap instances are actually created, occupying a lot of memory. Flash itself has a bitmapdata class to deal with this situation. The specific usage is that multiple displayobjects share one bitmapdata to share bitmap resources and reduce memory overhead. However, you need to manually specify which displayobjects need to share bitmapdata. In this case, if the same SWF file is loaded twice, flash will not automatically share the bitmap resources for us.

Memory usage after loading a SWF file containing a desktop wallpaper image multiple times

After all, Adobe believes that most of the art resources used in Flash are vector images rather than bitmaps. However, for web games, basically all the art resources are bitmap sequential frame animations. For example, a scenario contains 10 players with the same model. If there are 10 bitmap copies of the sequential Frame Animation, this is a huge waste.

A solution
One solution is to use a custom class to play an animation, rather than movieclip. In this way, we can ensure that the same bitmap has only one copy in the memory, that is, a bitmapdata. Therefore, SwF is only used for resource packaging.

Use Flash CS to export the as3 link for the corresponding image in the library, so that we can create the corresponding art resources in the program using the new testbmpclass () method.

Why use SWF instead of custom file formats to package image resources? There are two reasons: one is that for images with Alpha channels, SwF compression is smaller, second, the Flash virtual machine parses SWF files faster than parsing custom format files.

Currently, the company uses a self-contained image format that can perform lossless compression on BMP images, but its size is much larger than that of JPEG + Alpha channels, the decompression speed is also relatively slow (because there is no internal support for avm2 virtual machines ). My task is: a) Convert the company's animated images into SWF files, B) generate as3 links for each image in the SWF library.

Binary assembled SWF File
There are two ways to convert a bunch of BMP images into SWF files in batches. One is to use the batch processing script jsfl that comes with Flash CS, and the other is to directly assemble the binary file to spell out SWF. Because we need to convert a large number of art resources in batches, the efficiency of directly spelling binary files is high, so I use the latter.

The SWF file format Adobe has been open to the outside world. For more information, see swffileformat.pdf.

In short, SWF files are organized in the form of tag files. The file header of each Tag file identifies the length of the current file block and the type of file block. For example, each frame in Flash corresponds toShowframeFile block, and each as3 class corresponds toDoabcFile block. The SWF file to be assembled has the following structure:

The meaning of each Tag file can be roughly guessed by its name. Among them, the white label is required, the orange label is added to export the as3 class, and the blue label is added to place in the scene. As we only need bitmapdata in SWF, images do not need to be placed in the scene. Adding a blue tag actually increases the file size and reduces the parsing speed. However, such a SWF file is blank after being clicked by double-clicking, and is not previewed. Therefore, you can consider exporting the SWF files of preview and release respectively. Preview allows you to preview an animation when you double-click it. The release is a release without a blue label.

Another thing to note is that you do not need to display images in the scene in real time,You also need the showframe tag,Otherwise, the Flash virtual opportunity is stuck there.

Export class for as3
Image resources in SWF cannot be used directly. You need to export the link for as3. That is to say, the corresponding as3 class needs to be generated for each image. The exported class must inherit from flash. display. bitmapdata. At the same time, we can add the required fields (such as centerx and centery) to the class.

During the conversion process, we may need to record some other information, such as framecount and interval. You can create another class to store the information in static variables.

Each as3 class corresponds to a doabc file block. Note that all exported classes must have unique class names. Therefore, it is best to name classes by file name + suffix. In addition, the doabc file block must be followed by a symbolclass file block to associate an as3 class with an image resource.

I am talking about it very briefly. The specific operation is still a bit complicated. Fortunately, the Flash virtual machine avm2 is open-source. For the doabc file format and bytecode instructions, see avm2overview.pdf.

Solve resource duplication
Generally, the exported as3 class names are named after file names, while the resource files produced by art may have duplicate names. Therefore, make sure that each export class has a unique class name. Generally, the path + file name is unique, but we cannot use the path + file name as the class name, because this means that the path will never be changed once the file is generated. Finally, we chooseFile name + generation time + an auto-increment ValueThe class name of the file.

But there is also a problem to be solved. After loading the corresponding SWF file, we need to use the getdefinitionbyname () method to obtain the corresponding class definition, and then the corresponding resource is new. If only the file name is used as the class name, we can easily get the class definition through the file name. If we use the file name + generation time + auto-increment value as the class name, we do not know what the class name is after loading the file.

The solution to this problem is also quite simple. Place a textfield on the main timeline and save the Class Name of the export class in textfield. In this way, after loading SWF, we can get the class name through the textfield in the scenario, and then get the corresponding class definition through the class name.

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: 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.