Flash social game development strategy (IV)-basic control flow 3

Source: Internet
Author: User

I was busy some time ago, so I haven't updated it. Sorry. Let's talk about the control flow.

This time, I will mainly talk about two things: 1. Improve the function of inserting objects and removing objects.
2. Update the display list when appropriate, and update layer depth 1 to insert and remove objects. We imagine that because of program requirements, we need to insert an object in the scene, but because of other factors, this object needs to be removed immediately, and the interval between them is very small, as small as between two frames, at this time, we do not need to insert this object into the display list (this situation may occur even though it is not much ). Corresponding to public var objectstoadd: array (Object array to be added), we create an array public var objectstoremove: array (Object array to be removed) then, in the addobject method, we can determine whether the object also exists in objectstoremove. If so, the object is removed from the objectstoremove array. Otherwise, the object is added to the objectstoadd array, and the removeobject method is also processed. The Code is as follows: public function addobject (OBJ: baseobject): void {var removeindex: int; If (OBJ) {removeindex = objectstoremove. indexof (OBJ); If (removeindex! =-1) {objectstoremove. splice (removeindex, 1);} else if (objectstoadd. indexof (OBJ) =-1 & baseobjects. indexof (OBJ) =-1) {objectstoadd. push (OBJ); If (obj. parent! = NULL & obj. parent is baseobject) {baseobject (obj. Parent). removeobject (OBJ) ;}} return ;} move
The same is true if the code is output. In this way, we will eventually get an array of objects to be added to the display list and removed from the display list. Note that, no matter how you add or remove,
Before entering the next frame, the content in the display list is not changed and will not affect the display performance, and the content in baseobjects will not change.
The content in baseobjects is always consistent with the content in the real list. The above two methods pave the way for the method addandremoveobjects () in tickbase. Next I will discuss the role characteristics of addandremoveobjects.

I
Once you start playing the as3 game, you will soon find a major problem, that is, layer-Depth Control. The middle-layer Deep control of as2 may seem simple. You can set the depth of the layer, but this is caused
Adobe canceled, probably because of poor performance. Instead, we use the addchildat method to replace it. As a result, we may not be able to intuitively control the layer depth we want to control.
Therefore, we use addandremoveobjects ()
The method controls the relationship between layers based on the new property drawpriority (painting priority. The advantage is that, no matter how many layers in a scenario need to change, we will only sort these objects once when entering the next frame. Public Function addandremoveobjects (): void {var OBJ: baseobject; var index: int; var isprio: Boolean; If (objectstoremove. length> 0) {for each (var I in objectstoremove)
{
OBJ = I;
Index = baseobjects. indexof (OBJ );
If (index! =-1) {baseobjects. splice (index, 1);} If (obj. parent! = NULL & obj. Parent = This) {removechild (OBJ );}

} Objectstoremove. splice (0, objectstoremove. Length );}
If (objectstoadd. length> 0) {for each (var k in objectstoadd)
{
Isprio = false;
For (var j = 0; j <baseobjects. length; j ++)
{
If (K. drawpriority <baseobjects [J]. drawpriority)
{
Addchildat (K, getchildindex (baseobjects [J]);
Baseobjects. splice (J, 0, k );
Isprio = true;
Break;
}
}
If (! Isprio)

{
Baseobjects. Push (k );
Addchild (k );


}


} Objectstoadd. splice (0, objectstoadd. Length );}
Return;} a simple explanation of the Code involves two tasks: 1. Remove the objects in the objectstoremove array from baseobjects and remove them from the display list, clear objectstoremove2 according to the drawpriority attribute of each object in objectstoadd, insert the object into the display list and baseobjects array in sequence, and clear objectstoadd.
In this way, we can complete the sorting of all the "sub" in the object and reset the display layer depth. And each frame can be sorted only once at most. Good
Now we have completed more than half of the baseobject class. Has two methods to add and delete objects, and will pass the frame interval to the sub-objects of the object and the sub-objects of the object
And ...... (Too many don't write), and has the drawpriority attribute, as long as you change this attribute, when entering the next frame will change the hierarchy in the display list. Next, as long as we inherit this class, we can easily control the behavior of each object (as long as we re-write the tick method in the class ), and easily control their hierarchies (because it is only done at the frame interval, there will be no latency on the naked eye, and the execution efficiency will be relatively high ).
You can write such a class and use it for reference. Remember to call the tickbase function of the outermost baseobject object in the document class, and then try to add or delete it, modify the layer depth and overwrite the operations of the tick method, so that the structure of our basic control flow is almost the same. Next we will talk about how to use reflection to simplify and implement modular development.

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.