[Switch] the second time in the as3 tutorial series-Performance Optimization

Source: Internet
Author: User

The so-called performance optimization is mainly to prevent the game from being loaded and running.

1. How to optimize the volume of SwF exported by FLA?

1. In flash, the most components on the stage are generated. The larger the SWF generated, the more components with a connection name in the library, and the larger the SWF. The SwF generated when there are no components on the stage and the components in the library are not connected to the names is the smallest.

2. There are 10 frames in a flash animation, and all 10 frames are bitmaps. Who consumes more CPU resources to play these 10 images with a bitmap player?

A: Flash Animation playback consumes more performance, because although it is also a bitmap in the SWF file, the playing mechanism in the SWF is to play bitmap, vector graph, sound, video, etc. So even if you only play the bitmap, the useless code will still be executed.

3. In flash, even if the component is outside the stage, the SWF will become larger.

4. Smooth bitmap in flash can prevent jitter during movement.

5. In flash, the bitmap can be compressed by quality or imported JPEG data. Even if the quality is 100%, it will be compressed to a small extent, if the data is compressed according to the imported JPEG data, the data is also compressed to a small extent. Lossless compression is completely uncompressed.

6. The larger the number of lines of code in the frame, the larger the SWF will be generated (but the memory will be saved if the two lines of code are not merged into one line)

7. On frames in flash, even if no code is written, but there is only space in each line, the SWF size will be increased. The more words in the Code, the more memory is occupied, because letters also occupy memory.

2. How to reduce the size of the primary SWF In Flex?

1. The class library and UI should not be included in the main SWF and adopt the DLL loading mechanism (runtime loading ).

Specific Practices

 

The connection type is external. In this way, the code will not be included in the main SWF.

 

Load the generated DLL file into the game.

2. The main SWF contains only basic loading entries, and the basic background is enough.

3. Do not have too many classes in the project where the primary SWF is located. Because these classes are included by default.

3. How can we smoothly perform loading?

The loading mechanism of some games is to load a lot of resources, resulting in a large amount of loans and performance during loading, resulting in congestion. This is unscientific.

The scientific method is to adopt the queue loading mechanism. Put resources in a queue and load them one by one. Of course, when resources are very small, they can be concurrently loaded at the same time. In this way, the load will be smooth.

4. Performance Optimization:

1. Select appropriate variables. If it is a full number, do not use number. Use uint or Int. If it is unsigned, use uint. Shap or bitmap is used for non-interactive display objects (SHAP implements the drawing API, and bitmap is the display object of Bitmap ). Use sprite or movieclip for interactive objects that do not require timeline (Note: The loader class is also a display object)

2. It is best not to assign values directly to the place where the variable is declared, which will increase the memory overhead without any need, because declaring variables in Flash Player only consumes a very small amount of memory overhead (because Flash Player needs to save their default values ). After initialization, additional overhead will be added, so initialization will be performed as needed.

3. Note that the initial value of uint is 0, the initial value of number is Nan, the initial value of string is null, the initial values of other known types are null, and the initial value of position type is unidefinde. The default memory size of uint and Int Is 4 bytes, and the default memory size of number is 8 bytes.

4. Reuse objects

If you can use one object to complete the function, do not create multiple objects.

For example, a point object is used in many places. You only need to create a new point object, and then change its x and y values to OK.

5. Object pool mechanism

This is very important and can greatly optimize the performance.

For example, if I have a floating box lift, you put all the new floating box objects in two lists, one of which stores the objects in use, A list stores used objects. When you need to use it, first determine whether there are objects in the idle list. If there are objects, retrieve them directly. If not, create a new one and put it in a list.

Another example is that you need to refresh the same type of monsters in a map without any other steps. You don't need to refresh the monsters every time, but just reinitialize those dead monsters.

6. effectively release memory

The Flash Player garbage collection mechanism uses the price clearance mechanism. Because non-primitive variables are passed between references, this object can be recycled only when it is not referenced by any variables.

Generally, set the variable to null and clear events and references.

I will introduce how to release memory in another chapter.

7. performance can be optimized in bitmap and bitmapdata.

First, you must know that bitmapdata contains an array of pixel information, which indicates the information data of the bitmap image.

Therefore, the higher the length and width of bitmapdata, the higher the memory occupied by bitmapdata.

Therefore, when performing bitmap operations, remember to change the width and height of bitmapdata.

For example, when switching a map, the map size is not the same. The size of the first map is 800*600. You can set the width and height of bitmapdata to 800*600, the size of the second map is 500*500 ,. Therefore, it is a waste to use the 800*600 size to operate the 500*500 size.

Remember these methods.

Dispose (); this releases the memory in bitmapdata.

Lock () is to lock the image, so that when bitmapdata information is changed, the image that references this bitmapdata will not be changed

Change. This can save performance.

Unlock (); unlock the image.

Using setvector () to plot pixels is more efficient than setpixel () and setpixels ().

 

8. performance can be optimized in moviechip.

Movieclip is a display object with a timeline. Even if you remove MC, the running timeline still occupies the CPU cycle, when you recycle movieclip, stop the time axis.

9. The performance can be optimized in dictionary.

Set the strong reference in dictionary to true, use Delete to clear the reference of the element in the dictionary, and then set it to null.

For each (var obj: * in DIC)

{

Delete DIC [OBJ]

OBJ. Destroy ();

OBJ = NULL;

}

10. The performance can be optimized in arrary.

Sometimes the arrary is set to null, but the reference is cleared, but the elements in it are not destroyed.

For each (var obj: * In ARR)

{

OBJ. Destroy ();

OBJ = NULL;

}

Arr = NULL;

 

 

11. How to delete a variable in a dynamic class.

You can use Delete to delete a reference, including a dictionary.

12 use as few filters as possible

A raster bitmap is generated using a filter.

When you modify the properties of a bitmap, the source image and the raster bitmap are updated at the same time. CPU usage

13 For low color requirements, you can use 8-or 16-bit color channels.

14 balance between event dispatch and callback

Based on the Flash Player event mechanism, sub-objects are poll from the main stage until the target object.

Therefore, CPU and memory consumption are relatively high. Callback functions are faster and you do not need to create event objects.

Therefore, when using events, you can disable or set their sub-object to listen to events for objects that do not require event listening. MC. mousechildren = false;

MC. mouseenable = false;

MC. Enable = false;

15 Flash Player repainting Mechanism

Re-painting means that Flash Player refreshes the content displayed on the screen at a certain frame rate.

When will it be re-painted?

When the status, shape, and position of an object are displayed on the stage.

Level changed

Re-painting caused by some events, such as hovering the mouse over a button

Reduce repainting

Set visible to false and do not stop re-painting. The animation should be stopped (including child components), preferably

Remove.

When an object is removed from the stage, it is not re-painted. But the code in it is not repainted.

The logic still occupies the CPU cycle and memory.

When setting the hierarchical relationship of displayobject, make a judgment first to improve the performance with a certain probability.

If (mycontainer. getchildindex (mychild )! = 0)
{
Mycontainer. setchildindex (mychild, 0 );
}

When a Sprite/movieclip cacheasbitmap = true, the object has been treated as a whole re-painted area. When a certain area of the object (even if it is covered) changes, this will cause the entire sprite/movieclip to be repainted. One improvement is to use scrollrect to optimize the changed area.

For elements with cacheasbitmap = true, the cached bitmap will also change when the elements change because the elements are cached as bitmaps. This will cause element re-painting, therefore, it is best not to use cached bitmaps for display elements that often change in size and deformation, and occupy the memory.

16 scrollrect and mask

When using mask or scrollrect, invisible areas consume the same CPU for rendering. If necessary, set the Display object attribute in these invisible areas to false. use copypixels instead of scrollrect or mask in some game scrolling screens.

17 bitmap cache

The bitmap cache allows the Display object to be rendered as a bitmap. During repainting, the display object can be repainted as a bitmap. For example, when movieclip is re-painted, the re-painting and frame of sub-objects must be taken into account. These parts are ignored when bitmap is used for caching. However, W bitmap cache cannot be abused.

When Will bitmap cache be used?

When the object only needs simple translation and dragging. Or static. There is no need for complex internal changes.

Scrolling text fields, which is not easy to understand in flash. In fact, it means to put the text box to be rolled into MC, use scrollrect to scroll to MC, and then bitmap MC. In this way, you do not need to re-paint the entire textfield because it has been converted into a bitmap.

In the window system, panel objects are generally dragged and moved without too many changes.

When should bitmap be avoided?

Bitmap objects occupy more memory than source objects. Enabling the surface only improves the rendering performance.

Cache is not suitable for operations that are too complex, such as rotation, scaling, flip, and animation.

Do not place bitmap cache with vector data.

18. Reduce the nesting of display objects.

The more nesting in the display, the higher the flashplayer performance. Reduce the number of operations that rely on the parent object to the child object, such as the location of the parent object. Not only child objects, but also parent objects.

19. vector is faster than array class access.

20 var OBJ: Object ={}; var arr: array = []. This makes the assignment more efficient.

21 use the appedtext () method for textfield objects. Do not use the + = operation.

In a for loop, assign a value to textfield. Do not operate text directly. Instead, assign the value

A string. After the loop ends, another string is assigned to text.

22. Instead of directly using brackets, assign the value to a variable and perform operations on the variable.

Arr [I]. x = 100; this is incorrect.

VaR MC: * = arr [I]; Mc. x = 100; this is correct.

23 avoid expression in Loop

For (var I: uint = 0; I <arr. length; I ++); Error

VaR length: uint = arr. lenth;

For (var I: uint = 0; I <length; I ++.

The error method calculates the length of ARR every time.

24. Using appropriate stage quality can improve the smoothness of the game.

Stagequality. Low low rendering quality

25 when using the Alpha attribute, avoid using the Alpha mixed effect. If the column is deprecated, the Alpha hybrid will occupy a large amount of processing resources of Flash Player and avoid using the Alpha attribute whenever possible.

27 select the appropriate frame rate. The higher the frame rate, the higher the consumption performance.

Dynamic Frame Rate Change

Change the frame rate to 1 when there is no animated content.

28 bit operations as far as possible

For example, 10/2 is equivalent to 10> 1, but bit operations are faster.

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.