Reflection is a feature available in many object-oriented languages. However, I often see on forums that some people do not understand the purpose of reflection, or even some people say reflection is useless. Today, I will not talk about how many aspects of reflection can be mined. We will focus on the content of our games, the practical application of reflection, and many things that you don't need to know.
Me
They come to garden games. Garden games have a lot of flowers and a lot of flowers. It is estimated that there are 108 types, and they are constantly increasing. As we all know, flowers must be a good component of flash, so we need to go to the library.
There are also many methods to retrieve data from the database. The simplest thing is to export the vertex in the component in the library, and then instantiate the class in the program. For example, the name of the export class is flower1,
In the program, you can write as follows: var flower = new
Flower1 (); then you can add the instance of this class to the screen. Then, if you do this, you will find many problems: 1. Every time a new flower is added, I have to modify the process.
To re-create the instantiation process of a new class. 2. If you want to separate the main program from these resource files, you cannot even compile your program correctly, because in the main program, there is no
Flower1 class. Therefore, we use reflection. In this case, the reflection function is equivalent to replacing the class name with a string. Because it is a string, you can ignore the fact that this class does not exist. Good
We started to do it. I will briefly talk about our practice. We put all the flower components in a single FLA, and each flower has a separate export, from flower1
Flower108 and then generate flower_asset.swf. We load this SWF file in the main program and load an xml configuration file (how to load
I will describe it in detail later). In short, I load, and read the export class name corresponding to the flower of each ID (for example, the class of the component corresponding to the red rose with ID 20001 is flower1 ),
Now I want to display all the flowers of a user. For example, if the user has an array of flowers and the array contains the following data: [,], then we will create one
All flowerxxx classes are instantiated and then added to the stage. We first add a static method to the document class:
Public static function getmovieclip (mcname: string): movieclip {
Try
{
VaR mcclass: * = Class (getdefinitionbyname (mcname ));
If (mcclass = NULL)
{
Return NULL;
}
Return new mcclass;
}
Catch (E)
{
Return NULL;
}
Return NULL;} note that I use try to ensure system stability, even for some special reasons, we have obtained some wrong or expired data (for example, a flower has been deleted in our database, but it has not been deleted due to planning negligence ), we can also handle this issue without flash errors. Then we can call this method in the program. Based on the class name string obtained from the configuration file, call the getmovieclip method to obtain the corresponding component from the library. Almost all of the classes involved in display finally call this method. Reverse
The advantage of shooting is that it dynamically determines the class that the main program will call. For example, there are only five flowers in a player scenario, we don't need to let the program import 108 flower classes (in terms of performance, reflection is actually needed
Well, because dynamic things are usually slower than static ones, but the performance gains and losses are complicated. You can discuss them again .) From the perspective of development and maintenance, we should minimize the number of compilation and version releases.
To make the program more "customized" is always much better than writing code to the program.