GamePackage LoadQueue

xiaoxiao2021-04-07  360

The last article said to Loader, this one of the LoadQueue is a very headache. It feels that the realm of the author has arrived in the distance of the devil. This is why it is --_- ||

The implementation of LoadQueue inherits Array, without combining, in design mode, may lack elastic, but this is not an important place. So no matter.

First look at the constructor first

Understand the simple words, use a cycle.

For (var i = 0; i

THIS [I] = arguments [i];

However, the author used

Splice.Apply (this, [0, 0] .concat (arguments));

Splice and concat are Array methods, because LoadQueue inherits Array, so it also has these methods. The results of this sentence are the same as the above cycle. It is just that the author has applied existing methods.

The SPLICE method is to start the third parameter, followed by each parameter as an element, one one inserted into an array, and the first two parameters are the starting position and replacement number of the insertion;

Concat is a combination of two arrays;

Apply is a method of function, and the two parameters specify a list of arguments and call methods, respectively.

(Detailed functions usage help)

The whole sentence, the translation is

This.SPLICE (0, 0, Arguments [0], Arguments [1], .... arguments [arguments.length-1]); // this point to loadQueue

Then, like the Loader, use AsbroadCaster to initially loadqueue to make it broadcast capabilities.

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

Skip the Load method, a familiar ON method in front of you, it seems to be the event that Loader has, how can I define it in loadQueue? Look at what to do ~

After reading it, it is found that the same name is broadcast, and the writing of the broadcast statement is also very similar to the SPLICE usage in the constructor. With the above experience, give him another translate.

Take OnLoaderstart as an Example:

This.BroadcastMessage ("OnLoaderstart", Arguments [0], Arguments [1], ...., ...., ....

Oh ... I have been in the year, it is the online mobilerostart event after the onlineerstart method. Who is the onloaderstart? Who is it called?

Look back, look at the Load method, found

CurrentLoader.Addlistener (this);

This sentence

This understands that those methods that are defined by LoadQueue are loader events. Or's LoadQueue becomes a listener of Loader.

Look at the implementation of the OnLoaderinit method, it is clearer, after the Loader confirms the download, first broadcast the LoadQueue's onlineerinit event, then remove the loadqueue from the current Loader list, save resources, then execute an LOAD method, To traverse the entire queue.

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

Use the Loader and Loadqueue class

With such two classes, there are more people, ^^ _

Var loadQueue: loadQueue = new loadQueue ();

Var loadQueuelistener: object = new object ();

Loadqueue.addlistener (loadQueuelistener);

Loadqueuelistener.onLoaderstart = function () {};

// ...

LoadQueue.push (New Loader (MC1, URL1));

LoadQueue.push (New Loader (MC2, URL2));

Loadqueue.push (New Loader (MC3, URL3)); loadQueue.load ();

It can be seen that it is indeed convenient, but it seems that there is a defect, it is the same thing in the three Loader's triggering events. If I want to do some things, what should I do?

Of course, you can judge whether it is the second loader in LoadQueuelistener.onLoaderstart, but it is a bit of hardship. If there is 10 Loader, each Loader's onlinestart is different, isn't this loadQueuelistener.onLoadersTart to write a hundred files It's not easy to maintain it.

The best way is to take the second loader.

...

Var loader2: loader = New loader (MC2, URL2);

Loader2.onLoaderstart = function () {};

...

LoadQueue.push (New Loader (MC1, URL1));

Loadqueue.push (loader2);

...

If the program is written this, when executed, it will trigger the Loader's own event and the LoadQueue defined event ... and you want to let Loader trigger a custom event, do not want to execute the general event of loadQueue, that is also Simple, just write a sentence in the general event of loadQueue.

Loadqueue.onLoaderstart = Function (Loader: Loader)

{

IF (loader.onloaderstart! = null) return;

// Left general execution code

}

I was written here, but I was finished, but I seem to have not said that the author will take a fire into the magic and responsible ~ _ ~ |.

In fact, everyone can see if the author does not monitor the event method of Loadue in loading loader.

Private

These methods are obviously not used outside.

Although we know that there are some grammar shapes of OOP, it is not good, but it is not good, there is not good, at least increases readability. But the author is not adding private? Then is a trick, or defeated ?? ..... Even more analytical analysis.

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

Re-open the Loader class to see.

In the constructor of the Loader class, we saw AddListener (this); Description Loader is listening to yourself at the beginning. So we can

Var loader: loader = new loader;

Loader.onLoaderstart = function () {};

Loader.Load ();

This can be defined directly on the Loader object.

In the constructor of the LoadQueue class, there is no AddListener (this);

That is, LoadQueue is just a broadcaster, and it is necessary to establish a listening object outside, like this.

Var loaderQueue: loaderqueue = new loaderqueue (loader1, loader2, loader2);

Var loaderqueuelistener: Object = new object ();

Loaderqueue.Addlistener (loaderqueuelistener);

LoaderQueuelistener.onLoaderstart = function () {};

LoaderQueue.Load ();

This will listen to the event of loaderqueue. (Similarly to MovieCliploader)

The internal execution steps triggered by the above LoadQueue event are

The current Loader starts to broadcast the loader.onloaderstart event, at this time, the LoadQueue object is a listener, so performs the onlineerstart method.

Broadcast loadqueue.onLoaderstart event by the onlineerstart method

Due to loadQueue events and Loader's event is the same name, because LoadQueue is the listener of Loader, and the event method is not private, the event method is rewritten directly on the external loadqueue object, nor does it have any warning prompts, Flash will think you Just rewrite this function.

Var loaderQueue: loaderqueue = new loaderqueue (loader1, loader2, loader2);

LoaderQueue.onLoaderstart = function () {};

LoaderQueue.Load ();

So after the Loader event broadcasts, this rewriting function will be executed, so the effect is the same as that of LoadQueue is broadcast.

The internal execution step triggered by the LoadQueue event is

The current loader starts to broadcast the loader.onloaderstart event, at this time, the loadQueue object is a listener, so the onlineerstart method is executed.

However, because the ONLoaderstart is rewritten outside, it is performed by the external overwritten program.

In this way, it seems that there is no difference. Anyway, the executive event is executed, but the latter has made a great mistake. That is to give the original broadcast capacity of LoadQueue!

Think about it, if the exterior rewritten program does not perform broadcast, how to go to AddQueue to AddListener, Listener's listening event will not be executed.

There is an event named OnLoadQueueComplete, which is also built directly on the loadQueue object, and this event is not triggered, because there is no such as the same name in the Loader.

So we can see that LoadQueue is designed, like MovieCliploader, itself is not a listener.

The above seemingly correct calls are just a coincidence between Loader and LoadQueue. This coincidence is so high, that is, because of Loader and LoadQueue design similar but different, resulting in people's inertial thinking ... But there is no warning prompt, the most important reason is to disclose some of the private functions in loadQueue.

At this point, we should add private modifications to each corresponding loader in loadQueue, prevent it from being rewritten or called outside.

In addition, the addListener (this) within the Loader constructor is removed; and the 5 public function attributes are removed, making it only to add a listener through the outside, so that Loader and LoadQueue are designed in design.

As for AS2 fragile compilation, we can only avoid it.

Then, why not let LoadQueue listen to yourself? This is not, just like this, you have to make a big surgery, you should change the LoadQueue's event name to Loader. Otherwise still uses the existing existing Program, when the current loader is started, broadcast OnLoadStart, execute the LoadQueue's onlineerstart method, but LoadQueue itself is also listening to yourself, broadcasting is also OnLoaderstart, which forms an unlimited loop.

转载请注明原文地址:https://www.9cbs.com/read-132529.html

New Post(0)