When running SWF, determine whether two displayobjects belong to the same type and belong to the same component in the Flash Professional Library.

Source: Internet
Author: User

Generally, we can determine whether the two instance objects are of the same type. We can use typeof to obtain the object type and then compare them with the = sign.

Typeof applies to native types.

 

For user-defined types, although typeof obtains all objects, there is a stronger trick: getqualifiedclassname

The native function can be used to obtain the real types of the two instances.

However, when running the SWF created by Flash Professional, you need to know whether the two movieclip come from the same component in the library. None of the above methods can be used.

 

This article discusses this issue.

1. First of all, I think that if the two instances are of the same type, there should be a similar structure in the memory. However, Flash does not directly obtain the memory interface;

2. You can find bytearray instead of directly obtaining the memory. Bytearray. writeobject can be used to serialize objects in AMF mode.

3. However, the bytearray obtained by directly serializing two movieclip is certainly different, because dynamic variable values, such as x/y, are generated after all.

4. What if the movieclip itself is writeobject instead of its child element writeobject? I tried this method once, but at the end of the test I found that this is not universally applicable because sub-components may also have dynamic things, such as name. Various instancexxx names are assigned during running.

5. Aside from movieclip, how can we compare the most basic shapes with the same one? Shape is nothing more than drawing, so it is best to compare whether the drawing data is consistent.

Graphics. readgraphicsdata () can obtain the vector, and the content in the vector is static.

Combined with bytearray. writeobject, you can easily compare whether the two shapes are consistent.

6. Use the above shape thinking to handle movieclip in the same way. Obtain graphics data every frame, and writeobject is included in bytearray. Finally, compare.

 

Code details:

/*** Determine whether the same component from the Flash Professional Library * @ Param A * @ Param B */private function equals (A: displayobject, B: displayobject ): boolean {If (A is movieclip & B is movieclip) | (A is shape & B is shape) {var bytearraya: bytearray = new bytearray (); vaR bytearrayb: bytearray = new bytearray (); if (a is movieclip) {var MCA: movieclip = A as movieclip; var MCB: movieclip = B As movieclip; var isplayinga: Bo Olean = MCA. isplaying; var isplayingb: Boolean = McB. isplaying; var currentframea: Int = MCA. currentframe; var currentframeb: Int = McB. currentframe; For (var k: Int = 1; k <= MCA. totalframes; k ++) {MCA. gotoandstop (k); bytearraya. writeobject (MCA. graphics. readgraphicsdata () ;}for (var I: Int = 1; I <= McB. totalframes; I ++) {McB. gotoandstop (I); bytearrayb. writeobject (MCB. graphics. readgraphicsdata ()) ;} If (isplayinga) MCA. gotoandplay (currentframea); else MCA. gotoandstop (currentframea); If (isplayingb) McB. gotoandplay (currentframeb); else McB. gotoandstop (currentframeb);} else if (a is shape) {var shapea: Shape = A as shape; var shapeb: Shape = B as shape; bytearraya. writeobject (shapea. graphics. readgraphicsdata (); bytearrayb. writeobject (shapeb. graphics. readgraphicsdata ();} If (bytearraya. length! = Bytearrayb. Length) {return false;} else {bytearraya. Position = bytearrayb. Position = 0; while (bytearraya. bytesavailable) {If (bytearraya. readbyte ()! = Bytearrayb. readbyte () return false;} return true ;}} return false ;}

 

 

 

Verification test:

Case 1:

A movieclip, which is placed in two copies on the stage.

Only one copy of movieclip data and one copy of Shape Data

The algorithm is valid!

 

 

Case 2:

Based on scenario 1, let the replica components be slightly different.

Two copies of Shape Data and two copies of movieclip data are obtained during runtime detection.

The algorithm is valid!

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.